Otawa  0.10
PropList.h
Go to the documentation of this file.
1 /*
2  * PropList class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2003-8, 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 OTAWA_PROP_PROPLIST_H
22 #define OTAWA_PROP_PROPLIST_H
23 
24 #include <elm/utility.h>
25 #include <elm/PreIterator.h>
26 #include <elm/util/VarArg.h>
27 
28 namespace elm { template <class T> class Initializer; }
29 
30 namespace otawa {
31 
32 // pre-declaration
33 class AbstractIdentifier;
34 class Type;
35 
36 
37 // Constants
38 extern const AbstractIdentifier END;
39 
40 
41 // Property description
42 class Property {
43  friend class PropList;
44  mutable Property *_next;
46 protected:
47  virtual ~Property(void) { };
48  virtual Property *copy(void) { return new Property(_id); };
49 public:
51  inline Property(const AbstractIdentifier *id): _id(id) { };
52  inline Property(const AbstractIdentifier& id): _id(&id) { };
53  inline Property(elm::CString name): _id(getID(name)) { };
54  inline const AbstractIdentifier *id(void) const { return _id; };
55  inline Property *next(void) const { return _next; };
56  template <class T> inline const T& get(void) const;
57  template <class T> inline void set(const T& value);
58  virtual void print(elm::io::Output& out) const;
59 };
60 
61 
62 // PropList class
63 class PropList {
64  mutable Property *head;
65 public:
66  static const PropList EMPTY;
67  inline PropList(const PropList& props): head(0) { addProps(props); };
68  inline PropList(void): head(0) { };
69  inline ~PropList(void) { clearProps(); };
70 
71  // Property access
72  Property *getProp(const AbstractIdentifier *id) const;
73  void setProp(Property *prop);
74  void removeProp(const AbstractIdentifier *id);
75  inline void removeProp(const AbstractIdentifier& id) { removeProp(&id); }
77  inline Property *extractProp(const AbstractIdentifier& id) { return extractProp(&id); }
78  inline void setProp(const AbstractIdentifier *id) { setProp(new Property(id)); };
79  void addProp(Property *prop);
80  void removeAllProp(const AbstractIdentifier *id);
81  inline bool hasProp(const AbstractIdentifier& id) const
82  { return getProp(&id) != 0; }
83 
84  // Global management
85  void clearProps(void);
86  void addProps(const PropList& props);
87  void print(elm::io::Output& out) const;
88  inline PropList& operator=(const PropList& props)
89  { clearProps(); addProps(props); return *this; }
90 
91  // Iter class
92  class Iter: public elm::PreIterator<Iter, Property *> {
94  public:
95  inline Iter(const PropList& list): prop(list.head) { }
96  inline Iter(const PropList *list): prop(list->head) { }
97  inline void next(void) { ASSERT(prop); prop = prop->next(); }
98  inline bool ended(void) const { return prop == 0; }
99  inline Property *item(void) const { ASSERT(prop); return prop; }
100  inline bool operator==(const AbstractIdentifier *id) const
101  { return item()->id() == id; }
102  inline bool operator!=(const AbstractIdentifier *id) const
103  { return item()->id() != id; }
104  inline bool operator==(const AbstractIdentifier& id) const
105  { return item()->id() == &id; }
106  inline bool operator!=(const AbstractIdentifier& id) const
107  { return item()->id() != &id; }
108  };
109 
110  // Getter class
111  class Getter: public elm::PreIterator<Getter, Property *> {
112  public:
113  inline Getter(const PropList *list, const AbstractIdentifier& id)
114  : iter(*list), _id(id) { look(); }
115  inline Getter(const PropList& list, const AbstractIdentifier& id)
116  : iter(list), _id(id) { look(); }
117  inline bool ended(void) const { return iter.ended(); }
118  inline Property *item(void) const { return iter.item(); }
119  inline void next(void) { iter.next(); look(); }
120  private:
123  inline void look(void)
124  { for(; iter; iter++) if(iter->id() == &_id) return; }
125  };
126 
127 };
128 
129 
130 // output
132  props.print(out);
133  return out;
134 }
135 
137  props->print(out);
138  return out;
139 }
140 
141 }; // otawa
142 
143 #endif // OTAWA_PROP_PROPLIST_H
~PropList(void)
Definition: PropList.h:69
PropList(void)
Build an empty property list.
Definition: PropList.h:68
void setProp(Property *prop)
Set the property in the property list removing any double.
Definition: prop_PropList.cpp:379
void next(void)
Go to the next property.
Definition: PropList.h:119
This class is used for accessing all properties of property list with a given identifier.
Definition: PropList.h:111
virtual void print(elm::io::Output &out) const
Print the given property, that is, the identifier and its value if any.
Definition: prop_PropList.cpp:289
static const PropList EMPTY
This is an empty proplist for convenience.
Definition: PropList.h:66
const AbstractIdentifier * id(void) const
Get the identifier code of the property.
Definition: PropList.h:54
PropList(const PropList &props)
Initialize a property list from a sequence of (identifier, value) pairs passed as variable arguments...
Definition: PropList.h:67
bool ended(void) const
Test if there is still a property to examine.
Definition: PropList.h:117
Property * next(void) const
Get the next property.
Definition: PropList.h:55
bool operator==(const AbstractIdentifier &id) const
Equality overload for testing if a property is equals to an identifier.
Definition: PropList.h:104
Iter iter
Definition: PropList.h:121
static const AbstractIdentifier * getID(elm::CString name)
Allocate a new identifier matching the given name.
Definition: prop_PropList.cpp:245
Getter(const PropList &list, const AbstractIdentifier &id)
Build an iterator on properties matching the given name.
Definition: PropList.h:115
bool hasProp(const AbstractIdentifier &id) const
Test if the property list contains a property matching the given identifier.
Definition: PropList.h:81
Property * prop
Definition: PropList.h:93
elm::io::Output & operator<<(elm::io::Output &out, Address addr)
Definition: base.cpp:188
void setProp(const AbstractIdentifier *id)
Definition: PropList.h:78
Represents a unique identifier used by the annotation system.
Definition: AbstractIdentifier.h:32
This iterator is used for reading all properties of a property list.
Definition: PropList.h:92
void look(void)
Definition: PropList.h:123
const AbstractIdentifier * _id
Definition: PropList.h:45
Iter(const PropList &list)
Build a property iterator.
Definition: PropList.h:95
bool operator==(const AbstractIdentifier *id) const
Equality overload for testing if a property is equals to an identifier.
Definition: PropList.h:100
Property * extractProp(const AbstractIdentifier &id)
Remove a property matching the given identifier and return it.
Definition: PropList.h:77
PropList & operator=(const PropList &props)
Definition: PropList.h:88
virtual ~Property(void)
Virtual destructor for allowing destruction of any stored data.
Definition: PropList.h:47
void addProp(Property *prop)
Add property to the list without checking of duplication.
Definition: prop_PropList.cpp:537
Property * extractProp(const AbstractIdentifier *id)
Remove a property matching the given identifier and return it.
Definition: prop_PropList.cpp:427
const AbstractIdentifier & _id
Definition: PropList.h:122
void removeProp(const AbstractIdentifier &id)
Definition: PropList.h:75
sys::SystemOutStream & out
void clearProps(void)
Remove all properties from the list.
Definition: prop_PropList.cpp:452
void print(elm::io::Output &out) const
Display the current property list.
Definition: prop_PropList.cpp:639
void set(const T &value)
Definition: Property.h:70
void removeProp(const AbstractIdentifier *id)
Remove a property matching the given identifier.
Definition: prop_PropList.cpp:409
Getter(const PropList *list, const AbstractIdentifier &id)
Build an iterator on properties matching the given name.
Definition: PropList.h:113
bool ended(void) const
Test if there is still a property to examine.
Definition: PropList.h:98
void next(void)
Go to the next property.
Definition: PropList.h:97
virtual Property * copy(void)
This method is called when a property is copied.
Definition: PropList.h:48
Iter(const PropList *list)
Build a property iterator.
Definition: PropList.h:96
cstring name
Definition: odisasm.cpp:107
bool operator!=(const AbstractIdentifier *id) const
Equality overload for testing if a property is equals to an identifier.
Definition: PropList.h:102
const AbstractIdentifier END
This identifier is used for marking the end of property list definition in variable arguments "...
Property(const AbstractIdentifier *id)
Build a new property with the given identifier.
Definition: PropList.h:51
Property * item(void) const
Get the current property.
Definition: PropList.h:118
This a list of properties.
Definition: PropList.h:63
void removeAllProp(const AbstractIdentifier *id)
Remove all the properties matching the given identifier.
Definition: prop_PropList.cpp:547
bool operator!=(const AbstractIdentifier &id) const
Equality overload for testing if a property is equals to an identifier.
Definition: PropList.h:106
Property * item(void) const
Get the current property.
Definition: PropList.h:99
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
Property * _next
Definition: PropList.h:44
Property(const AbstractIdentifier &id)
build a property from a static identifier.
Definition: PropList.h:52
Property * head
Definition: PropList.h:64
Property(elm::CString name)
Build a property with the name of its identifier.
Definition: PropList.h:53
void addProps(const PropList &props)
Add all properties from the given property list, in a reverse order.
Definition: prop_PropList.cpp:344