MPQC 3.0.0-alpha
Loading...
Searching...
No Matches
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#ifndef _util_class_class_h
29#define _util_class_class_h
30
31#include <map>
32#include <set>
33#include <string>
34
35#include <stdio.h>
36#include <string.h>
37#include <stdarg.h>
38#include <iostream>
39#include <iomanip>
40#include <typeinfo>
41#include <util/ref/ref.h>
42#include <util/misc/exenv.h>
43#include <util/misc/exception.h>
44
45namespace sc {
46
47class DescribedClass;
48class ClassDesc;
49typedef ClassDesc* ClassDescP;
50typedef const ClassDesc* CClassDescP;
51
52class ClassDesc;
53
56{
57 public:
58 enum Access { Private, Protected, Public };
59 private:
60 Access _access;
61 int _is_virtual;
62 ClassDesc* _classdesc;
63 public:
64 ParentClass(ClassDesc*,Access access = Private,int is_virtual = 0);
67 int is_virtual() const;
68 Access access() const { return _access; }
69 const ClassDesc* classdesc() const;
70 void change_classdesc(ClassDesc*n);
71};
72
75{
76 private:
77 int _n;
78 ParentClass** _classes;
79 void add(ParentClass*);
80 // do not allow copy constructor or assignment
82 void operator=(const ParentClasses&);
83 public:
85 void init(const char*);
87 ParentClass& parent(int i) { return *_classes[i]; }
88 const ParentClass& parent(int i) const { return *_classes[i]; }
89 ParentClass& operator[](int i) { return *_classes[i]; }
90 const ParentClass& operator[](int i) const { return *_classes[i]; }
91 int n() const { return _n; }
92 void change_parent(ClassDesc*oldcd,ClassDesc*newcd);
93};
94
95
96class KeyVal;
97class StateIn;
98
101template <class T>
103{
104 return new T;
105}
106
109template <class T>
111{
112 return new T(keyval);
113}
114
117template <class T>
119{
120 return new T(statein);
121}
122
124 private:
125 const std::type_info *ti_;
126 public:
127 type_info_key(): ti_(0) {}
128 type_info_key(const std::type_info *ti): ti_(ti) {}
129 type_info_key& operator=(const type_info_key&);
130 int operator==(const type_info_key&) const;
131 int operator<(const type_info_key&) const;
132 int cmp(const type_info_key&) const;
133 const std::type_info* type_info() const { return ti_; }
134};
135
148 friend class ParentClasses;
149 private:
150 static std::map<std::string,ClassDescP> *all_;
151 static std::map<type_info_key,ClassDescP> *type_info_all_;
152 static char * classlib_search_path_;
153 static std::set<std::string> *unresolved_parents_;
154
155 char* classname_;
156 int version_;
157 ParentClasses parents_;
158 std::set<std::string> *children_;
159 DescribedClass* (*ctor_)();
160 DescribedClass* (*keyvalctor_)(const Ref<KeyVal>&);
161 DescribedClass* (*stateinctor_)(StateIn&);
162 const std::type_info *ti_;
163
164 void change_parent(ClassDesc*oldcd,ClassDesc*newcd);
165
166 // do not allow copy constructor or assignment
167 ClassDesc(const ClassDesc&);
168 void operator=(const ClassDesc&);
169
170 // this is used for temporary parent class descriptors
171 ClassDesc(const char*);
172 void init(const char*,int=1,const char* p=0,
173 const std::type_info *ti=0,
174 DescribedClass* (*ctor)()=0,
175 DescribedClass* (*keyvalctor)(const Ref<KeyVal>&)=0,
176 DescribedClass* (*stateinctor)(StateIn&)=0);
177 public:
178 ClassDesc(const std::type_info&, const char*,int=1,const char* p=0,
179 DescribedClass* (*ctor)()=0,
180 DescribedClass* (*keyvalctor)(const Ref<KeyVal>&)=0,
181 DescribedClass* (*stateinctor)(StateIn&)=0);
182 ~ClassDesc();
183
184 static std::map<std::string,ClassDescP>& all();
185 const ParentClasses& parents() const { return parents_; }
186
188 static void list_all_classes();
191 static ClassDesc* name_to_class_desc(const char*);
193 static ClassDesc *class_desc(const std::type_info &);
195 const char* name() const { return classname_; }
197 int version() const { return version_; }
207 virtual DescribedClass* create() const;
213 virtual DescribedClass* create(const Ref<KeyVal>&) const;
219 virtual DescribedClass* create(StateIn&) const;
220
223 static int load_class(const char* classname);
224};
225
233class DescribedClass : virtual public RefCount {
234 public:
237 DescribedClass& operator=(const DescribedClass&);
238 virtual ~DescribedClass();
241 ClassDesc* class_desc() const MPQC__NOEXCEPT;
243 const char* class_name() const;
245 int class_version() const;
247 virtual void print(std::ostream& = ExEnv::out0()) const;
252 };
253
255template <class T>
256inline ClassDesc *
258{
259 return ClassDesc::class_desc(typeid(T));
260}
261
264inline ClassDesc *
266{
267 return ClassDesc::class_desc(typeid(*d));
268}
269
272template<class T>
273inline T
274require_dynamic_cast(DescribedClass*p,const char * errmsg,...)
275{
276 T t = dynamic_cast<T>(p);
277 if (p && !t) {
278 va_list args;
279 va_start(args,errmsg);
280 fprintf(stderr,"A required dynamic_cast failed in: ");
281 vfprintf(stderr,errmsg,args);
282 fprintf(stderr,"\nwanted type \"%s\" but got \"%s\"\n",
283 typeid(T).name(),p->class_desc()->name());
284 fflush(stderr);
285 va_end(args);
286 abort();
287 }
288 return t;
289}
290
293template<class T>
294inline T
295require_dynamic_cast(const DescribedClass*p,const char * errmsg,...)
296{
297 T t = dynamic_cast<T>(p);
298 if (p && !t) {
299 va_list args;
300 va_start(args,errmsg);
301 fprintf(stderr,"A required dynamic_cast failed in: ");
302 vfprintf(stderr,errmsg,args);
303 fprintf(stderr,"\nwanted type \"%s\" but got \"%s\"\n",
304 typeid(T).name(),p->class_desc()->name());
305 fflush(stderr);
306 va_end(args);
307 abort();
308 }
309 return t;
310}
311
314template <class A>
316 public:
317 ForceLinkBase() {};
318 virtual ~ForceLinkBase() {};
319 virtual DescribedClass *create(A) = 0;
320};
321
331template <class T, class A = const Ref<KeyVal> &>
332class ForceLink: public ForceLinkBase<A> {
333 public:
334 ForceLink() {};
335 virtual ~ForceLink() {};
336 DescribedClass *create(A a) { return new T(a); }
337};
338
339
346std::string::size_type
347string_distance(const std::string& str1,
348 const std::string& str2);
349
350}
351
352#endif
353
354// Local Variables:
355// mode: c++
356// c-file-style: "CLJ"
357// End:
This class is used to contain information about classes.
Definition class.h:147
int version() const
Returns the version number of the class.
Definition class.h:197
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:195
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:233
ClassDesc * class_desc() const MPQC__NOEXCEPT
This returns the unique pointer to the ClassDesc corresponding to the given type_info object.
Ref< DescribedClass > ref()
Return this object wrapped up in a Ref smart pointer.
Definition class.h:251
const char * class_name() const
Return the name of the object's exact type.
int class_version() const
Return the version of the class.
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:46
This, together with ForceLink, is used to force code for particular classes to be linked into executa...
Definition class.h:315
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:56
Gives a list of parent classes of a class.
Definition class.h:75
The base class for all reference counted objects.
Definition ref.h:192
A template class that maintains references counts.
Definition ref.h:361
Restores fundamental and user-defined types from images created with StateOut.
Definition statein.h:79
Definition class.h:123
Contains all MPQC code up to version 3.
Definition mpqcin.h:14
T require_dynamic_cast(DescribedClass *p, const char *errmsg,...)
Attempt to cast a DescribedClass pointer to a DescribedClass descendent.
Definition class.h:274
std::string::size_type string_distance(const std::string &str1, const std::string &str2)
computes DamerauĞLevenshtein distance between two strings
ClassDesc * class_desc()
Return the ClassDesc corresponding to template argument.
Definition class.h:257
DescribedClass * create()
This is used to pass a function that make void constructor calls to the ClassDesc constructor.
Definition class.h:102
STL namespace.

Generated at Wed Sep 25 2024 02:45:30 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.12.0.