WARPXM v1.10.0
Loading...
Searching...
No Matches
wxany.h
Go to the documentation of this file.
1#ifndef wxany_h
2#define wxany_h
3
4// WarpX includes
5#include "wxexcept.h"
6
7// std includes
8#include <algorithm>
9#include <cstddef>
10#include <stdint.h>
11#include <string>
12#include <typeinfo>
13#include <vector>
14
15class WxAny;
16
17namespace wxm
18{
19namespace detail
20{
26template<class T> struct wxany_arithmetic_caster
27{
28 T as_value(const WxAny& v) const;
29};
30
37{
38 std::string as_value(const WxAny& v) const;
39};
40
45template<class T> struct wxany_avector_caster
46{
47 std::vector<T> as_value(const WxAny& v) const;
48};
49
55{
56 std::vector<std::string> as_value(const WxAny& v) const;
57};
58
62template<class T> struct wxany_caster
63{
64 T as_value(const WxAny& v) const;
65};
66
67template<> struct wxany_caster<std::string> : public wxany_string_caster
68{
69};
70
71template<> struct wxany_caster<std::vector<std::string>> : public wxany_svector_caster
72{
73};
74
75#define WXCASTER_HELPER(TYPE) \
76 template<> struct wxany_caster<TYPE> : public wxany_arithmetic_caster<TYPE> \
77 { \
78 }
79
82WXCASTER_HELPER(unsigned char);
84WXCASTER_HELPER(unsigned short);
86WXCASTER_HELPER(unsigned int);
88WXCASTER_HELPER(unsigned long);
89WXCASTER_HELPER(long long);
90WXCASTER_HELPER(unsigned long long);
93
94#undef WXCASTER_HELPER
95
96#define WXVECCASTER_HELPER(TYPE) \
97 template<> \
98 struct wxany_caster<std::vector<TYPE>> : public wxany_avector_caster<TYPE> \
99 { \
100 }
101
104WXVECCASTER_HELPER(unsigned char);
106WXVECCASTER_HELPER(unsigned short);
108WXVECCASTER_HELPER(unsigned int);
110WXVECCASTER_HELPER(unsigned long);
112WXVECCASTER_HELPER(unsigned long long);
115
116#undef WXVECCASTER_HELPER
117} // namespace detail
118} // namespace wxm
119
138class WxAny
139{
140 template<class T>
142 {
143 return t.as_value(*this);
144 }
145
146public:
150 WxAny() : content(nullptr)
151 {
152 }
153
159 template<typename VALUETYPE>
160 WxAny(const VALUETYPE& value) : content(new _WxHolder<VALUETYPE>(value))
161 {
162 }
163
167 WxAny(const WxAny& other) : content(other.content ? other.content->clone() : nullptr)
168 {
169 }
170
174 WxAny(WxAny&& other) noexcept : content(other.content)
175 {
176 other.content = nullptr;
177 }
178
180 {
181 delete content;
182 }
183
190 {
191 std::swap(content, rhs.content);
192 return *this;
193 }
194
200 WxAny& operator=(const WxAny& rhs)
201 {
202 WxAny(rhs).swap(*this);
203 return *this;
204 }
205
206 WxAny& operator=(WxAny&& rhs) noexcept
207 {
208 if (&rhs != this)
209 {
210 swap(rhs);
211 }
212 return *this;
213 }
214
218 template<typename VALUETYPE> WxAny& operator=(const VALUETYPE& rhs)
219 {
220 WxAny(rhs).swap(*this);
221 return *this;
222 }
223
227 bool empty() const
228 {
229 return !content;
230 }
231
235 const std::type_info& type() const
236 {
237 return content ? content->type() : typeid(void);
238 }
239
246 {
247 return content ? content->sizeInBytes() : -1;
248 }
249
254 template<typename VALUETYPE> const VALUETYPE* to_ptr() const
255 {
256 // return pointer to held object or NULL if wrong type
257 return type() == typeid(VALUETYPE)
258 ? &static_cast<_WxHolder<VALUETYPE>*>(content)->held
259 : 0;
260 }
261
266 const void* to_void_ptr() const
267 {
268 return content->void_ptr();
269 }
270
276 {
277 return content->void_ptr();
278 }
279
287 template<typename VALUETYPE> VALUETYPE to_value() const
288 {
289 return as_value_impl(wxm::detail::wxany_caster<VALUETYPE>());
290 }
291
292private:
293 class _WxPlaceHolder
294 {
295 public:
296 // dtor
297 virtual ~_WxPlaceHolder() = default;
298
299 // typeinfo: must be provided by children
300 virtual const std::type_info& type() const = 0;
301
302 // sizeInBytes: must be provided by children
303 virtual int sizeInBytes() const = 0;
304
305 // make a copy: must be provided by children
306 virtual _WxPlaceHolder* clone() const = 0;
307
308 // return object as a const void *
309 virtual const void* void_ptr() const = 0;
310
311 // return object as a void *
312 virtual void* void_ptr() = 0;
313 };
314
315 template<typename VALUETYPE> class _WxHolder : public _WxPlaceHolder
316 {
317 public:
318 _WxHolder(const VALUETYPE& value) : held(value)
319 {
320 }
321
322 // type_info about VALUETYPE
323 virtual const std::type_info& type() const
324 {
325 return typeid(VALUETYPE);
326 }
327
328 // size in bytes of stored value
329 virtual int sizeInBytes() const
330 {
331 return sizeof(VALUETYPE);
332 }
333
334 // make a copy of held object
335 virtual _WxPlaceHolder* clone() const
336 {
337 return new _WxHolder(held);
338 }
339
340 // return object as a const void *
341 virtual const void* void_ptr() const
342 {
343 return reinterpret_cast<const void*>(&held);
344 }
345
346 // return object as a void *
347 virtual void* void_ptr()
348 {
349 return reinterpret_cast<void*>(&held);
350 }
351
352 VALUETYPE held; // held object
353 };
354
355 _WxPlaceHolder* content;
356};
357
365template<typename VALUETYPE>
367{
368 // return object or throw exception if types do not match
369 const VALUETYPE* result = operand.template to_ptr<VALUETYPE>();
370
371 // if it is not null, return value else thow exception
372 if (result)
373 return *result;
374 else
375 {
376 WxExcept wxe("wx_access_payload_with_type_check : ");
377 wxe << "Attempted to assign '" << operand.type().name() << "' as '"
378 << typeid(VALUETYPE).name() << "'" << std::endl;
379 throw wxe;
380 }
381 // throw std::bad_cast();
382}
383
391template<typename VALUETYPE> VALUETYPE wx_access_payload_with_type_check(WxAny& operand)
392{
393 // return object or throw exception if types do not match
394 const VALUETYPE* result = operand.template to_ptr<VALUETYPE>();
395
396 // if it is not null, return value else thow exception
397 if (result)
398 return *result;
399 else
400 throw std::bad_cast();
401}
402
405#include "detail/wxany.tcc"
406
407#endif // wxany_h
Class WxAny is based on the "any" class described in "Valued Conversion", Kevlin Henney,...
Definition: wxany.h:139
~WxAny()
Definition: wxany.h:179
int payloadSizeInBytes() const
Size in bytes for this object's held value.
Definition: wxany.h:245
WxAny(const WxAny &other)
Copy ctor.
Definition: wxany.h:167
WxAny & operator=(const WxAny &rhs)
Assignment operator: use WxAny object to create a new object.
Definition: wxany.h:200
WxAny()
Create empty object.
Definition: wxany.h:150
WxAny(WxAny &&other) noexcept
move ctor
Definition: wxany.h:174
bool empty() const
Is object empty?
Definition: wxany.h:227
WxAny & operator=(const VALUETYPE &rhs)
Assignment operator: use VALUETYPE object to create a new object.
Definition: wxany.h:218
const std::type_info & type() const
Type_info for this object's held value.
Definition: wxany.h:235
void * to_void_ptr()
Convert held object to a void *.
Definition: wxany.h:275
const VALUETYPE * to_ptr() const
Convert the held object to a pointer.
Definition: wxany.h:254
const void * to_void_ptr() const
Convert held object to a const void *.
Definition: wxany.h:266
WxAny & swap(WxAny &rhs)
Swap contents of object with contents of supplied object.
Definition: wxany.h:189
WxAny & operator=(WxAny &&rhs) noexcept
Definition: wxany.h:206
WxAny(const VALUETYPE &value)
Create new WxAny from an object of a given type.
Definition: wxany.h:160
VALUETYPE to_value() const
Extract the data from the WxAny object.
Definition: wxany.h:287
wxm::lib::Except is the class to use for creating and throwing exceptions.
Definition: wxexcept.h:31
VALUETYPE wx_access_payload_with_type_check(const WxAny &operand)
Extract the data from the WxAny object.
Definition: wxany.h:366
value
Definition: bloch_periodic_1D.py:182
type
Definition: sheath.py:84
Base namespace for everything not included in the global namespace.
Definition: field_source.h:8
Helper for casting any arithmetic type to any other arithmetic type.
Definition: wxany.h:27
T as_value(const WxAny &v) const
Helper for casting to a vector of arithmetic types from other vectors of arithmetic types or from a s...
Definition: wxany.h:46
std::vector< T > as_value(const WxAny &v) const
Helper for casting between various types.
Definition: wxany.h:63
T as_value(const WxAny &v) const
Helper for casting to a string type.
Definition: wxany.h:37
std::string as_value(const WxAny &v) const
Helper for casting to a vector of strings.
Definition: wxany.h:55
std::vector< std::string > as_value(const WxAny &v) const
#define WXCASTER_HELPER(TYPE)
Definition: wxany.h:75
#define WXVECCASTER_HELPER(TYPE)
Definition: wxany.h:96