Line data Source code
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
42 : #include "bindings/lua/lua_function_handle.h"
43 : #include "bindings/lua/lua_table_handle.h"
44 : #endif
45 :
46 : namespace ug{
47 :
48 : /// \addtogroup ugbase_common_types
49 : /// \{
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 :
73 : /// A variant can represent variables of different types.
74 : /** A variant will represent the value, which was assigned during construction
75 : * or in an assignment operation.
76 : * Use the different to_.. methods, to retrieve the encapsulated value. Take
77 : * care only to retrieve values, which are compatible with the encapsulated value.
78 : *
79 : * Number and boolean types are automatically converted to each other:
80 : * VT_BOOL, VT_INT, VT_FLOAT, VT_DOUBLE.
81 : *
82 : * A stdstring is automatically converted to a cstr, but not vice-versa.
83 : *
84 : * You may inspect the type represented by a variant by calling the type() method,
85 : * which will return a constant enumerated in Variant::Type.
86 : */
87 : class UG_API Variant{
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
122 : Variant(LuaFunctionHandle val);
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 0 : 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:
159 : /// this method only performs assignment, no prior clean up
160 : void assign_variant(const Variant& v);
161 :
162 : /// returns the name of the current type
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;
176 : SmartPtr<void>* m_smartptr;
177 : ConstSmartPtr<void>* m_constsmartptr;
178 : #ifdef UG_FOR_LUA
179 : LuaFunctionHandle m_luafcthandle;
180 : LuaTableHandle m_luatblhandle;
181 : #endif
182 : };
183 :
184 : Type m_type;
185 : };
186 :
187 0 : template <> inline bool Variant::to<bool>() const {return to_bool();}
188 0 : template <> inline int Variant::to<int>() const {return to_int();}
189 0 : template <> inline size_t Variant::to<size_t>() const {return to_size_t();}
190 0 : template <> inline float Variant::to<float>() const {return to_float();}
191 0 : template <> inline double Variant::to<double>() const {return to_double();}
192 0 : template <> inline const char* Variant::to<const char*>() const {return to_c_string();}
193 0 : template <> inline std::string Variant::to<std::string>() const {return to_std_string();}
194 0 : template <> inline const std::string& Variant::to<const std::string&>() const {return to_std_string();}
195 0 : template <> inline void* Variant::to<void*>() const {return to_pointer();}
196 0 : template <> inline const void* Variant::to<const void*>() const {return to_const_pointer();}
197 0 : template <> inline SmartPtr<void> Variant::to<SmartPtr<void> >() const {return to_smart_pointer();}
198 0 : template <> inline ConstSmartPtr<void> Variant::to<ConstSmartPtr<void> >() const {return to_const_smart_pointer();}
199 : #ifdef UG_FOR_LUA
200 0 : 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
223 : /// \}
224 :
225 : }// end of namespace
226 :
227 : UG_API std::ostream& operator<< (std::ostream& outStream, const ug::Variant& v);
228 :
229 : #endif
|