| 1 |
// |
| 2 |
// Copyright (c) 2010-2017 Benjamin Kaufmann |
| 3 |
// |
| 4 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
// of this software and associated documentation files (the "Software"), to |
| 6 |
// deal in the Software without restriction, including without limitation the |
| 7 |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 |
// sell copies of the Software, and to permit persons to whom the Software is |
| 9 |
// furnished to do so, subject to the following conditions: |
| 10 |
// |
| 11 |
// The above copyright notice and this permission notice shall be included in |
| 12 |
// all copies or substantial portions of the Software. |
| 13 |
// |
| 14 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 |
// IN THE SOFTWARE. |
| 21 |
// |
| 22 |
// |
| 23 |
// NOTE: ProgramOptions is inspired by Boost.Program_options |
| 24 |
// see: www.boost.org/libs/program_options |
| 25 |
// |
| 26 |
#ifndef PROGRAM_OPTIONS_VALUE_STORE_IMPL_H_INCLUDED |
| 27 |
#define PROGRAM_OPTIONS_VALUE_STORE_IMPL_H_INCLUDED |
| 28 |
|
| 29 |
namespace detail { |
| 30 |
template <class T> |
| 31 |
struct VTable { |
| 32 |
static void clone(const void* o, void** out) { |
| 33 |
*out = new T(*static_cast<const T*>(o)); |
| 34 |
} |
| 35 |
static void destroy(const void* o, void** out) { |
| 36 |
delete static_cast<const T*>(o); |
| 37 |
*out = 0; |
| 38 |
} |
| 39 |
static void typeinfo(const void*, void** out) { |
| 40 |
*out = const_cast<void*>( static_cast<const void*>(&typeid(T)) ); |
| 41 |
} |
| 42 |
static vtable_type vtable_s; |
| 43 |
}; |
| 44 |
template <class T> |
| 45 |
struct OptVTable { |
| 46 |
static void clone(const void* o, void** out) { |
| 47 |
new (&*out) T(*static_cast<const T*>(o)); |
| 48 |
} |
| 49 |
static void destroy(const void* o, void** out) { |
| 50 |
static_cast<const T*>(o)->~T(); |
| 51 |
*out = 0; |
| 52 |
} |
| 53 |
static vtable_type vtable_s; |
| 54 |
}; |
| 55 |
template <class T> |
| 56 |
vtable_type VTable<T>::vtable_s = { |
| 57 |
0 |
| 58 |
, &VTable<T>::clone |
| 59 |
, &VTable<T>::destroy |
| 60 |
, &VTable<T>::typeinfo |
| 61 |
}; |
| 62 |
template <class T> |
| 63 |
vtable_type OptVTable<T>::vtable_s = { |
| 64 |
(vcall_type)0x1 |
| 65 |
, &OptVTable<T>::clone |
| 66 |
, &OptVTable<T>::destroy |
| 67 |
, &VTable<T>::typeinfo |
| 68 |
}; |
| 69 |
template <bool> struct bool2type {}; |
| 70 |
template <class T> |
| 71 |
inline vptr_type vtable_select(bool2type<0>, const T* = 0) { |
| 72 |
return &VTable<T>::vtable_s; |
| 73 |
} |
| 74 |
template <class T> |
| 75 |
inline vptr_type vtable_select(bool2type<1>, const T* = 0) { |
| 76 |
return &OptVTable<T>::vtable_s; |
| 77 |
} |
| 78 |
template <class T> |
| 79 |
inline vptr_type vtable(const T* x) { |
| 80 |
return vtable_select(bool2type<sizeof(T)<=sizeof(void*)>(), x); |
| 81 |
} |
| 82 |
|
| 83 |
template <class T> |
| 84 |
inline vptr_type base_vtable(const T* x) { |
| 85 |
return vtable_select(bool2type<0>(), x); |
| 86 |
} |
| 87 |
|
| 88 |
} // namespace detail |
| 89 |
|
| 90 |
#endif |