| 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_H_INCLUDED |
| 27 |
#define PROGRAM_OPTIONS_VALUE_STORE_H_INCLUDED |
| 28 |
#include <typeinfo> |
| 29 |
#include <new> |
| 30 |
namespace Potassco { namespace ProgramOptions { namespace detail { |
| 31 |
typedef void (*vcall_type)(const void* in, void** out); |
| 32 |
typedef vcall_type vtable_type[4]; |
| 33 |
typedef vtable_type* vptr_type; |
| 34 |
// workaround: some compilers don't support |
| 35 |
// out-of-class definition of member templates. |
| 36 |
template <class T> |
| 37 |
inline vptr_type vtable(const T* = 0); |
| 38 |
template <class T> |
| 39 |
inline vptr_type base_vtable(const T* = 0); |
| 40 |
} // namespace detail |
| 41 |
|
| 42 |
//! A type that can hold any kind of value type |
| 43 |
class ValueStore { |
| 44 |
public: |
| 45 |
//! creates an empty object |
| 46 |
ValueStore(); |
| 47 |
//! copies the value in other |
| 48 |
ValueStore(const ValueStore& other); |
| 49 |
//! stores a copy of obj |
| 50 |
template <class T> |
| 51 |
ValueStore(const T& obj) |
| 52 |
: vptr_(detail::vtable(static_cast<const T*>(0))) |
| 53 |
, value_(0) { |
| 54 |
clone(&obj, &value_); |
| 55 |
} |
| 56 |
//! releases any stored value |
| 57 |
~ValueStore(); |
| 58 |
//! stores a copy of other releasing any previous value |
| 59 |
ValueStore& operator=(ValueStore other); |
| 60 |
//! stores a copy of obj releasing any previous value |
| 61 |
template <class T> |
| 62 |
ValueStore& operator=(const T& obj) { |
| 63 |
ValueStore(obj).swap(*this); |
| 64 |
return *this; |
| 65 |
} |
| 66 |
//! stores obj in this and takes over ownership of obj |
| 67 |
/*! |
| 68 |
* \pre delete obj is valid |
| 69 |
*/ |
| 70 |
template <class T> |
| 71 |
ValueStore& assimilate(T* obj) { |
| 72 |
clear(); |
| 73 |
vptr_ = detail::base_vtable(static_cast<const T*>(0)); |
| 74 |
value_= obj; |
| 75 |
return *this; |
| 76 |
} |
| 77 |
//! swaps this with other |
| 78 |
void swap(ValueStore& other); |
| 79 |
//! Returns true if holder does not contain a value. |
| 80 |
bool empty() const { return vptr_ == 0; } |
| 81 |
//! Returns the type of the stored value. |
| 82 |
const std::type_info& |
| 83 |
type() const; |
| 84 |
//! destroys and releases any stored value |
| 85 |
void clear(); |
| 86 |
//! surrenders any stored value without destroying it |
| 87 |
void surrender(); |
| 88 |
|
| 89 |
void* extract_raw() const { |
| 90 |
return !empty() |
| 91 |
? extract(const_cast<void**>(&value_)) |
| 92 |
: 0; |
| 93 |
} |
| 94 |
private: |
| 95 |
enum { call_extract = 0, vcall_clone = 1, vcall_destroy = 2, vcall_typeid = 3 }; |
| 96 |
typedef detail::vptr_type vptr_type; |
| 97 |
void clone(const void* obj, void** out) const; |
| 98 |
void* extract(void** o) const; |
| 99 |
vptr_type vptr_; |
| 100 |
void* value_; |
| 101 |
}; |
| 102 |
|
| 103 |
struct bad_value_cast : std::bad_cast { |
| 104 |
const char * what() const throw() { |
| 105 |
return "value_cast: invalid conversion on ValueStore"; |
| 106 |
} |
| 107 |
}; |
| 108 |
|
| 109 |
//! Extracts a typed value from a ValueStore. |
| 110 |
/*! |
| 111 |
* \throw bad_value_cast if value is not of type T |
| 112 |
*/ |
| 113 |
template <class T> |
| 114 |
const T& value_cast(const ValueStore& v, const T* = 0) { |
| 115 |
if (v.type() == typeid(T)) { |
| 116 |
return *static_cast<const T*>(const_cast<const void*>(v.extract_raw())); |
| 117 |
} |
| 118 |
throw bad_value_cast(); |
| 119 |
} |
| 120 |
//! Returns 0 on error |
| 121 |
template <class T> |
| 122 |
const T* value_cast(const ValueStore* v, const T* = 0) { |
| 123 |
if (v->type() == typeid(T)) { |
| 124 |
return static_cast<const T*>(const_cast<const void*>(v->extract_raw())); |
| 125 |
} |
| 126 |
return 0; |
| 127 |
} |
| 128 |
/*! |
| 129 |
* \overload |
| 130 |
*/ |
| 131 |
template <class T> |
| 132 |
T& value_cast(ValueStore& v, const T* p = 0) { |
| 133 |
return const_cast<T&>(value_cast(const_cast<const ValueStore&>(v), p)); |
| 134 |
} |
| 135 |
template <class T> |
| 136 |
T* value_cast(ValueStore* v, const T* p = 0) { |
| 137 |
return const_cast<T*>(value_cast(const_cast<const ValueStore*>(v), p)); |
| 138 |
} |
| 139 |
|
| 140 |
//! Extracts a typed value from a ValueStore *without* checking if the type matches |
| 141 |
template <class T> |
| 142 |
const T* unsafe_value_cast(const ValueStore* v, const T* = 0) { |
| 143 |
return static_cast<const T*>(const_cast<const void*>(v->extract_raw())); |
| 144 |
} |
| 145 |
/*! |
| 146 |
* \overload |
| 147 |
*/ |
| 148 |
template <class T> |
| 149 |
T* unsafe_value_cast(ValueStore* v, const T* p = 0) { |
| 150 |
return const_cast<T*>(unsafe_value_cast(const_cast<const ValueStore*>(v), p)); |
| 151 |
} |
| 152 |
|
| 153 |
#include "detail/value_store.h" |
| 154 |
|
| 155 |
} // namespace ProgramOptions |
| 156 |
} // namespace Potassco |
| 157 |
#endif |