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
48namespace sc {
49
50template <class T, class C>
51class DescribedMemberDatum {
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
59class DescribedClass;
60class ClassDesc;
61typedef ClassDesc* ClassDescP;
62typedef const ClassDesc* CClassDescP;
63
64class ClassDesc;
65
67class ParentClass
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
86class ParentClasses
87{
88 private:
89 int _n;
90 ParentClass** _classes;
91 void add(ParentClass*);
92 // do not allow copy constructor or assignment
93 ParentClasses(const ParentClasses&);
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
108class KeyVal;
109class StateIn;
110
113template <class T>
114DescribedClass* create()
115{
116 return new T;
117}
118
121template <class T>
122DescribedClass* create(const Ref<KeyVal>& keyval)
123{
124 return new T(keyval);
125}
126
129template <class T>
130DescribedClass* create(StateIn& statein)
131{
132 return new T(statein);
133}
134
135class type_info_key {
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
158class 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
244class DescribedClass : public RefCount {
245 public:
246 DescribedClass();
247 DescribedClass(const 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
262template <class T>
263inline ClassDesc *
265{
266 return ClassDesc::class_desc(typeid(T));
267}
268
271inline ClassDesc *
273{
274 return ClassDesc::class_desc(typeid(*d));
275}
276
279template<class T>
280inline T
281require_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
300template<class T>
301inline T
302require_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
321template <class A>
322class ForceLinkBase {
323 public:
324 ForceLinkBase() {};
325 virtual ~ForceLinkBase() {};
326 virtual DescribedClass *create(A) = 0;
327};
328
338template <class T, class A = const Ref<KeyVal> &>
339class 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:
This class is used to contain information about classes.
Definition class.h:158
int version() const
Returns the version number of the class.
Definition class.h:208
static int load_class(const char *classname)
Attempt to dynamically load the shared object file for classname.
static ClassDesc * name_to_class_desc(const char *)
Given the name of the class, return a pointer to the class descriptor.
virtual DescribedClass * create() const
Create an instance of DescribedClass with exact type equal to the class to which this class descripto...
const char * name() const
Returns the name of the class.
Definition class.h:206
static void list_all_classes()
Writes a list of all of the classes to ExEnv::out0().
virtual DescribedClass * create(StateIn &) const
Create an instance of DescribedClass with exact type equal to the class to which this class descripto...
static ClassDesc * class_desc(const std::type_info &)
Given a type_info object return a pointer to the ClassDesc.
DescribedClass * create_described_class() const
This member has been replaced by create().
virtual DescribedClass * create(const Ref< KeyVal > &) const
Create an instance of DescribedClass with exact type equal to the class to which this class descripto...
Classes which need runtime information about themselves and their relationship to other classes can v...
Definition class.h:244
const char * class_name() const
Return the name of the object's exact type.
int class_version() const
Return the version of the class.
ClassDesc * class_desc() const
This returns the unique pointer to the ClassDesc corresponding to the given type_info object.
virtual void print(std::ostream &=ExEnv::out0()) const
Print the object.
The ExEnv class is used to find out about how the program is being run.
Definition exenv.h:47
Identity gives objects a unique identity and ordering relationship relative to all other objects.
Definition identity.h:89
The KeyVal class is designed to simplify the process of allowing a user to specify keyword/value asso...
Definition keyval.h:69
Gives one parent class of a class.
Definition class.h:68
A template class that maintains references counts.
Definition ref.h:332
Restores objects that derive from SavableState.
Definition statein.h:70

Generated at Fri Jan 31 2025 00:00:00 for MPQC 2.3.1 using the documentation package Doxygen 1.13.2.