2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "util/ConfigSource.h"
18 #include "util/GeneralConfigSource.h"
19 #include "util/EnvConfigSource.h"
32 static std::unique_ptr<IConfigSource> _source;
34 void config_source(std::unique_ptr<IConfigSource> &&source) { _source = std::move(source); }
36 static IConfigSource *config_source()
40 #ifdef ENVVAR_FOR_DEFAULT_CONFIG
41 // Default ConfigSource is EnvConfigSource
42 _source = std::make_unique<EnvConfigSource>();
44 _source = std::make_unique<GeneralConfigSource>();
45 #endif // ENVVAR_FOR_DEFAULT_CONFIG
50 static std::string getConfigOrDefault(const std::string &key)
52 static std::unordered_map<std::string, std::string> defaults;
55 #define CONFIG(Name, Type, Default) \
57 auto name = std::string{#Name}; \
58 defaults.emplace(name, std::string{Default}); \
61 #include "util/Config.lst"
66 // Treat empty string and absence of the value to be the same
67 auto ret = config_source()->get(key);
70 auto itr = defaults.find(key);
71 if (itr != defaults.end())
73 // Return the default value if exists
81 bool toBool(const std::string &val)
83 static const std::array<std::string, 5> false_list{"0", "OFF", "FALSE", "N", "NO"};
84 auto false_found = std::find(false_list.begin(), false_list.end(), val);
85 return false_found == false_list.end();
88 int toInt(const std::string &val) { return std::stoi(val); }
90 bool getConfigBool(const std::string &key)
92 auto raw = getConfigOrDefault(key);
96 int getConfigInt(const std::string &key)
98 auto raw = getConfigOrDefault(key);
102 std::string getConfigString(const std::string &key) { return getConfigOrDefault(key); }
114 #define CONFIG(Name, Type, Default) const char *Name = #Name;
116 #include "util/Config.lst"
120 } // namespace config