Otawa  0.10
Identifier.h
Go to the documentation of this file.
1 /*
2  * $Id$
3  * Identifier class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2003-09, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef OTAWA_PROP_IDENTIFIER_H
23 #define OTAWA_PROP_IDENTIFIER_H
24 
25 #include <elm/rtti.h>
26 #include <elm/meta.h>
27 #include <otawa/type.h>
28 #include <otawa/prop/Property.h>
29 #include <otawa/prop/PropList.h>
31 #include <otawa/prop/Ref.h>
32 #include <elm/sys/Path.h>
33 
34 namespace otawa {
35 
36 using namespace elm;
37 using namespace elm::io;
38 
39 // External class
40 class PropList;
41 
42 
43 namespace p {
44  template <class T> inline void print(Output& out, const T& v) { out << v; }
45  template <class T> inline void print(Output& out, T *p) { if(!p) out << "<null>"; else out << p; }
46 }
47 
48 
49 // GenericIdentifier class
50 template <class T>
52 public:
53 
54  // Constructors
56  inline Identifier(cstring name, const T& default_value)
57  : AbstractIdentifier(name), def(default_value) { }
58 
59  inline Identifier(cstring name, const T& default_value, Property *prop, ...)
60  : AbstractIdentifier(name), def(default_value)
61  { VARARG_BEGIN(args, prop); initProps(prop, args); VARARG_END }
62 
63  inline Identifier(cstring name, const T& default_value, Property *prop, VarArg& args)
64  : AbstractIdentifier(name, prop, args), def(default_value) { }
65 
66  // intrinsic accessor
67  inline const T& defaultValue(void) const { return def; }
68 
69  // PropList& Accessors
70  inline void add(PropList& list, const T& value) const;
71  inline void set(PropList& list, const T& value) const;
72  inline elm::Option<T> get(const PropList& list) const;
73  inline const T& get(const PropList& list, const T& def) const;
74  inline T& ref(PropList& list) const;
75  inline const T& use(const PropList& list) const;
76  inline const T& value(const PropList& list) const;
77  inline Ref<T, Identifier> value(PropList& list) const;
78  inline void remove(PropList& list) const { list.removeProp(this); }
79  inline bool exists(PropList& list) const { return list.getProp(this); }
80  inline void copy(PropList& list, Property *prop)
81  { list.addProp(GenericProperty<T>::make(this, get(prop))); }
82 
83  // PropList* Accessors
84  inline void add(PropList *list, const T& value) const { add(*list, value); }
85  inline void set(PropList *list, const T& value) const { set(*list, value); }
86  inline elm::Option<T> get(const PropList *list) const { return get(*list); }
87  inline const T& get(const PropList *list, const T& def) const { return get(*list, def); }
88  inline T& ref(PropList *list) const { return ref(*list); }
89  inline const T& use(const PropList *list) const { return use(*list); }
90  inline const T& value(const PropList *list) const { return value(*list); }
91  inline Ref<T, Identifier<T> > value(PropList *list) const { return value(*list); }
92  inline void remove(PropList *list) const { list->removeProp(this); }
93  inline bool exists(PropList *list) const { return list->getProp(this); }
94  inline void copy(PropList *list, Property *prop) { copy(*list, prop); }
95 
96  // Property accessors
97  inline const T& get(const Property *prop) const
98  { return static_cast<const GenericProperty<T> *>(prop)->value(); }
99  inline void set(Property *prop, const T& value) const
100  { static_cast<GenericProperty<T> *>(prop)->value() = value; }
101 
102  // Operators
103  inline const T& operator()(const PropList& props) const { return value(props); }
104  inline const T& operator()(const PropList *props) const { return value(props); }
105  inline Ref<T, Identifier<T> > operator()(PropList& props) const { return value(props); }
106  inline Ref<T, Identifier<T> > operator()(PropList *props) const { return value(props); }
107  inline const T& operator()(Property *prop) const { return get(prop); }
108 
109  // Identifier overload
110  virtual void print(elm::io::Output& out, const Property *prop) const
111  { p::print(out, !prop ? def : get(*prop)); }
112  virtual void printFormatted(io::Output& out, const Property *prop) const
113  { p::print(out, !prop ? def : get(*prop)); }
114  virtual const Type& type(void) const { return otawa::type<T>(); }
115  virtual void fromString(PropList& props, const string& str) const;
116  virtual Property *copy(Property& prop) const
117  { return GenericProperty<T>::make(this, get(&prop)); }
118 
119  // Getter class
120  class Getter: public PreIterator<Getter, const T&> {
121  public:
122  inline Getter(const PropList *list, Identifier<T>& id): getter(list, id) { }
123  inline Getter(const PropList& list, Identifier<T>& id): getter(list, id) { }
124  inline const T& item(void) const { return ((GenericProperty<T> *)*getter)->value(); }
125  inline bool ended(void) const { return getter.ended(); }
126  inline void next(void) { getter.next(); }
127  private:
129  };
130 
131 private:
132  T def;
133  inline const T& get(const Property& prop) const;
134 
135  typedef struct {
136  static inline void scan(const Identifier<T>& id, PropList& props, VarArg& args);
137  } __class;
138 
139  typedef struct {
140  static inline void scan(const Identifier<T>& id, PropList& props, VarArg& args);
141  } __simple;
142 };
143 
144 
145 // Inlines
146 template <class T>
147 inline void Identifier<T>::add(PropList& list, const T& value) const {
148  list.addProp(GenericProperty<T>::make(this, value));
149 }
150 
151 template <class T>
152 inline const T& Identifier<T>::get(const Property& prop) const {
153  return ((const GenericProperty<T> &)prop).value();
154 }
155 
156 template <class T>
157 inline void Identifier<T>::set(PropList& list, const T& value) const {
158  Property *p = list.getProp(this);
159  if(p == 0)
160  add(list, value);
161  else
162  set(p, value);
163 }
164 
165 template <class T>
166 inline elm::Option<T> Identifier<T>::get(const PropList& list) const {
167  Property *prop = list.getProp(this);
168  if(!prop)
169  return none;
170  else
171  return get(prop);
172 }
173 
174 template <class T>
175 inline const T& Identifier<T>::get(const PropList& list, const T& def) const {
176  Property *prop = list.getProp(this);
177  if(!prop)
178  return def;
179  else
180  return ((GenericProperty<T> *)prop)->value();
181 }
182 
183 template <class T>
184 inline const T& Identifier<T>::use(const PropList& list) const {
185  Property *prop = list.getProp(this);
186  ASSERT(prop);
187  return ((GenericProperty<T> *)prop)->value();
188 }
189 
190 template <class T>
191 inline const T& Identifier<T>::value(const PropList& list) const {
192  Property *prop = list.getProp(this);
193  if(!prop)
194  return def;
195  else
196  return ((GenericProperty<T> *)prop)->value();
197 }
198 
199 template <class T>
200 inline class Ref<T, Identifier<T> > Identifier<T>::value(PropList& list) const
201  { return Ref<T, Identifier<T> >(list, *this); }
202 
203 template <class T>
204 inline T& Identifier<T>::ref(PropList& list) const {
205  GenericProperty<T> *_prop = (GenericProperty<T> *)list.getProp(this);
206  if(!_prop) {
207  _prop = GenericProperty<T>::make(this, def);
208  list.addProp(_prop);
209  }
210  return _prop->value();
211 }
212 
213 
214 // Identifier<T>::printFormatted specializations
215 template <> void Identifier<char>::printFormatted(io::Output& out, const Property *prop) const;
216 template <> void Identifier<string>::printFormatted(io::Output& out, const Property *prop) const;
217 template <> void Identifier<cstring>::printFormatted(io::Output& out, const Property *prop) const;
218 template <> void Identifier<PropList>::printFormatted(io::Output& out, const Property *prop) const;
219 template <> void Identifier<const PropList *>::printFormatted(io::Output& out, const Property *prop) const;
220 
221 
222 // Identifier<T>::scan Specializations
223 template <class T>
225 PropList& props, VarArg& args) {
226  T *ptr = args.next<T *>();
227  id.set(props, *ptr);
228 }
229 
230 template <class T>
231 inline void Identifier<T>::__simple::scan(const Identifier<T>& id, PropList& props, VarArg& args) {
232  T ptr = args.next<T>();
233  id.set(props, ptr);
234 }
235 
236 /*template <class T>
237 void Identifier<T>::scan(PropList& props, VarArg& args) const {
238  _if<type_info<T>::is_scalar || type_info<T>::is_ptr, __simple, __class>
239  ::_::scan(*this, props, args);
240 }
241 
242 template <> void Identifier<elm::CString>::scan(PropList& props, VarArg& args) const;
243 template <> void Identifier<cstring>::scan(PropList& props, VarArg& args) const;
244 template <> void Identifier<elm::String>::scan(PropList& props, VarArg& args) const;*/
245 
246 
247 // GenericIdentifier<T>::fromString
248 template <class T> inline void Identifier<T>::fromString(PropList& props, const string& str) const
249  { T v; StringInput in(str); in >> v; set(props, v); }
250 // { throw io::IOException("type not supported for Identifier::fromString() call"); }
251 template <> void Identifier<bool>::fromString(PropList& props, const string& str) const;
252 template <> void Identifier<int>::fromString(PropList& props, const string& str) const;
253 template <> void Identifier<unsigned int>::fromString(PropList& props, const string& str) const;
254 template <> void Identifier<long>::fromString(PropList& props, const string& str) const;
255 template <> void Identifier<unsigned long>::fromString(PropList& props, const string& str) const;
256 template <> void Identifier<long long>::fromString(PropList& props, const string& str) const;
257 template <> void Identifier<unsigned long long>::fromString(PropList& props, const string& str) const;
258 template <> void Identifier<double>::fromString(PropList& props, const string& str) const;
259 template <> void Identifier<string>::fromString(PropList& props, const string& str) const;
260 template <> void Identifier<Address>::fromString(PropList& props, const string& str) const;
261 template <> void Identifier<elm::sys::Path>::fromString(PropList& props, const string& str) const;
262 
263 } // otawa
264 
265 #endif // OTAWA_PROP_IDENTIFIER_H
static void scan(const Identifier< T > &id, PropList &props, VarArg &args)
Definition: Identifier.h:224
void set(PropList *list, const T &value) const
Set the value of a generic property with the current identifier to the given list.
Definition: Identifier.h:85
Identifier(cstring name, const T &default_value, Property *prop, VarArg &args)
Definition: Identifier.h:63
virtual const Type & type(void) const
Get the identifier of data linked with this property.
Definition: Identifier.h:114
const T & defaultValue(void) const
Definition: Identifier.h:67
bool ended(void) const
Definition: Identifier.h:125
This class is used for accessing all properties of property list with a given identifier.
Definition: PropList.h:111
PropList::Getter getter
Definition: Identifier.h:128
const T & value(const PropList *list) const
For internal use only.
Definition: Identifier.h:90
Getter(const PropList *list, Identifier< T > &id)
Definition: Identifier.h:122
const T & operator()(const PropList *props) const
Read the value in a functional way.
Definition: Identifier.h:104
This generic class allows attaching any type of data to a property.
Definition: Property.h:20
Getter(const PropList &list, Identifier< T > &id)
Definition: Identifier.h:123
sys::SystemInStream & in
const T & use(const PropList *list) const
Get the value matching the current identifier in the given list.
Definition: Identifier.h:89
const T & operator()(Property *prop) const
Definition: Identifier.h:107
void next(void)
Definition: Identifier.h:126
Represents a unique identifier used by the annotation system.
Definition: AbstractIdentifier.h:32
T def
Definition: Identifier.h:132
Identifier(cstring name)
Build a new generic identifier.
Definition: Identifier.h:55
Definition: Identifier.h:120
const T & use(const PropList &list) const
Get the value matching the current identifier in the given list.
Definition: Identifier.h:184
value_t value(CString name, int value)
void set(Property *prop, const T &value) const
Definition: Identifier.h:99
Definition: Identifier.h:135
T & ref(PropList *list) const
Definition: Identifier.h:88
Ref< T, Identifier< T > > value(PropList *list) const
For internal use only.
Definition: Identifier.h:91
bool exists(PropList &list) const
Test if the given list contains a property with the current identifier.
Definition: Identifier.h:79
const T & value(const PropList &list) const
For internal use only.
Definition: Identifier.h:191
Identifier(cstring name, const T &default_value, Property *prop,...)
Definition: Identifier.h:59
const int use
Definition: Registration.h:46
void set(PropList &list, const T &value) const
Set the value of a generic property with the current identifier to the given list.
Definition: Identifier.h:157
bool exists(PropList *list) const
Test if the given list contains a property with the current identifier.
Definition: Identifier.h:93
virtual void print(elm::io::Output &out, const Property *prop) const
Print the value of the given property (accordint the property matches the given identifier).
Definition: Identifier.h:110
Definition: Identifier.h:139
virtual void fromString(PropList &props, const string &str) const
Get value of an identifier from a string and store it in the given property list. ...
Definition: Identifier.h:248
void addProp(Property *prop)
Add property to the list without checking of duplication.
Definition: prop_PropList.cpp:537
elm::Option< T > get(const PropList &list) const
Definition: Identifier.h:166
sys::SystemOutStream & out
virtual void printFormatted(io::Output &out, const Property *prop) const
Print the value of the given property in a formatted way, that is, possibly to perform re-scanning af...
Definition: Identifier.h:112
void add(PropList &list, const T &value) const
Add a generic property to the given list with the current identifier.
Definition: Identifier.h:147
Ref< T, Identifier< T > > operator()(PropList &props) const
Read or write a property value in a functional way.
Definition: Identifier.h:105
inst add(int d, int a, int b)
Definition: inst.h:163
This class represents identifier with a typed associated value.
Definition: Identifier.h:51
virtual Property * copy(Property &prop) const
Build a copy of the given property.
Definition: Identifier.h:116
void copy(PropList &list, Property *prop)
Definition: Identifier.h:80
cstring name
Definition: odisasm.cpp:107
void copy(PropList *list, Property *prop)
Definition: Identifier.h:94
T & ref(PropList &list) const
Definition: Identifier.h:204
static void scan(const Identifier< T > &id, PropList &props, VarArg &args)
Definition: Identifier.h:231
void add(PropList *list, const T &value) const
Add a generic property to the given list with the current identifier.
Definition: Identifier.h:84
const T & value(void) const
Get the value of the property.
Definition: Property.h:33
const T & item(void) const
Definition: Identifier.h:124
This a list of properties.
Definition: PropList.h:63
T next(void)
const T & operator()(const PropList &props) const
Read the value in a functional way.
Definition: Identifier.h:103
const OptionalNone none
Property * getProp(const AbstractIdentifier *id) const
Find a property by its identifier.
Definition: prop_PropList.cpp:357
A property associates a data with an identifier.
Definition: PropList.h:42
This class describes the type of the data in the program.
Definition: type.h:39
static GenericProperty< T > * make(const AbstractIdentifier *id, const T &value)
Build a new generic property with the given data.
Definition: Property.h:31
void print(Output &out, const T &v)
Definition: Identifier.h:44
Ref< T, Identifier< T > > operator()(PropList *props) const
Read or write a property value in a functional way.
Definition: Identifier.h:106
Identifier(cstring name, const T &default_value)
Build a new generic identifier.
Definition: Identifier.h:56