Graphite Version 3
An experimental 3D geometry processing program
Loading...
Searching...
No Matches
meta.h
Go to the documentation of this file.
1/*
2 * GXML/Graphite: Geometry and Graphics Programming Library + Utilities
3 * Copyright (C) 2000 Bruno Levy
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 * If you modify this software, you should include a notice giving the
20 * name of the person performing the modification, the date of modification,
21 * and the reason for such modification.
22 *
23 * Contact: Bruno Levy
24 *
25 * levy@loria.fr
26 *
27 * ISA Project
28 * LORIA, INRIA Lorraine,
29 * Campus Scientifique, BP 239
30 * 54506 VANDOEUVRE LES NANCY CEDEX
31 * FRANCE
32 *
33 * Note that the GNU General Public License does not permit incorporating
34 * the Software into proprietary programs.
35 */
36
37#ifndef H_OGF_GOM_REFLECTION_META_H
38#define H_OGF_GOM_REFLECTION_META_H
39
43
44#include <typeinfo>
45#include <string>
46#include <map>
47#include <vector>
48
49#include <sstream>
50
51//___________________________________________________
52
58namespace OGF {
59
73 class GOM_API Meta {
74 public:
79
84 static Meta* instance() ;
85
94 bool meta_type_is_bound(const std::string& name) const ;
95
105 bool typeid_name_is_bound(const std::string& typeid_name) const ;
106
112 bool bind_meta_type(MetaType* meta_type) ;
113
114
122 void bind_meta_type_alias(MetaType* meta_type, const std::string& alias);
123
132 MetaType* meta_type, const std::string& typeid_name
133 ) ;
134
141 MetaType* resolve_meta_type(const std::string& type_name) const ;
142
149 MetaClass* resolve_meta_class(const std::string& type_name) const {
150 return dynamic_cast<MetaClass*>(resolve_meta_type(type_name));
151 }
152
161 const std::string& typeid_name
162 ) const ;
163
164
171 bool unbind_meta_type(const std::string& name) ;
172
177 void list_types(std::vector<MetaType*>& types) ;
178
183 void list_type_names(std::vector<std::string>& type_names);
184
190 static void initialize() ;
191
197 static void terminate() ;
198
199 private:
204 Meta() ;
205
206 typedef std::map<std::string, MetaType_var> MetaTypesTable ;
207 typedef std::map<std::string, MetaType*> TypeidNamesTable ;
208
209 static Meta* instance_ ;
210 MetaTypesTable type_name_to_meta_type_ ;
211 TypeidNamesTable typeid_name_to_meta_type_ ;
212 } ;
213
214 //___________________________________________________________________
215
216
227 template <class T> inline MetaType* ogf_dynamic_type(const T& x) {
228 ogf_argused(x); // suppresses a warning of MSVC
230 typeid(x).name()
231 ) ;
232 }
233
243 template <class T> inline MetaType* ogf_static_type(const T& x) {
244 ogf_argused(x); // suppresses a warning of MSVC
246 typeid(T).name()
247 ) ;
248 }
249
257 template <class T> inline bool ogf_is_a(const T& x, const MetaType* type) {
258 return ogf_dynamic_type(x)->is_subtype_of(type) ;
259 }
260
261 //___________________________________________________________________
262
267 template <class T> class ogf_meta {
268 public:
269
274 static MetaType* type() {
275 static MetaType* result = nullptr ;
276 if(result == nullptr) {
278 typeid(T).name()
279 ) ;
280 }
281 if(result == nullptr) {
282 Logger::err("Meta") << "No MetaType for typeid "
283 << typeid(T).name()
284 << std::endl ;
285 }
286 ogf_assert(result != nullptr) ;
287 return result ;
288 }
289
298 static MetaClass* result = nullptr ;
299 if(result == nullptr) {
300 result = dynamic_cast<MetaClass*>(type()) ;
301 }
302 if(result == nullptr) {
303 Logger::err("Meta") << "MetaType for typeid "
304 << typeid(T).name()
305 << " is not a MetaClass"
306 << std::endl ;
307 }
308 ogf_assert(result != nullptr) ;
309 return result ;
310 }
311
320 static Serializer* result = type()->serializer() ;
321 if(result == nullptr) {
322 Logger::err("Meta") << "No Serializer for typeid "
323 << typeid(T).name()
324 << std::endl ;
325 Logger::err("Meta") << "type name = " << type()->name()
326 << std::endl ;
327 }
328 ogf_assert(result != nullptr) ;
329 return result ;
330 }
331
339 static Factory* factory() {
340 return meta_class()->factory() ;
341 }
342 } ;
343
344 //___________________________________________________________________
345
353 template <class T> inline bool ogf_convert_to_string(
354 const T& value, std::string& string
355 ) {
356 std::ostringstream stream ;
358 stream, (void*)(&value)
359 ) ;
360 if(result) {
361 string = stream.str() ;
362 }
363 return result ;
364 }
365
373 template <class T> inline bool ogf_convert_from_string(
374 const std::string& string, T& value
375 ) {
376 if(string.length() == 0) {
377 value = T() ;
378 return true ;
379 }
380 std::istringstream stream(string) ;
382 stream, (void*)(&value)
383 ) ;
384 }
385
386 //___________________________________________________________________
387
388}
389
390#endif
Creates instances of a specific class.
Definition factory.h:61
The representation of a class in the Meta repository.
Definition meta_class.h:64
Factory * factory() const
Gets the factory.
Definition meta_class.h:440
The representation of a type in the Meta repository.
Definition meta_type.h:222
Serializer * serializer() const
Gets the serializer associated with the type.
Definition meta_type.h:287
const std::string & name() const
Gets the C++ name of the type.
Definition meta_type.h:242
Stores all the meta information of the system, used by the reflection API.
Definition meta.h:73
MetaClass * resolve_meta_class(const std::string &type_name) const
Finds a MetaClass by type name.
Definition meta.h:149
bool meta_type_is_bound(const std::string &name) const
Tests whether a MetaType exists in the system by type name.
MetaType * resolve_meta_type(const std::string &type_name) const
Finds a MetaType by type name.
static Meta * instance()
Gets the instance.
~Meta()
destructor fo the Meta database.
bool typeid_name_is_bound(const std::string &typeid_name) const
Tests whether a MetaType exists in the system by typeid name.
bool bind_meta_type(MetaType *meta_type)
Declares a MetaType to the system.
bool bind_meta_type(MetaType *meta_type, const std::string &typeid_name)
Declares a MetaType to the system.
void list_types(std::vector< MetaType * > &types)
Gets the list of all MetaType objects declared to the system.
void list_type_names(std::vector< std::string > &type_names)
Gets the list of all type names declared to the system.
bool unbind_meta_type(const std::string &name)
Removes a MetaType from the system.
void bind_meta_type_alias(MetaType *meta_type, const std::string &alias)
Declares an alias (typedef) to a MetaType.
static void initialize()
Initializes the Meta database.
static void terminate()
Terminates the Meta database.
MetaType * resolve_meta_type_by_typeid_name(const std::string &typeid_name) const
Finds a MetaType by typeid name.
Abstract base class for reading and writing values from/to streams.
Definition serializer.h:58
virtual bool serialize_read(std::istream &stream, void *addr)=0
Reads a value from a stream.
virtual bool serialize_write(std::ostream &stream, void *addr)=0
Writes a value to a stream.
Provides easy access to meta information from C++ types.
Definition meta.h:267
static Serializer * serializer()
Gets the Serializer.
Definition meta.h:319
static Factory * factory()
Gets the Factory.
Definition meta.h:339
static MetaClass * meta_class()
Gets the MetaClass.
Definition meta.h:297
static MetaType * type()
Gets the MetaType.
Definition meta.h:274
MetaType for classes.
Base class for meta informations.
Global Graphite namespace.
Definition common.h:76
bool ogf_is_a(const T &x, const MetaType *type)
Tests whether the type of a variable derives from another type.
Definition meta.h:257
MetaType * ogf_static_type(const T &x)
Gets the MetaType associated with a variable.
Definition meta.h:243
bool ogf_convert_from_string(const std::string &string, T &value)
Converts a string to a variable using the reflection API.
Definition meta.h:373
bool ogf_convert_to_string(const T &value, std::string &string)
Converts a variable to a string using the reflection API.
Definition meta.h:353
MetaType * ogf_dynamic_type(const T &x)
Gets the MetaType associated with a variable.
Definition meta.h:227
Definitions common to all include files in the gom library.