@@ -18,113 +18,149 @@ limitations under the License.
1818#define _Context_
1919
2020#include < map>
21- #include < sstream>
2221#include < string>
23- #include " concurrent.hpp"
24- #include " util/strings.hpp"
25-
26- namespace kanzi
27- {
28-
29- // Poor's man equivalent to std::variant used to support C++98 and up.
30- // union cannot be used due to the std:string field.
31- // The extra memory used does not matter for the application context since
32- // the map is small.
33- typedef struct ContextVal {
34- int64 lVal;
35- std::string sVal ;
36- bool isString;
37-
38- ContextVal (bool b, int64 val, const std::string& str) : lVal(val), sVal (str), isString(b) {}
39- ContextVal () { isString = false ; lVal = 0 ; }
40- } ctxVal;
41-
42- class Context
43- {
22+
23+ #if __cplusplus >= 201703L
24+ #include < variant>
25+ #endif
26+
27+ #include " types.hpp"
28+
29+ namespace kanzi {
30+
31+ #if __cplusplus >= 201703L
32+ // C++17+ version using std::variant
33+ typedef std::variant<int64, std::string> ContextVal;
34+
35+ #else
36+ // C++98 / C++03 / C++11 / C++14
37+ struct ContextVal {
38+ int64 lVal;
39+ std::string sVal ;
40+ bool isString;
41+
42+ ContextVal () : lVal(0 ), isString(false ) {}
43+ ContextVal (int64 v) : lVal(v), isString(false ) {}
44+ ContextVal (const std::string& s) : lVal(0 ), sVal (s), isString(true ) {}
45+ };
46+ #endif
47+
48+
49+ class Context {
4450 public:
4551
46- #ifdef CONCURRENCY_ENABLED
52+ #ifdef CONCURRENCY_ENABLED
4753 Context (ThreadPool* p = nullptr ) : _pool(p) {}
4854 Context (const Context& c) : _map(c._map), _pool(c._pool) {}
4955 Context (const Context& c, ThreadPool* p) : _map(c._map), _pool(p) {}
5056 Context& operator =(const Context& c) = default ;
51- #else
57+ #else
5258 Context () {}
5359 Context (const Context& c) : _map(c._map) {}
54- Context& operator =(const Context& c) { _map = c._map ; return *this ; };
55- #endif
60+ Context& operator =(const Context& c) { _map = c._map ; return *this ; }
61+ #endif
62+
63+ ~Context () {}
5664
57- virtual ~Context () {}
5865 bool has (const std::string& key) const ;
66+
5967 int getInt (const std::string& key, int defValue = 0 ) const ;
6068 int64 getLong (const std::string& key, int64 defValue = 0 ) const ;
61- std::string getString (const std::string& key, const std::string& defValue = " " ) const ;
69+ std::string getString (const std::string& key,
70+ const std::string& defValue = " " ) const ;
71+
6272 void putInt (const std::string& key, int value);
6373 void putLong (const std::string& key, int64 value);
6474 void putString (const std::string& key, const std::string& value);
6575
66- #ifdef CONCURRENCY_ENABLED
76+ #ifdef CONCURRENCY_ENABLED
6777 ThreadPool* getPool () const { return _pool; }
68- #endif
78+ #endif
6979
7080 private:
7181 std::map<std::string, ContextVal> _map;
7282
73- #ifdef CONCURRENCY_ENABLED
83+ #ifdef CONCURRENCY_ENABLED
7484 ThreadPool* _pool;
75- #endif
85+ #endif
7686 };
7787
7888
7989 inline bool Context::has (const std::string& key) const
8090 {
81- return _map.find (key) != _map.end ();
91+ return _map.find (key) != _map.end ();
8292 }
8393
8494
8595 inline int Context::getInt (const std::string& key, int defValue) const
8696 {
87- return int (this -> getLong (key, defValue));
97+ return int (getLong (key, defValue));
8898 }
8999
90100
91101 inline int64 Context::getLong (const std::string& key, int64 defValue) const
92102 {
93- std::map<std::string, ContextVal>::const_iterator it = _map.find (key);
103+ const std::map<std::string, ContextVal>::const_iterator it = _map.find (key);
94104
95- if (it == _map.end ())
105+ if (it == _map.end ())
96106 return defValue;
97107
98- return it->second .isString == true ? defValue : it->second .lVal ;
108+ #if __cplusplus >= 201703L
109+ if (std::holds_alternative<int64>(it->second ))
110+ return std::get<int64>(it->second );
111+
112+ return defValue;
113+ #else
114+ return it->second .isString ? defValue : it->second .lVal ;
115+ #endif
99116 }
100117
101118
102- inline std::string Context::getString (const std::string& key, const std::string& defValue) const
119+ inline std::string Context::getString (const std::string& key,
120+ const std::string& defValue) const
103121 {
104- std::map<std::string, ContextVal>::const_iterator it = _map.find (key);
122+ const std::map<std::string, ContextVal>::const_iterator it = _map.find (key);
105123
106- if (it == _map.end ())
107- return defValue;
124+ if (it == _map.end ())
125+ return defValue;
108126
109- return it->second .isString == true ? it->second .sVal : defValue;
127+ #if __cplusplus >= 201703L
128+ if (std::holds_alternative<std::string>(it->second ))
129+ return std::get<std::string>(it->second );
130+
131+ return defValue;
132+ #else
133+ return it->second .isString ? it->second .sVal : defValue;
134+ #endif
110135 }
111136
112137
113138 inline void Context::putInt (const std::string& key, int value)
114139 {
115- _map[key] = ctxVal (false , value, " " );
140+ #if __cplusplus >= 201703L
141+ _map[key] = int64 (value);
142+ #else
143+ _map[key] = ContextVal ((int64)value);
144+ #endif
116145 }
117146
118147
119148 inline void Context::putLong (const std::string& key, int64 value)
120149 {
121- _map[key] = ctxVal (false , value, " " );
150+ #if __cplusplus >= 201703L
151+ _map[key] = value;
152+ #else
153+ _map[key] = ContextVal (value);
154+ #endif
122155 }
123156
124-
125157 inline void Context::putString (const std::string& key, const std::string& value)
126158 {
127- _map[key] = ctxVal (true , 0 , value);
159+ #if __cplusplus >= 201703L
160+ _map[key] = value;
161+ #else
162+ _map[key] = ContextVal (value);
163+ #endif
128164 }
129165
130166}
0 commit comments