Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Type.h
1 /*
2  * Type class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2007, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_RTTI_TYPE_H_
22 #define ELM_RTTI_TYPE_H_
23 
24 #include <elm/data/HashMap.h>
25 #include <elm/data/List.h>
26 #include <elm/ptr.h>
27 #include <elm/string.h>
28 #include <elm/util/Initializer.h>
29 
30 namespace elm {
31 
32 namespace serial2 {
33  class Serializer;
34  class Unserializer;
35 };
36 
37 namespace rtti {
38 
39 class AbstractClass;
40 class Type;
41 
42 class Enumerable {
43 public:
44  virtual ~Enumerable(void);
45  virtual const Type& type(void) const = 0;
46  virtual int valueFor(string text) const = 0;
47  virtual cstring nameFor(int value) const = 0;
48 };
49 
50 class Serializable {
51 public:
52  virtual ~Serializable(void);
53  virtual const Type& type(void) const = 0;
54  virtual void *instantiate(void) const = 0;
55  virtual void serialize(serial2::Serializer& ser, const void *data) const = 0;
56  virtual void unserialize(serial2::Unserializer& uns, void *data) const = 0;
57 };
58 
59 class TemplateType {
60 public:
61  virtual ~TemplateType(void);
62  virtual int count(void) const = 0;
63 };
64 
65 class ParamType;
66 
67 class InstanceType {
68 public:
69  virtual ~InstanceType(void);
70  virtual const Type& templ(void) const = 0;
71  virtual const List<const Type *> params(void) const = 0;
72  const Type& typeFor(const Type& param) const;
73 };
74 
75 class ParamType {
76 public:
77  virtual ~ParamType(void);
78  virtual int index(void) const = 0;
79 };
80 
81 class PointerType;
82 
83 class Type {
84 public:
86  static const Type *get(string name);
87  static TypeIter types(void);
88 
89  Type(string name = "");
90  virtual ~Type(void);
91  inline string name(void) const { return _name; }
92  const PointerType& pointer(void) const;
93  virtual bool canCast(const Type *t) const;
94 
95  virtual bool isVoid(void) const;
96  virtual bool isBool(void) const;
97  virtual bool isInt(void) const;
98  virtual bool isFloat(void) const;
99 
100  virtual bool isPtr(void) const;
101  virtual const PointerType& asPtr(void) const;
102  virtual bool isClass(void) const;
103  virtual const AbstractClass& asClass(void) const;
104  virtual bool isEnum(void) const;
105  virtual const Enumerable& asEnum(void) const;
106  virtual bool isSerial(void) const;
107  virtual const Serializable& asSerial(void) const;
108 
109  virtual const TemplateType *asTemplate(void) const;
110  virtual const InstanceType *asInstance(void) const;
111  virtual const ParamType *asParam(void) const;
112  static const Type &param0, &param1, &param2, &param3;
113 
114  void initialize(void);
115  inline bool operator==(const Type& t) const { return this == &t; }
116  inline bool operator!=(const Type& t) const { return !operator==(t); }
117 private:
118  string _name;
119  mutable UniquePtr<PointerType>_pointer;
120  static Initializer<Type> _init;
121 };
122 
123 class PointerType: public Type {
124 public:
125  PointerType(const Type& to);
126  virtual bool isPtr(void) const override;
127  virtual const PointerType& asPtr(void) const override;
128  const Type& to(void) const { return _to; }
129 private:
130  const Type& _to;
131 };
132 
133 io::Output& operator<<(io::Output& out, const Type& type);
134 
135 } } // otawa::rtti
136 
137 #endif /* ELM_RTTI_TYPE_H_ */
elm::rtti::PointerType::asPtr
virtual const PointerType & asPtr(void) const override
Definition: rtti.cpp:207
elm::rtti::Type::asTemplate
virtual const TemplateType * asTemplate(void) const
Definition: rtti.cpp:473
elm::t::out
typename type_info< T >::out_t out
Definition: type_info.h:284
elm::rtti::InstanceType::~InstanceType
virtual ~InstanceType(void)
Definition: rtti.cpp:565
elm::rtti::Type::name
string name(void) const
Definition: Type.h:91
elm::rtti::Type::isFloat
virtual bool isFloat(void) const
Definition: rtti.cpp:387
elm::rtti::Type::~Type
virtual ~Type(void)
Definition: rtti.cpp:332
elm::rtti::Serializable::instantiate
virtual void * instantiate(void) const =0
elm::rtti::Type::asPtr
virtual const PointerType & asPtr(void) const
Definition: rtti.cpp:423
elm::rtti::Type::isInt
virtual bool isInt(void) const
Definition: rtti.cpp:379
elm::UniquePtr
Definition: UniquePtr.h:27
elm::rtti::Type::TypeIter
HashMap< string, const Type * >::Iter TypeIter
Definition: Type.h:85
elm::rtti::Type::isEnum
virtual bool isEnum(void) const
Definition: rtti.cpp:434
elm::rtti::PointerType
Definition: Type.h:123
elm::rtti::Type::asInstance
virtual const InstanceType * asInstance(void) const
Definition: rtti.cpp:482
elm::rtti::Type::asParam
virtual const ParamType * asParam(void) const
Definition: rtti.cpp:491
elm::rtti::Enumerable
Definition: Type.h:42
elm::rtti::ParamType::index
virtual int index(void) const =0
elm::rtti::Type::param2
static const Type & param2
Definition: Type.h:112
elm::rtti::Enumerable::type
virtual const Type & type(void) const =0
elm::rtti::Type::initialize
void initialize(void)
Definition: rtti.cpp:306
elm::rtti::ParamType
Definition: Type.h:75
elm::rtti::Type::param1
static const Type & param1
Definition: Type.h:112
elm::rtti::operator<<
io::Output & operator<<(io::Output &out, const Type &type)
Definition: rtti.cpp:1414
elm::rtti::Serializable
Definition: Type.h:50
elm::rtti::Type::param0
static const Type & param0
Definition: Type.h:112
elm::rtti::Type::asClass
virtual const AbstractClass & asClass(void) const
Definition: rtti.cpp:413
elm::rtti::ParamType::~ParamType
virtual ~ParamType(void)
Definition: rtti.cpp:504
value
elm::CString
Definition: CString.h:17
elm::rtti::Type::param3
static const Type & param3
Definition: Type.h:112
elm::rtti::Enumerable::~Enumerable
virtual ~Enumerable(void)
Definition: rtti.cpp:226
elm::rtti::PointerType::to
const Type & to(void) const
Definition: Type.h:128
elm::rtti::Type::isSerial
virtual bool isSerial(void) const
Definition: rtti.cpp:453
elm::rtti::Type::Type
Type(string name="")
Definition: rtti.cpp:299
elm::rtti::Type::canCast
virtual bool canCast(const Type *t) const
Definition: rtti.cpp:354
elm::rtti::Serializable::~Serializable
virtual ~Serializable(void)
Definition: rtti.cpp:259
elm::rtti::TemplateType::count
virtual int count(void) const =0
elm
Definition: adapter.h:26
elm::serial2::Serializer
Definition: Serializer.h:36
elm::rtti::Type::asSerial
virtual const Serializable & asSerial(void) const
Definition: rtti.cpp:463
elm::rtti::Type::get
static const Type * get(string name)
Definition: rtti.cpp:316
elm::rtti::Serializable::serialize
virtual void serialize(serial2::Serializer &ser, const void *data) const =0
elm::rtti::PointerType::isPtr
virtual bool isPtr(void) const override
Definition: rtti.cpp:203
elm::rtti::TemplateType
Definition: Type.h:59
elm::rtti::Serializable::unserialize
virtual void unserialize(serial2::Unserializer &uns, void *data) const =0
elm::rtti::Type::pointer
const PointerType & pointer(void) const
Definition: rtti.cpp:338
elm::rtti::InstanceType::params
virtual const List< const Type * > params(void) const =0
elm::rtti::TemplateType::~TemplateType
virtual ~TemplateType(void)
Definition: rtti.cpp:548
elm::rtti::Type::isVoid
virtual bool isVoid(void) const
Definition: rtti.cpp:363
elm::HashMap::Iter
Definition: HashMap.h:98
elm::rtti::Enumerable::nameFor
virtual cstring nameFor(int value) const =0
elm::serial2::Unserializer
Definition: Unserializer.h:15
elm::rtti::Serializable::type
virtual const Type & type(void) const =0
elm::Initializer
Definition: Initializer.h:14
elm::rtti::InstanceType::templ
virtual const Type & templ(void) const =0
elm::rtti::InstanceType::typeFor
const Type & typeFor(const Type &param) const
Definition: rtti.cpp:587
elm::rtti::InstanceType
Definition: Type.h:67
elm::rtti::Type::operator!=
bool operator!=(const Type &t) const
Definition: Type.h:116
elm::rtti::AbstractClass
Definition: Class.h:289
elm::rtti::Type::asEnum
virtual const Enumerable & asEnum(void) const
Definition: rtti.cpp:443
elm::rtti::Type::isBool
virtual bool isBool(void) const
Definition: rtti.cpp:371
elm::rtti::Type::isClass
virtual bool isClass(void) const
Definition: rtti.cpp:404
elm::io::Output
Definition: Output.h:179
elm::rtti::PointerType::PointerType
PointerType(const Type &to)
Definition: rtti.cpp:198
elm::rtti::Type::types
static TypeIter types(void)
Definition: rtti.cpp:325
elm::List
Definition: List.h:34
elm::rtti::Type
Definition: Type.h:83
elm::rtti::Type::isPtr
virtual bool isPtr(void) const
Definition: rtti.cpp:395
elm::rtti::Enumerable::valueFor
virtual int valueFor(string text) const =0
elm::rtti::Type::operator==
bool operator==(const Type &t) const
Definition: Type.h:115