MPQC  2.3.1
class.h
1 //
2 // class.h
3 //
4 // Copyright (C) 1996 Limit Point Systems, Inc.
5 //
6 // Author: Curtis Janssen <cljanss@limitpt.com>
7 // Maintainer: LPS
8 //
9 // This file is part of the SC Toolkit.
10 //
11 // The SC Toolkit is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU Library General Public License as published by
13 // the Free Software Foundation; either version 2, or (at your option)
14 // any later version.
15 //
16 // The SC Toolkit is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU Library General Public License for more details.
20 //
21 // You should have received a copy of the GNU Library General Public License
22 // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 //
25 // The U.S. Government is granted a limited license as per AL 91-7.
26 //
27 
28 #ifdef __GNUG__
29 #pragma interface
30 #endif
31 
32 #ifndef _util_class_class_h
33 #define _util_class_class_h
34 
35 #include <map>
36 #include <set>
37 #include <string>
38 
39 #include <stdio.h>
40 #include <string.h>
41 #include <stdarg.h>
42 #include <iostream>
43 #include <iomanip>
44 #include <typeinfo>
45 #include <util/ref/ref.h>
46 #include <util/misc/exenv.h>
47 
48 namespace sc {
49 
50 template <class T, class C>
52  private:
53  T C::*member_;
54  public:
55  DescribedMemberDatum(T C::*member): member_(member) {}
56  //T &member(C *c) { return c->*member_; }
57 };
58 
59 class DescribedClass;
60 class ClassDesc;
61 typedef ClassDesc* ClassDescP;
62 typedef const ClassDesc* CClassDescP;
63 
64 class ClassDesc;
65 
68 {
69  public:
70  enum Access { Private, Protected, Public };
71  private:
72  Access _access;
73  int _is_virtual;
74  ClassDesc* _classdesc;
75  public:
76  ParentClass(ClassDesc*,Access access = Private,int is_virtual = 0);
77  ParentClass(const ParentClass&);
78  ~ParentClass();
79  int is_virtual() const;
80  Access access() const { return _access; }
81  const ClassDesc* classdesc() const;
82  void change_classdesc(ClassDesc*n);
83 };
84 
87 {
88  private:
89  int _n;
90  ParentClass** _classes;
91  void add(ParentClass*);
92  // do not allow copy constructor or assignment
94  void operator=(const ParentClasses&);
95  public:
96  ParentClasses();
97  void init(const char*);
98  ~ParentClasses();
99  ParentClass& parent(int i) { return *_classes[i]; }
100  const ParentClass& parent(int i) const { return *_classes[i]; }
101  ParentClass& operator[](int i) { return *_classes[i]; }
102  const ParentClass& operator[](int i) const { return *_classes[i]; }
103  int n() const { return _n; }
104  void change_parent(ClassDesc*oldcd,ClassDesc*newcd);
105 };
106 
107 
108 class KeyVal;
109 class StateIn;
110 
113 template <class T>
114 DescribedClass* create()
115 {
116  return new T;
117 }
118 
121 template <class T>
122 DescribedClass* create(const Ref<KeyVal>& keyval)
123 {
124  return new T(keyval);
125 }
126 
129 template <class T>
130 DescribedClass* create(StateIn& statein)
131 {
132  return new T(statein);
133 }
134 
136  private:
137  const std::type_info *ti_;
138  public:
139  type_info_key(): ti_(0) {}
140  type_info_key(const std::type_info *ti): ti_(ti) {}
141  type_info_key& operator=(const type_info_key&);
142  int operator==(const type_info_key&) const;
143  int operator<(const type_info_key&) const;
144  int cmp(const type_info_key&) const;
145 };
146 
158 class ClassDesc: public Identity {
159  friend class ParentClasses;
160  private:
161  static std::map<std::string,ClassDescP> *all_;
162  static std::map<type_info_key,ClassDescP> *type_info_all_;
163  static char * classlib_search_path_;
164  static std::set<std::string> *unresolved_parents_;
165 
166  char* classname_;
167  int version_;
168  ParentClasses parents_;
169  std::set<std::string> *children_;
170  DescribedClass* (*ctor_)();
171  DescribedClass* (*keyvalctor_)(const Ref<KeyVal>&);
172  DescribedClass* (*stateinctor_)(StateIn&);
173  const std::type_info *ti_;
174 
175  void change_parent(ClassDesc*oldcd,ClassDesc*newcd);
176 
177  // do not allow copy constructor or assignment
178  ClassDesc(const ClassDesc&);
179  void operator=(const ClassDesc&);
180 
181  // this is used for temporary parent class descriptors
182  ClassDesc(const char*);
183  void init(const char*,int=1,const char* p=0,
184  const std::type_info *ti=0,
185  DescribedClass* (*ctor)()=0,
186  DescribedClass* (*keyvalctor)(const Ref<KeyVal>&)=0,
187  DescribedClass* (*stateinctor)(StateIn&)=0);
188  public:
189  ClassDesc(const std::type_info&, const char*,int=1,const char* p=0,
190  DescribedClass* (*ctor)()=0,
191  DescribedClass* (*keyvalctor)(const Ref<KeyVal>&)=0,
192  DescribedClass* (*stateinctor)(StateIn&)=0);
193  ~ClassDesc();
194 
195  static std::map<std::string,ClassDescP>& all();
196  const ParentClasses& parents() const { return parents_; }
197 
199  static void list_all_classes();
202  static ClassDesc* name_to_class_desc(const char*);
204  static ClassDesc *class_desc(const std::type_info &);
206  const char* name() const { return classname_; }
208  int version() const { return version_; }
218  virtual DescribedClass* create() const;
224  virtual DescribedClass* create(const Ref<KeyVal>&) const;
230  virtual DescribedClass* create(StateIn&) const;
231 
234  static int load_class(const char* classname);
235 };
236 
244 class DescribedClass : public RefCount {
245  public:
246  DescribedClass();
248  DescribedClass& operator=(const DescribedClass&);
249  virtual ~DescribedClass();
252  ClassDesc* class_desc() const throw();
254  const char* class_name() const;
256  int class_version() const;
258  virtual void print(std::ostream& = ExEnv::out0()) const;
259  };
260 
262 template <class T>
263 inline ClassDesc *
264 class_desc()
265 {
266  return ClassDesc::class_desc(typeid(T));
267 }
268 
271 inline ClassDesc *
272 class_desc(DescribedClass *d)
273 {
274  return ClassDesc::class_desc(typeid(*d));
275 }
276 
279 template<class T>
280 inline T
281 require_dynamic_cast(DescribedClass*p,const char * errmsg,...)
282 {
283  T t = dynamic_cast<T>(p);
284  if (p && !t) {
285  va_list args;
286  va_start(args,errmsg);
287  fprintf(stderr,"A required dynamic_cast failed in: ");
288  vfprintf(stderr,errmsg,args);
289  fprintf(stderr,"\nwanted type \"%s\" but got \"%s\"\n",
290  typeid(T).name(),p->class_desc()->name());
291  fflush(stderr);
292  va_end(args);
293  abort();
294  }
295  return t;
296 }
297 
300 template<class T>
301 inline T
302 require_dynamic_cast(const DescribedClass*p,const char * errmsg,...)
303 {
304  T t = dynamic_cast<T>(p);
305  if (p && !t) {
306  va_list args;
307  va_start(args,errmsg);
308  fprintf(stderr,"A required dynamic_cast failed in: ");
309  vfprintf(stderr,errmsg,args);
310  fprintf(stderr,"\nwanted type \"%s\" but got \"%s\"\n",
311  typeid(T).name(),p->class_desc()->name());
312  fflush(stderr);
313  va_end(args);
314  abort();
315  }
316  return t;
317 }
318 
321 template <class A>
323  public:
324  ForceLinkBase() {};
325  virtual ~ForceLinkBase() {};
326  virtual DescribedClass *create(A) = 0;
327 };
328 
338 template <class T, class A = const Ref<KeyVal> &>
339 class ForceLink: public ForceLinkBase<A> {
340  public:
341  ForceLink() {};
342  virtual ~ForceLink() {};
343  DescribedClass *create(A a) { return new T(a); }
344 };
345 
346 }
347 
348 #endif
349 
350 // Local Variables:
351 // mode: c++
352 // c-file-style: "CLJ"
353 // End:
sc::ClassDesc::version
int version() const
Returns the version number of the class.
Definition: class.h:208
sc::Identity
Identity gives objects a unique identity and ordering relationship relative to all other objects.
Definition: identity.h:89
sc::KeyVal
The KeyVal class is designed to simplify the process of allowing a user to specify keyword/value asso...
Definition: keyval.h:69
sc::DescribedClass::class_desc
ClassDesc * class_desc() const
This returns the unique pointer to the ClassDesc corresponding to the given type_info object.
sc::Ref
A template class that maintains references counts.
Definition: ref.h:332
sc::ParentClasses
Gives a list of parent classes of a class.
Definition: class.h:86
sc::ParentClass
Gives one parent class of a class.
Definition: class.h:67
sc::DescribedClass::class_version
int class_version() const
Return the version of the class.
sc::ClassDesc::class_desc
static ClassDesc * class_desc(const std::type_info &)
Given a type_info object return a pointer to the ClassDesc.
sc::ClassDesc::name_to_class_desc
static ClassDesc * name_to_class_desc(const char *)
Given the name of the class, return a pointer to the class descriptor.
sc::StateIn
Restores objects that derive from SavableState.
Definition: statein.h:70
sc::ClassDesc::name
const char * name() const
Returns the name of the class.
Definition: class.h:206
sc::ClassDesc
This class is used to contain information about classes.
Definition: class.h:158
sc::type_info_key
Definition: class.h:135
sc::DescribedClass::print
virtual void print(std::ostream &=ExEnv::out0()) const
Print the object.
sc::ClassDesc::load_class
static int load_class(const char *classname)
Attempt to dynamically load the shared object file for classname.
sc::ClassDesc::create
virtual DescribedClass * create() const
Create an instance of DescribedClass with exact type equal to the class to which this class descripto...
sc::DescribedClass
Classes which need runtime information about themselves and their relationship to other classes can v...
Definition: class.h:244
sc::ExEnv::out0
static std::ostream & out0()
Return an ostream that writes from node 0.
sc::DescribedMemberDatum
Definition: class.h:51
sc::RefCount
The base class for all reference counted objects.
Definition: ref.h:194
sc::ClassDesc::create_described_class
DescribedClass * create_described_class() const
This member has been replaced by create().
sc::ClassDesc::list_all_classes
static void list_all_classes()
Writes a list of all of the classes to ExEnv::out0().
sc::DescribedClass::class_name
const char * class_name() const
Return the name of the object's exact type.
sc::ForceLinkBase
This, together with ForceLink, is used to force code for particular classes to be linked into executa...
Definition: class.h:322

Generated at Sun Jan 26 2020 23:33:03 for MPQC 2.3.1 using the documentation package Doxygen 1.8.16.