GRPC Core  9.0.0
global_config_env.h
Go to the documentation of this file.
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 
23 
26 
27 namespace grpc_core {
28 
29 typedef void (*GlobalConfigEnvErrorFunctionType)(const char* error_message);
30 
31 /*
32  * Set global_config_env_error_function which is called when config system
33  * encounters errors such as parsing error. What the default function does
34  * is logging error message.
35  */
37 
38 // Base class for all classes to access environment variables.
40  protected:
41  // `name` should be writable and alive after constructor is called.
42  constexpr explicit GlobalConfigEnv(char* name) : name_(name) {}
43 
44  public:
45  // Returns the value of `name` variable.
47 
48  // Sets the value of `name` variable.
49  void SetValue(const char* value);
50 
51  // Unsets `name` variable.
52  void Unset();
53 
54  protected:
55  char* GetName();
56 
57  private:
58  char* name_;
59 };
60 
62  public:
63  constexpr GlobalConfigEnvBool(char* name, bool default_value)
64  : GlobalConfigEnv(name), default_value_(default_value) {}
65 
66  bool Get();
67  void Set(bool value);
68 
69  private:
70  bool default_value_;
71 };
72 
74  public:
75  constexpr GlobalConfigEnvInt32(char* name, int32_t default_value)
76  : GlobalConfigEnv(name), default_value_(default_value) {}
77 
78  int32_t Get();
79  void Set(int32_t value);
80 
81  private:
82  int32_t default_value_;
83 };
84 
86  public:
87  constexpr GlobalConfigEnvString(char* name, const char* default_value)
88  : GlobalConfigEnv(name), default_value_(default_value) {}
89 
91  void Set(const char* value);
92 
93  private:
94  const char* default_value_;
95 };
96 
97 } // namespace grpc_core
98 
99 // Macros for defining global config instances using environment variables.
100 // This defines a GlobalConfig*Type* instance with arguments for
101 // mutable variable name and default value.
102 // Mutable name (g_env_str_##name) is here for having an array
103 // for the canonical name without dynamic allocation.
104 // `help` argument is ignored for this implementation.
105 
106 #define GPR_GLOBAL_CONFIG_DEFINE_BOOL(name, default_value, help) \
107  static char g_env_str_##name[] = #name; \
108  static ::grpc_core::GlobalConfigEnvBool g_env_##name(g_env_str_##name, \
109  default_value); \
110  bool gpr_global_config_get_##name() { return g_env_##name.Get(); } \
111  void gpr_global_config_set_##name(bool value) { g_env_##name.Set(value); }
112 
113 #define GPR_GLOBAL_CONFIG_DEFINE_INT32(name, default_value, help) \
114  static char g_env_str_##name[] = #name; \
115  static ::grpc_core::GlobalConfigEnvInt32 g_env_##name(g_env_str_##name, \
116  default_value); \
117  int32_t gpr_global_config_get_##name() { return g_env_##name.Get(); } \
118  void gpr_global_config_set_##name(int32_t value) { g_env_##name.Set(value); }
119 
120 #define GPR_GLOBAL_CONFIG_DEFINE_STRING(name, default_value, help) \
121  static char g_env_str_##name[] = #name; \
122  static ::grpc_core::GlobalConfigEnvString g_env_##name(g_env_str_##name, \
123  default_value); \
124  ::grpc_core::UniquePtr<char> gpr_global_config_get_##name() { \
125  return g_env_##name.Get(); \
126  } \
127  void gpr_global_config_set_##name(const char* value) { \
128  g_env_##name.Set(value); \
129  }
130 
131 #endif /* GRPC_CORE_LIB_GPRPP_GLOBAL_CONFIG_ENV_H */
void Unset()
Definition: global_config_env.cc:67
Definition: global_config_env.h:85
constexpr GlobalConfigEnvString(char *name, const char *default_value)
Definition: global_config_env.h:87
void(* GlobalConfigEnvErrorFunctionType)(const char *error_message)
Definition: global_config_env.h:29
Round Robin Policy.
Definition: backend_metric.cc:24
grpc_core::UniquePtr< char > GetValue()
Definition: global_config_env.cc:59
constexpr GlobalConfigEnvInt32(char *name, int32_t default_value)
Definition: global_config_env.h:75
void Set(bool value)
Definition: global_config_env.cc:94
constexpr GlobalConfigEnvBool(char *name, bool default_value)
Definition: global_config_env.h:63
const char * error_message
Definition: lame_client.cc:53
void Set(const char *value)
Definition: global_config_env.cc:133
Definition: global_config_env.h:39
Definition: global_config_env.h:61
int32_t Get()
Definition: global_config_env.cc:101
std::unique_ptr< T, DefaultDeleteChar > UniquePtr
Definition: memory.h:45
constexpr GlobalConfigEnv(char *name)
Definition: global_config_env.h:42
bool Get()
Definition: global_config_env.cc:80
void SetGlobalConfigEnvErrorFunction(GlobalConfigEnvErrorFunctionType func)
Definition: global_config_env.cc:55
Definition: global_config_env.h:73
void Set(int32_t value)
Definition: global_config_env.cc:116
void SetValue(const char *value)
Definition: global_config_env.cc:63
grpc_core::UniquePtr< char > Get()
Definition: global_config_env.cc:125
char * GetName()
Definition: global_config_env.cc:69