Graphite  Version 3
An experimental 3D geometry processing program
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 
40 #include <OGF/gom/common/common.h>
43 
44 #include <typeinfo>
45 #include <string>
46 #include <map>
47 #include <vector>
48 
49 #include <sstream>
50 
51 //___________________________________________________
52 
58 namespace OGF {
59 
73  class GOM_API Meta {
74  public:
78  ~Meta() ;
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 
122  MetaType* meta_type, const std::string& typeid_name
123  ) ;
124 
131  MetaType* resolve_meta_type(const std::string& type_name) const ;
132 
139  MetaClass* resolve_meta_class(const std::string& type_name) const {
140  return dynamic_cast<MetaClass*>(resolve_meta_type(type_name));
141  }
142 
151  const std::string& typeid_name
152  ) const ;
153 
154 
161  bool unbind_meta_type(const std::string& name) ;
162 
167  void list_types(std::vector<MetaType*>& types) ;
168 
173  void list_type_names(std::vector<std::string>& type_names);
174 
180  static void initialize() ;
181 
187  static void terminate() ;
188 
189  private:
194  Meta() ;
195 
196  typedef std::map<std::string, MetaType_var> MetaTypesTable ;
197  typedef std::map<std::string, MetaType*> TypeidNamesTable ;
198 
199  static Meta* instance_ ;
200  MetaTypesTable type_name_to_meta_type_ ;
201  TypeidNamesTable typeid_name_to_meta_type_ ;
202  } ;
203 
204  //___________________________________________________________________
205 
206 
217  template <class T> inline MetaType* ogf_dynamic_type(const T& x) {
218  ogf_argused(x); // suppresses a warning of MSVC
220  typeid(x).name()
221  ) ;
222  }
223 
233  template <class T> inline MetaType* ogf_static_type(const T& x) {
234  ogf_argused(x); // suppresses a warning of MSVC
236  typeid(T).name()
237  ) ;
238  }
239 
247  template <class T> inline bool ogf_is_a(const T& x, const MetaType* type) {
248  return ogf_dynamic_type(x)->is_subtype_of(type) ;
249  }
250 
251  //___________________________________________________________________
252 
257  template <class T> class ogf_meta {
258  public:
259 
264  static MetaType* type() {
265  static MetaType* result = nullptr ;
266  if(result == nullptr) {
268  typeid(T).name()
269  ) ;
270  }
271  if(result == nullptr) {
272  Logger::err("Meta") << "No MetaType for typeid "
273  << typeid(T).name()
274  << std::endl ;
275  }
276  ogf_assert(result != nullptr) ;
277  return result ;
278  }
279 
287  static MetaClass* meta_class() {
288  static MetaClass* result = nullptr ;
289  if(result == nullptr) {
290  result = dynamic_cast<MetaClass*>(type()) ;
291  }
292  if(result == nullptr) {
293  Logger::err("Meta") << "MetaType for typeid "
294  << typeid(T).name()
295  << " is not a MetaClass"
296  << std::endl ;
297  }
298  ogf_assert(result != nullptr) ;
299  return result ;
300  }
301 
309  static Serializer* serializer() {
310  static Serializer* result = type()->serializer() ;
311  if(result == nullptr) {
312  Logger::err("Meta") << "No Serializer for typeid "
313  << typeid(T).name()
314  << std::endl ;
315  Logger::err("Meta") << "type name = " << type()->name()
316  << std::endl ;
317  }
318  ogf_assert(result != nullptr) ;
319  return result ;
320  }
321 
329  static Factory* factory() {
330  return meta_class()->factory() ;
331  }
332  } ;
333 
334  //___________________________________________________________________
335 
343  template <class T> inline bool ogf_convert_to_string(
344  const T& value, std::string& string
345  ) {
346  std::ostringstream stream ;
347  bool result = ogf_meta<T>::serializer()->serialize_write(
348  stream, (void*)(&value)
349  ) ;
350  if(result) {
351  string = stream.str() ;
352  }
353  return result ;
354  }
355 
363  template <class T> inline bool ogf_convert_from_string(
364  const std::string& string, T& value
365  ) {
366  if(string.length() == 0) {
367  value = T() ;
368  return true ;
369  }
370  std::istringstream stream(string) ;
372  stream, (void*)(&value)
373  ) ;
374  }
375 
376  //___________________________________________________________________
377 
378 }
379 
380 #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:433
The representation of a type in the Meta repository.
Definition: meta_type.h:221
const std::string & name() const
Gets the C++ name of the type.
Definition: meta_type.h:241
Serializer * serializer() const
Gets the serializer associated with the type.
Definition: meta_type.h:286
Stores all the meta information of the system, used by the reflection API.
Definition: meta.h:73
MetaType * resolve_meta_type_by_typeid_name(const std::string &typeid_name) const
Finds a MetaType by typeid name.
bool meta_type_is_bound(const std::string &name) const
Tests whether a MetaType exists in the system by type name.
static Meta * instance()
Gets the instance.
~Meta()
destructor fo the Meta database.
MetaClass * resolve_meta_class(const std::string &type_name) const
Finds a MetaClass by type name.
Definition: meta.h:139
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.
MetaType * resolve_meta_type(const std::string &type_name) const
Finds a MetaType by type name.
static void initialize()
Initializes the Meta database.
static void terminate()
Terminates the Meta database.
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:257
static MetaType * type()
Gets the MetaType.
Definition: meta.h:264
static Factory * factory()
Gets the Factory.
Definition: meta.h:329
static MetaClass * meta_class()
Gets the MetaClass.
Definition: meta.h:287
static Serializer * serializer()
Gets the Serializer.
Definition: meta.h:309
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:247
bool ogf_convert_from_string(const std::string &string, T &value)
Converts a string to a variable using the reflection API.
Definition: meta.h:363
bool ogf_convert_to_string(const T &value, std::string &string)
Converts a variable to a string using the reflection API.
Definition: meta.h:343
MetaType * ogf_dynamic_type(const T &x)
Gets the MetaType associated with a variable.
Definition: meta.h:217
MetaType * ogf_static_type(const T &x)
Gets the MetaType associated with a variable.
Definition: meta.h:233
Definitions common to all include files in the gom library.