Debian Codegraph @

src/core/lib/gprpp/global_config_env.h

raw
1 /*
2 *
3 * Copyright 2019 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #ifndef GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_ENV_H
20 #define GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_ENV_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include <stdint.h>
25
26 #include "src/core/lib/gprpp/global_config_generic.h" // IWYU pragma: export
27 #include "src/core/lib/gprpp/memory.h"
28
29 namespace grpc_core {
30
31 typedef void (*GlobalConfigEnvErrorFunctionType)(const char* error_message);
32
33 /*
34 * Set global_config_env_error_function which is called when config system
35 * encounters errors such as parsing error. What the default function does
36 * is logging error message.
37 */
38 void SetGlobalConfigEnvErrorFunction(GlobalConfigEnvErrorFunctionType func);
39
40 // Base class for all classes to access environment variables.
41 class GlobalConfigEnv {
42 protected:
43 // `name` should be writable and alive after constructor is called.
44 constexpr explicit GlobalConfigEnv(char* name) : name_(name) {}
45
46 public:
47 // Returns the value of `name` variable.
48 UniquePtr<char> GetValue();
49
50 // Sets the value of `name` variable.
51 void SetValue(const char* value);
52
53 // Unsets `name` variable.
54 void Unset();
55
56 protected:
57 char* GetName();
58
59 private:
60 char* name_;
61 };
62
63 class GlobalConfigEnvBool : public GlobalConfigEnv {
64 public:
65 constexpr GlobalConfigEnvBool(char* name, bool default_value)
66 : GlobalConfigEnv(name), default_value_(default_value) {}
67
68 bool Get();
69 void Set(bool value);
70
71 private:
72 bool default_value_;
73 };
74
75 class GlobalConfigEnvInt32 : public GlobalConfigEnv {
76 public:
77 constexpr GlobalConfigEnvInt32(char* name, int32_t default_value)
78 : GlobalConfigEnv(name), default_value_(default_value) {}
79
80 int32_t Get();
81 void Set(int32_t value);
82
83 private:
84 int32_t default_value_;
85 };
86
87 class GlobalConfigEnvString : public GlobalConfigEnv {
88 public:
89 constexpr GlobalConfigEnvString(char* name, const char* default_value)
90 : GlobalConfigEnv(name), default_value_(default_value) {}
91
92 UniquePtr<char> Get();
93 void Set(const char* value);
94
95 private:
96 const char* default_value_;
97 };
98
99 } // namespace grpc_core
100
101 // Macros for defining global config instances using environment variables.
102 // This defines a GlobalConfig*Type* instance with arguments for
103 // mutable variable name and default value.
104 // Mutable name (g_env_str_##name) is here for having an array
105 // for the canonical name without dynamic allocation.
106 // `help` argument is ignored for this implementation.
107
108 #define GPR_GLOBAL_CONFIG_DEFINE_BOOL(name, default_value, help) \
109 static char g_env_str_##name[] = #name; \
110 static ::grpc_core::GlobalConfigEnvBool g_env_##name(g_env_str_##name, \
111 default_value); \
112 bool gpr_global_config_get_##name() { return g_env_##name.Get(); } \
113 void gpr_global_config_set_##name(bool value) { g_env_##name.Set(value); }
114
115 #define GPR_GLOBAL_CONFIG_DEFINE_INT32(name, default_value, help) \
116 static char g_env_str_##name[] = #name; \
117 static ::grpc_core::GlobalConfigEnvInt32 g_env_##name(g_env_str_##name, \
118 default_value); \
119 int32_t gpr_global_config_get_##name() { return g_env_##name.Get(); } \
120 void gpr_global_config_set_##name(int32_t value) { g_env_##name.Set(value); }
121
122 #define GPR_GLOBAL_CONFIG_DEFINE_STRING(name, default_value, help) \
123 static char g_env_str_##name[] = #name; \
124 static ::grpc_core::GlobalConfigEnvString g_env_##name(g_env_str_##name, \
125 default_value); \
126 ::grpc_core::UniquePtr<char> gpr_global_config_get_##name() { \
127 return g_env_##name.Get(); \
128 } \
129 void gpr_global_config_set_##name(const char* value) { \
130 g_env_##name.Set(value); \
131 }
132
133 #endif /* GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_ENV_H */