45cce662e09ad250a4c0fe694098ff8904dc6b58
[platform/core/ml/nnfw.git] / runtime / onert / core / src / util / ConfigSource.cc
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "util/ConfigSource.h"
18 #include "util/GeneralConfigSource.h"
19 #include "util/EnvConfigSource.h"
20
21 #include <array>
22 #include <algorithm>
23 #include <cassert>
24
25 #include <memory>
26
27 namespace onert
28 {
29 namespace util
30 {
31
32 static std::unique_ptr<IConfigSource> _source;
33
34 void config_source(std::unique_ptr<IConfigSource> &&source) { _source = std::move(source); }
35
36 static IConfigSource *config_source()
37 {
38   if (!_source)
39   {
40 #ifdef ENVVAR_FOR_DEFAULT_CONFIG
41     // Default ConfigSource is EnvConfigSource
42     _source = std::make_unique<EnvConfigSource>();
43 #else
44     _source = std::make_unique<GeneralConfigSource>();
45 #endif // ENVVAR_FOR_DEFAULT_CONFIG
46   }
47   return _source.get();
48 }
49
50 static std::string getConfigOrDefault(const std::string &key)
51 {
52   static std::unordered_map<std::string, std::string> defaults;
53   if (defaults.empty())
54   {
55 #define CONFIG(Name, Type, Default)               \
56   {                                               \
57     auto name = std::string{#Name};               \
58     defaults.emplace(name, std::string{Default}); \
59   }
60
61 #include "util/Config.lst"
62
63 #undef CONFIG
64   }
65
66   // Treat empty string and absence of the value to be the same
67   auto ret = config_source()->get(key);
68   if (ret.empty())
69   {
70     auto itr = defaults.find(key);
71     if (itr != defaults.end())
72     {
73       // Return the default value if exists
74       ret = itr->second;
75     }
76   }
77
78   return ret;
79 }
80
81 bool toBool(const std::string &val)
82 {
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();
86 }
87
88 int toInt(const std::string &val) { return std::stoi(val); }
89
90 bool getConfigBool(const std::string &key)
91 {
92   auto raw = getConfigOrDefault(key);
93   return toBool(raw);
94 }
95
96 int getConfigInt(const std::string &key)
97 {
98   auto raw = getConfigOrDefault(key);
99   return toInt(raw);
100 }
101
102 std::string getConfigString(const std::string &key) { return getConfigOrDefault(key); }
103
104 } // namespace util
105 } // namespace onert
106
107 namespace onert
108 {
109 namespace util
110 {
111 namespace config
112 {
113
114 #define CONFIG(Name, Type, Default) const char *Name = #Name;
115
116 #include "util/Config.lst"
117
118 #undef CONFIG
119
120 } // namespace config
121 } // namespace util
122 } // namespace onert