ug4
variant.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2015: G-CSC, Goethe University Frankfurt
3  * Author: Sebastian Reiter
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__UG_variant__
34 #define __H__UG_variant__
35 
36 #include <iostream>
37 #include <string>
38 #include "smart_pointer.h"
39 #include "common/types.h"
40 #include "common/ug_config.h"
41 #ifdef UG_FOR_LUA
44 #endif
45 
46 namespace ug{
47 
50 
51 // todo: Change numbers in VRL
52 // Type values must not be changed! Bindings rely on the exact values.
53 // Append new types at the end and update bindings.
54 // If in doubt contact binding developers!
55 // BUT NOW HAVE BEEN CHANGED (A.Vogel)
56 // HERE ARE THE OLD VALUES, BELOW THE NEW
57 /*enum ParameterTypes
58 {
59  PT_UNKNOWN = 0,
60  PT_BOOL = 1,
61  PT_INTEGER = 2,
62  PT_NUMBER = 3,
63  PT_CSTRING = 4,
64  PT_STD_STRING = 5,
65  PT_POINTER = 6,
66  PT_CONST_POINTER = 7,
67  PT_SMART_POINTER = 8,
68  PT_CONST_SMART_POINTER = 9
69 };
70 */
71 
72 
74 
88  public: // TODO?: hide enum
89  enum Type{
90  VT_INVALID = 0,
91  VT_BOOL = 1,
92  VT_INT = 2,
93  VT_SIZE_T = 3,
94  VT_FLOAT = 4,
95  VT_DOUBLE = 5,
96  VT_CSTRING = 6,
97  VT_STDSTRING = 7,
98  VT_POINTER = 8,
99  VT_CONST_POINTER = 9,
100  VT_SMART_POINTER = 10,
101  VT_CONST_SMART_POINTER = 11
102 #ifdef UG_FOR_LUA
103  ,VT_LUA_FUNCTION_HANDLE = 12
104  ,VT_LUA_TABLE_HANDLE = 13
105 #endif
106  };
107 
108  public:
109  Variant();
110  Variant(bool val);
111  Variant(int val);
112  Variant(size_t val);
113  Variant(float val);
114  Variant(double val);
115  Variant(const char* val);
116  Variant(const std::string& val);
117  Variant(void* val);
118  Variant(const void* val);
119  Variant(const SmartPtr<void>& val);
120  Variant(const ConstSmartPtr<void>& val);
121 #ifdef UG_FOR_LUA
123  Variant(LuaTableHandle val);
124 #endif
125 
126  Variant(const Variant& v);
127 
128  ~Variant();
129 
130  const Variant& operator=(const Variant& v);
131 
132  inline Type type() const {return m_type;}
133 
134  template <typename T>
135  inline static Type type() {return VT_INVALID;}
136 
137  bool is_valid() const{return m_type;}
138  bool to_bool() const;
139  int to_int() const;
140  size_t to_size_t() const;
141  float to_float() const;
142  number to_number() const;
143  double to_double() const;
144  const char* to_c_string() const;
145  const std::string& to_std_string() const;
146  void* to_pointer() const;
147  const void* to_const_pointer() const;
148  SmartPtr<void> to_smart_pointer() const;
149  ConstSmartPtr<void> to_const_smart_pointer() const;
150 #ifdef UG_FOR_LUA
151  LuaFunctionHandle to_lua_function_handle() const;
152  LuaTableHandle to_lua_table_handle() const;
153 #endif
154 
155  template <typename T>
156  inline T to() const;
157 
158  private:
160  void assign_variant(const Variant& v);
161 
163  const char* type_name() const;
164 
165  private:
166  union{
167  bool m_bool;
168  int m_int;
169  size_t m_size_t;
170  float m_float;
171  double m_double;
172  const char* m_cstring;
173  std::string* m_stdstring;
174  void* m_pointer;
175  const void* m_constptr;
178 #ifdef UG_FOR_LUA
179  LuaFunctionHandle m_luafcthandle;
180  LuaTableHandle m_luatblhandle;
181 #endif
182  };
183 
185 };
186 
187 template <> inline bool Variant::to<bool>() const {return to_bool();}
188 template <> inline int Variant::to<int>() const {return to_int();}
189 template <> inline size_t Variant::to<size_t>() const {return to_size_t();}
190 template <> inline float Variant::to<float>() const {return to_float();}
191 template <> inline double Variant::to<double>() const {return to_double();}
192 template <> inline const char* Variant::to<const char*>() const {return to_c_string();}
193 template <> inline std::string Variant::to<std::string>() const {return to_std_string();}
194 template <> inline const std::string& Variant::to<const std::string&>() const {return to_std_string();}
195 template <> inline void* Variant::to<void*>() const {return to_pointer();}
196 template <> inline const void* Variant::to<const void*>() const {return to_const_pointer();}
197 template <> inline SmartPtr<void> Variant::to<SmartPtr<void> >() const {return to_smart_pointer();}
198 template <> inline ConstSmartPtr<void> Variant::to<ConstSmartPtr<void> >() const {return to_const_smart_pointer();}
199 #ifdef UG_FOR_LUA
200 template <> inline LuaFunctionHandle Variant::to<LuaFunctionHandle>() const {return to_lua_function_handle();}
201 template <> inline LuaTableHandle Variant::to<LuaTableHandle>() const {return to_lua_table_handle();}
202 #endif
203 
204 template <> inline Variant::Type Variant::type<bool>() {return VT_BOOL;}
205 template <> inline Variant::Type Variant::type<int>() {return VT_INT;}
206 template <> inline Variant::Type Variant::type<size_t>() {return VT_SIZE_T;}
207 template <> inline Variant::Type Variant::type<float>() {return VT_FLOAT;}
208 template <> inline Variant::Type Variant::type<double>() {return VT_DOUBLE;}
209 template <> inline Variant::Type Variant::type<const char*>() {return VT_CSTRING;}
210 template <> inline Variant::Type Variant::type<std::string>() {return VT_STDSTRING;}
211 template <> inline Variant::Type Variant::type<const std::string&>() {return VT_STDSTRING;}
212 template <> inline Variant::Type Variant::type<void*>() {return VT_POINTER;}
213 template <> inline Variant::Type Variant::type<const void*>() {return VT_CONST_POINTER;}
214 template <> inline Variant::Type Variant::type<SmartPtr<void> >() {return VT_SMART_POINTER;}
215 template <> inline Variant::Type Variant::type<ConstSmartPtr<void> >() {return VT_CONST_SMART_POINTER;}
216 #ifdef UG_FOR_LUA
217 template <> inline Variant::Type Variant::type<LuaFunctionHandle>() {return VT_LUA_FUNCTION_HANDLE;}
218 template <> inline Variant::Type Variant::type<LuaTableHandle>() {return VT_LUA_TABLE_HANDLE;}
219 #endif
220 
221 
222 // end group ugbase_common_types
224 
225 }// end of namespace
226 
227 UG_API std::ostream& operator<< (std::ostream& outStream, const ug::Variant& v);
228 
229 #endif
Definition: smart_pointer.h:650
Definition: smart_pointer.h:525
Handle for a lua reference.
Definition: lua_function_handle.h:40
Handle for a lua reference.
Definition: lua_table_handle.h:49
A variant can represent variables of different types.
Definition: variant.h:87
static Type type()
Definition: variant.h:135
ConstSmartPtr< void > * m_constsmartptr
Definition: variant.h:177
size_t to_size_t() const
Definition: variant.cpp:245
bool m_bool
Definition: variant.h:167
double to_double() const
Definition: variant.cpp:291
const char * m_cstring
Definition: variant.h:172
const void * m_constptr
Definition: variant.h:175
std::string * m_stdstring
Definition: variant.h:173
const void * to_const_pointer() const
Definition: variant.cpp:338
float to_float() const
Definition: variant.cpp:259
bool to_bool() const
Definition: variant.cpp:213
double m_double
Definition: variant.h:171
Type
Definition: variant.h:89
@ VT_SIZE_T
Definition: variant.h:93
@ VT_BOOL
Definition: variant.h:91
@ VT_CONST_POINTER
Definition: variant.h:99
@ VT_INT
Definition: variant.h:92
@ VT_CSTRING
Definition: variant.h:96
@ VT_DOUBLE
Definition: variant.h:95
@ VT_FLOAT
Definition: variant.h:94
@ VT_POINTER
Definition: variant.h:98
bool is_valid() const
Definition: variant.h:137
size_t m_size_t
Definition: variant.h:169
int m_int
Definition: variant.h:168
void * to_pointer() const
Definition: variant.cpp:330
SmartPtr< void > * m_smartptr
Definition: variant.h:176
Type type() const
Definition: variant.h:132
const char * to_c_string() const
Definition: variant.cpp:307
float m_float
Definition: variant.h:170
Type m_type
Definition: variant.h:184
T to() const
void * m_pointer
Definition: variant.h:174
int to_int() const
Definition: variant.cpp:229
#define UG_API
Definition: ug_config.h:65
double number
Definition: types.h:124
the ug namespace
UG_API std::ostream & operator<<(std::ostream &outStream, const ug::Variant &v)
Definition: variant.cpp:401