WARPXM v1.10.0
Loading...
Searching...
No Matches
wxcrypt.h
Go to the documentation of this file.
1#ifndef wxcrypt_h
2#define wxcrypt_h
3
4// WarpX include
5#include "warpxm/warpxm_config.h"
6#include "wxany.h"
7#include "wxdatatypes.h"
8#include "wxexcept.h"
9
10// std includes
11#include <map>
12#include <stdexcept>
13#include <string>
14#include <vector>
15
33{
34public:
35 // types of string->WxAny map and pairs
36 typedef std::map<std::string, WxAny, std::less<std::string>> AnyMap_t;
37 typedef std::pair<std::string, WxAny> AnyPair_t;
38
39 // these are the supported integer and floating point types since reading from a
40 // string of digits does not explicitly define the precision
41 typedef int crypt_signed_int_t;
42
44 virtual ~WxCrypt();
45
49 WxCrypt(const WxCrypt& crypt);
50
55
63 template<typename VALUETYPE> bool add(const std::string& name, const VALUETYPE& value)
64 {
65 return _values.insert(AnyPair_t(name, value)).second;
66 }
67
73 bool has(const std::string& name) const;
74
80 template<typename VALUETYPE> VALUETYPE get(const std::string& name) const
81 {
82 AnyMap_t::const_iterator i = _values.find(name);
83 if (i != _values.end())
84 return ((*i).second).to_value<VALUETYPE>();
85 // value not found: throw an exception
86 WxExcept wxe;
87 wxe << "Value " << name << " not found";
88 throw wxe;
89 }
90
97 template<typename VALUETYPE>
98 VALUETYPE getWithDefault(const std::string& name, const VALUETYPE defaultValue) const
99 {
100 if (has(name))
101 {
102 return get<VALUETYPE>(name);
103 }
104 else
105 {
106 return defaultValue;
107 }
108 }
109
114 const WxAny& get(const std::string& name) const
115 {
116 AnyMap_t::const_iterator i = _values.find(name);
117 if (i != _values.end())
118 return ((*i).second);
119 // value not found: throw an exception
120 WxExcept wxe;
121 wxe << "Value " << name << " not found";
122 throw wxe;
123 }
124
128 WxAny& get(const std::string& name)
129 {
130 return const_cast<WxAny&>(const_cast<const WxCrypt&>(*this).get(name));
131 }
132
137 void set(const std::string& name, const WxAny& val)
138 {
139 _values[name] = val;
140 }
141
146 void set(const std::string& name, WxAny&& val)
147 {
148 _values[name] = std::move(val);
149 }
150
151 template<class T> void set(const std::string& name, T val)
152 {
153 _values[name] = std::move(val);
154 }
155
161 template<typename VALUETYPE>
162 std::vector<VALUETYPE> getVec(const std::string& name) const
163 {
164 return get(name).to_value<std::vector<VALUETYPE>>();
165 }
166
167private:
168 AnyMap_t _values;
169};
170
172#endif // wxcrypt_h
Class WxAny is based on the "any" class described in "Valued Conversion", Kevlin Henney,...
Definition: wxany.h:139
WxCrypt provides a container to store/retrive name-value pairs.
Definition: wxcrypt.h:33
const WxAny & get(const std::string &name) const
Gets the raw WxAny value associated with name.
Definition: wxcrypt.h:114
std::pair< std::string, WxAny > AnyPair_t
Definition: wxcrypt.h:37
void set(const std::string &name, const WxAny &val)
Ensures a value with name is this wxcrypt, either by adding it or changing the existing map.
Definition: wxcrypt.h:137
bool has(const std::string &name) const
Returns true if the 'name' exists in the crypt.
VALUETYPE getWithDefault(const std::string &name, const VALUETYPE defaultValue) const
Retrieve the value associated with name, or the given default value if no such key is in the cryptset...
Definition: wxcrypt.h:98
int crypt_signed_int_t
Definition: wxcrypt.h:41
WxAny & get(const std::string &name)
Gets the raw WxAny value associated with name.
Definition: wxcrypt.h:128
WxCrypt(const WxCrypt &crypt)
Copy ctor: make a deep copy of 'crypt'.
bool add(const std::string &name, const VALUETYPE &value)
Insert a name, value pair.
Definition: wxcrypt.h:63
void set(const std::string &name, T val)
Definition: wxcrypt.h:151
virtual ~WxCrypt()
std::vector< VALUETYPE > getVec(const std::string &name) const
Retrieve list of values associated with name.
Definition: wxcrypt.h:162
WxCrypt & operator=(const WxCrypt &rhs)
Assignment operator: LHS is a deep copy of 'rhs'.
std::map< std::string, WxAny, std::less< std::string > > AnyMap_t
Definition: wxcrypt.h:36
VALUETYPE get(const std::string &name) const
Retrieve the value associated with name.
Definition: wxcrypt.h:80
void set(const std::string &name, WxAny &&val)
Ensures a value with name is this wxcrypt, either by adding it or changing the existing map.
Definition: wxcrypt.h:146
wxm::lib::Except is the class to use for creating and throwing exceptions.
Definition: wxexcept.h:31