clingo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
value_store.h
Go to the documentation of this file.
1 //
2 // Copyright (c) Benjamin Kaufmann 2010
3 //
4 // This is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This file is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this file; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 //
18 //
19 // NOTE: ProgramOptions is inspired by Boost.Program_options
20 // see: www.boost.org/libs/program_options
21 //
22 #ifndef PROGRAM_OPTIONS_VALUE_STORE_H_INCLUDED
23 #define PROGRAM_OPTIONS_VALUE_STORE_H_INCLUDED
24 #include <typeinfo>
25 #include <new>
26 namespace ProgramOptions {
27 namespace detail {
28 typedef void (*vcall_type)(const void* in, void** out);
31 // workaround: some compilers don't support
32 // out-of-class definition of member templates.
33 template <class T>
34 inline vptr_type vtable(const T* = 0);
35 template <class T>
36 inline vptr_type base_vtable(const T* = 0);
37 }
38 
40 class ValueStore {
41 public:
43  ValueStore();
45  ValueStore(const ValueStore& other);
47  template <class T>
48  ValueStore(const T& obj)
49  : vptr_(detail::vtable(static_cast<const T*>(0)))
50  , value_(0) {
51  clone(&obj, &value_);
52  }
54  ~ValueStore();
58  template <class T>
59  ValueStore& operator=(const T& obj) {
60  ValueStore(obj).swap(*this);
61  return *this;
62  }
64 
67  template <class T>
69  clear();
70  vptr_ = detail::base_vtable(static_cast<const T*>(0));
71  value_= obj;
72  return *this;
73  }
75  void swap(ValueStore& other);
77  bool empty() const { return vptr_ == 0; }
79  const std::type_info&
80  type() const;
82  void clear();
84  void surrender();
85 
86  void* extract_raw() const {
87  return !empty()
88  ? extract(const_cast<void**>(&value_))
89  : 0;
90  }
91 private:
92  enum { call_extract = 0, vcall_clone = 1, vcall_destroy = 2, vcall_typeid = 3 };
93  typedef detail::vptr_type vptr_type;
94  void clone(const void* obj, void** out) const;
95  void* extract(void** o) const;
96  vptr_type vptr_;
97  void* value_;
98 };
99 
100 struct bad_value_cast : std::bad_cast {
101  const char * what() const throw() {
102  return "value_cast: invalid conversion on ValueStore";
103  }
104 };
105 
107 
110 template <class T>
111 const T& value_cast(const ValueStore& v, const T* = 0) {
112  if (v.type() == typeid(T)) {
113  return *static_cast<const T*>(const_cast<const void*>(v.extract_raw()));
114  }
115  throw bad_value_cast();
116 }
118 template <class T>
119 const T* value_cast(const ValueStore* v, const T* = 0) {
120  if (v->type() == typeid(T)) {
121  return static_cast<const T*>(const_cast<const void*>(v->extract_raw()));
122  }
123  return 0;
124 }
128 template <class T>
129 T& value_cast(ValueStore& v, const T* p = 0) {
130  return const_cast<T&>(value_cast(const_cast<const ValueStore&>(v), p));
131 }
132 template <class T>
133 T* value_cast(ValueStore* v, const T* p = 0) {
134  return const_cast<T*>(value_cast(const_cast<const ValueStore*>(v), p));
135 }
136 
138 template <class T>
139 const T* unsafe_value_cast(const ValueStore* v, const T* = 0) {
140  return static_cast<const T*>(const_cast<const void*>(v->extract_raw()));
141 }
145 template <class T>
146 T* unsafe_value_cast(ValueStore* v, const T* p = 0) {
147  return const_cast<T*>(unsafe_value_cast(const_cast<const ValueStore*>(v), p));
148 }
149 
150 #include "detail/value_store.h"
151 
152 }
153 #endif
const T & value_cast(const ValueStore &v, const T *=0)
Extracts a typed value from a ValueStore.
Definition: value_store.h:111
const char * what() const
Definition: value_store.h:101
ValueStore()
creates an empty object
Definition: value_store.cpp:26
vptr_type vtable(const T *=0)
Definition: value_store.h:78
vptr_type base_vtable(const T *=0)
Definition: value_store.h:83
bool empty() const
Returns true if holder does not contain a value.
Definition: value_store.h:77
void(* vcall_type)(const void *in, void **out)
Definition: value_store.h:28
const std::type_info & type() const
Returns the type of the stored value.
Definition: value_store.cpp:49
void * extract_raw() const
Definition: value_store.h:86
A type that can hold any kind of value type.
Definition: value_store.h:40
Definition: value_store.h:100
void swap(ValueStore &other)
swaps this with other
Definition: value_store.cpp:44
~ValueStore()
releases any stored value
Definition: value_store.cpp:37
ValueStore & assimilate(T *obj)
stores obj in this and takes over ownership of obj
Definition: value_store.h:68
void clear()
destroys and releases any stored value
Definition: value_store.cpp:59
const T * unsafe_value_cast(const ValueStore *v, const T *=0)
Extracts a typed value from a ValueStore without checking if the type matches.
Definition: value_store.h:139
void surrender()
surrenders any stored value without destroying it
Definition: value_store.cpp:66
tuple p
Definition: server.py:49
ValueStore(const T &obj)
stores a copy of obj
Definition: value_store.h:48
ValueStore & operator=(ValueStore other)
stores a copy of other releasing any previous value
Definition: value_store.cpp:40
vtable_type * vptr_type
Definition: value_store.h:30
ValueStore & operator=(const T &obj)
stores a copy of obj releasing any previous value
Definition: value_store.h:59
LparseOutputter & out
Definition: output.cc:685
vcall_type vtable_type[4]
Definition: value_store.h:29