ug4
class_name_provider.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3  * Authors: Sebastian Reiter, Andreas Vogel
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 
34 #ifndef __H__UG_BRIDGE__CLASS_NAME_PROVIDER__
35 #define __H__UG_BRIDGE__CLASS_NAME_PROVIDER__
36 
37 #include "common/assert.h"
38 #include <vector>
39 #include <cstring>
40 #include <string>
41 #include <algorithm>
42 #include <iostream>
43 #include <typeinfo>
44 #include <map>
45 #include "common/common.h"
47 #include "common/ug_config.h"
48 #include "error.h"
49 
50 namespace ug
51 {
52 namespace bridge
53 {
54 
57 
59 
65 {
66  public:
68  ClassNameNode();
69 
71  void set_name(const std::string& name);
72 
74  void add_base_class(const ClassNameNode& node);
75 
77  const std::string& name() const {return m_name;}
78 
80  bool empty() const {return m_name.empty();}
81 
83  bool named() const;
84 
86  size_t num_base_classes() const {return m_vBaseClass.size();}
87 
89  const ClassNameNode& base_class(size_t i) const {return *m_vBaseClass.at(i);}
90 
91  protected:
93  std::string m_name;
94 
96  std::vector<const ClassNameNode*> m_vBaseClass;
97 };
98 
100 template <typename TClass>
102 {
103  public:
105  static void set_name(const std::string& nameIn, const std::string& group,
106  bool newName = false);
107 
109  template <typename TParent1>
110  static void set_name(const std::string& nameIn, const std::string& group,
111  bool newName = false);
112 
114  template <typename TParent1, typename TParent2>
115  static void set_name(const std::string& nameIn, const std::string& group,
116  bool newName = false);
117 
124  static bool is_a(const std::string& parent, bool strict = false);
125 
127  static const std::string& name();
128 
130  static const std::string& group(){return m_group;}
131 
133  static const std::vector<const char*>& names();
134 
136  static const ClassNameNode& class_name_node() {return m_ClassNameNode;}
137 
139  static bool forward_declared() {return m_bForwardDeclared;}
140 
142  static bool named() {return !m_bForwardDeclared && !m_ClassNameNode.empty();}
143 
144  protected:
146  static void set_foreward_declared();
147 
148  private:
150  static std::vector<const char*> m_names;
151 
153  static std::string m_group;
154 
156  static bool m_bForwardDeclared;
157 
160 };
161 
162 template <typename TClass>
163 const char* GetClassName(){
164  return ClassNameProvider<TClass>::name().c_str();
165 }
166 
168 template <typename TBase, typename TDerived>
169 void* StaticVoidCast(void* DerivVoidPtr);
170 
171 
173  UGError_ClassCastFailed(const std::string& from, const std::string& to) :
174  UGError("Class cast failed"), m_from(from), m_to(to) {}
175 
176  std::string m_from;
177  std::string m_to;
178 };
179 
182 {
183  public:
185  template <typename TBase, typename TDerived>
186  static void add_cast_func();
187 
189 
201  static void* cast_to_base_class(void* pDerivVoid,
202  const ClassNameNode*& node,
203  const std::string& baseName);
204  static const void* cast_to_base_class(const void* pDerivVoid,
205  const ClassNameNode*& node,
206  const std::string& baseName);
208 
210 
221  template <typename T>
222  static T* cast_to(void* pDerivVoid, const ClassNameNode*& node);
223  template <typename T>
224  static const T* cast_to(const void* pDerivVoid, const ClassNameNode*& node);
225  template <typename T>
226  static SmartPtr<T> cast_to(SmartPtr<void> pDerivVoid, const ClassNameNode*& node);
227  template <typename T>
228  static ConstSmartPtr<T> cast_to(ConstSmartPtr<void> pDerivVoid, const ClassNameNode*& node);
230 
231  protected:
232  // type of cast pointer
233  typedef void* (*CastFunc)(void*);
234 
235  // cast map
236  static std::map<std::pair<const ClassNameNode*, const ClassNameNode*>, CastFunc> m_mmCast;
237 };
238 
239 
241 void ExtractClassNameVec(std::vector<const char*>& names,
242  const ClassNameNode& node,
243  bool clearVec = true);
244 
246 bool ClassNameVecContains(const std::vector<const char*>& names, const std::string& name);
247 
249 bool ClassNameTreeContains(const ClassNameNode& node, const std::string& name);
250 
253 bool ClassNameTreeWay(std::vector<size_t>& vWay, const ClassNameNode& node, const std::string& name);
254 
255 // end group registry
257 
258 } // end namespace
259 } // end namespace
260 
261 // include implementation
263 
264 #endif /* __H__UG_BRIDGE__CLASS_NAME_PROVIDER__ */
location name
Definition: checkpoint_util.lua:128
Definition: smart_pointer.h:650
Definition: smart_pointer.h:296
Definition: smart_pointer.h:525
Definition: smart_pointer.h:108
Instances of this class or of derived classes are thrown if errors arise.
Definition: error.h:104
provides castings from derived classes to base classes
Definition: class_name_provider.h:182
static const T * cast_to(const void *pDerivVoid, const ClassNameNode *&node)
cast a pointer to the desired base class
static void * cast_to_base_class(void *pDerivVoid, const ClassNameNode *&node, const std::string &baseName)
cast a pointer to the desired base class
Definition: class_name_provider.cpp:139
static T * cast_to(void *pDerivVoid, const ClassNameNode *&node)
casts a void pointer to a concrete class
void *(* CastFunc)(void *)
Definition: class_name_provider.h:233
static SmartPtr< T > cast_to(SmartPtr< void > pDerivVoid, const ClassNameNode *&node)
cast a pointer to the desired base class
static ConstSmartPtr< T > cast_to(ConstSmartPtr< void > pDerivVoid, const ClassNameNode *&node)
cast a pointer to the desired base class
static void add_cast_func()
add a cast function to the registry: Casts: Derived -> Base
static std::map< std::pair< const ClassNameNode *, const ClassNameNode * >, CastFunc > m_mmCast
Definition: class_name_provider.h:236
node for class names
Definition: class_name_provider.h:65
bool empty() const
returns if a name has been set
Definition: class_name_provider.h:80
const std::string & name() const
returns own name
Definition: class_name_provider.h:77
size_t num_base_classes() const
returns number of parents
Definition: class_name_provider.h:86
std::string m_name
own name
Definition: class_name_provider.h:93
std::vector< const ClassNameNode * > m_vBaseClass
base classes
Definition: class_name_provider.h:96
const ClassNameNode & base_class(size_t i) const
return a base class
Definition: class_name_provider.h:89
provides the name for a class
Definition: class_name_provider.h:102
static bool named()
returns if the class has been named by user
Definition: class_name_provider.h:142
static std::string m_group
Name of group, we're this class is sorted.
Definition: class_name_provider.h:153
static void set_name(const std::string &nameIn, const std::string &group, bool newName=false)
set name of class and copy parent names
static bool m_bForwardDeclared
set to true if class has not been named by user, but a default name given
Definition: class_name_provider.h:156
static const std::string & name()
name of this class
Definition: class_name_provider_impl.h:152
static std::vector< const char * > m_names
vector of parent class names (depreciated)
Definition: class_name_provider.h:150
static bool forward_declared()
returns if class name is forward declared
Definition: class_name_provider.h:139
static const ClassNameNode & class_name_node()
return the class name node in the class hierarchy
Definition: class_name_provider.h:136
static void set_name(const std::string &nameIn, const std::string &group, bool newName=false)
set name of class and copy parent names
static ClassNameNode m_ClassNameNode
class name node holding own name and pointers to base class name nodes
Definition: class_name_provider.h:159
static const std::string & group()
groups
Definition: class_name_provider.h:130
const char * GetClassName()
Definition: class_name_provider.h:163
bool ClassNameTreeContains(const ClassNameNode &node, const std::string &name)
returns if a name is contained in the name tree at node or in base classes
Definition: class_name_provider.cpp:104
void * StaticVoidCast(void *DerivVoidPtr)
static cast function for two classes
bool ClassNameVecContains(const std::vector< const char * > &names, const std::string &name)
returns if a name is contained in the name vector
Definition: class_name_provider.cpp:76
void ExtractClassNameVec(std::vector< const char * > &names, const ClassNameNode &node, bool clearVec)
returns the vector containing all names in the name tree for node and its base classes
Definition: class_name_provider.cpp:90
bool ClassNameTreeWay(std::vector< size_t > &vWay, const ClassNameNode &node, const std::string &name)
Definition: class_name_provider.cpp:119
#define UG_API
Definition: ug_config.h:65
the ug namespace
void baseName(std::string &nameOut, const std::string &nameIn)
Definition: vtkoutput.cpp:209
Definition: class_name_provider.h:172
std::string m_to
Definition: class_name_provider.h:177
UGError_ClassCastFailed(const std::string &from, const std::string &to)
Definition: class_name_provider.h:173
std::string m_from
Definition: class_name_provider.h:176