Otawa  0.10
Ref.h
Go to the documentation of this file.
1 /*
2  * $Id$
3  * Ref class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2009, 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_REF_H_
23 #define OTAWA_PROP_REF_H_
24 
25 #include <otawa/prop/PropList.h>
26 
27 namespace otawa {
28 
29 // ImmutableRef class
30 template <class T, class I>
31 class ImmutableRef {
32 public:
33  inline ImmutableRef(const PropList& _prop, const I& _id): prop(_prop), id(_id) { }
34  inline ImmutableRef(const PropList *_prop, const I& _id): prop(*_prop), id(_id) { }
35  inline ImmutableRef(const ImmutableRef<T, I>& ref): prop(ref.prop), id(ref.id) { }
36  inline const PropList& proplist(void) const { return prop; }
37  inline const I& identifier(void) const { return id; }
38 
39  // accessors
40  inline bool exists(void) const { return prop.hasProp(id); }
41  inline const T& get(void) const { return id.value(prop); }
42  inline void print(io::Output& out) const { id.print(out, prop.getProp(&id)); }
43  inline operator const T&(void) const { return id.get(prop, id.defaultValue()); }
44  inline T operator->(void) const { return id.get(prop, id.defaultValue()); }
45 
46 protected:
47  const PropList& prop;
48  const I& id;
49 };
50 
51 
52 // Ref class
53 template <class T, class I>
54 class Ref: public ImmutableRef<T, I> {
55 public:
56  inline Ref(PropList& _prop, const I& _id): ImmutableRef<T, I>(_prop, _id) { }
57  inline Ref(PropList *_prop, const I& _id): ImmutableRef<T, I>(_prop, _id) { }
58  inline Ref(const Ref<T, I>& ref): ImmutableRef<T, I>(ref) { }
59 
60  // accessors
61  inline PropList& props(void) const { return const_cast<PropList&>(ImmutableRef<T, I>::proplist()); }
62  inline const I& id(void) const { return ImmutableRef<T, I>::identifier(); }
63 
64  // mutators
65  inline const Ref& add(const T& value) const { id().add(props(), value); return *this; }
66  inline void remove(void) const { props().removeProp(id()); }
67  inline T& ref(void) const { return id().ref(props()); }
68 
69  inline T& operator*(void) const { return ref(); }
70  inline const Ref<T, I>& operator=(const T& value) const { id().set(props(), value); return *this; }
71  inline Ref<T, I>& operator=(const Ref<T, I>& value) { id().set(props(), value.get()); return *this; }
72  inline Ref<T, I>& operator+=(const T& v) { ref() += v; return *this; }
73  inline Ref<T, I>& operator-=(const T& v) { ref() -= v; return *this; }
74  inline Ref<T, I>& operator*=(const T& v) { ref() *= v; return *this; }
75  inline Ref<T, I>& operator/=(const T& v) { ref() /= v; return *this; }
76  inline Ref<T, I>& operator%=(const T& v) const { ref() %= v; return *this; }
77  inline Ref<T, I>& operator&=(const T& v) const { ref() &= v; return *this; }
78  inline Ref<T, I>& operator|=(const T& v) const { ref() |= v; return *this; }
79  inline Ref<T, I>& operator^=(const T& v) const { ref() ^= v; return *this; }
80  inline Ref<T, I>& operator<<=(const T& v) const { ref() <<= v; return *this; }
81  inline Ref<T, I>& operator>>=(const T& v) const { ref() >>= v; return *this; }
82  inline Ref<T, I>& operator++(void) const { ref()++; return *this; }
83  inline Ref<T, I>& operator--(void) const { ref()--; return *this; }
84  inline Ref<T, I>& operator++(int) const { ref()++; return *this; }
85  inline Ref<T, I>& operator--(int) const { ref()--; return *this; }
86 };
87 
88 // output
89 template <class T, class I>
90 io::Output& operator<<(io::Output& out, const Ref<T, I>& ref) { ref.print(out); return out; }
91 template <class T, class I>
92 io::Output& operator<<(io::Output& out, const ImmutableRef<T, I>& ref) { ref.print(out); return out; }
93 
94 } // otawa
95 
96 #endif /* OTAWA_PROP_REF_H_ */
Ref(const Ref< T, I > &ref)
Definition: Ref.h:58
const Ref & add(const T &value) const
Definition: Ref.h:65
ImmutableRef(const ImmutableRef< T, I > &ref)
Definition: Ref.h:35
Ref(PropList &_prop, const I &_id)
Definition: Ref.h:56
const I & identifier(void) const
Definition: Ref.h:37
Ref< T, I > & operator*=(const T &v)
Definition: Ref.h:74
Ref< T, I > & operator-=(const T &v)
Definition: Ref.h:73
Ref< T, I > & operator+=(const T &v)
Definition: Ref.h:72
void print(io::Output &out) const
Definition: Ref.h:42
Ref< T, I > & operator^=(const T &v) const
Definition: Ref.h:79
Ref< T, I > & operator/=(const T &v)
Definition: Ref.h:75
bool hasProp(const AbstractIdentifier &id) const
Test if the property list contains a property matching the given identifier.
Definition: PropList.h:81
PropList & props(void) const
Definition: Ref.h:61
bool exists(void) const
Definition: Ref.h:40
T & ref(void) const
Definition: Ref.h:67
ImmutableRef(const PropList &_prop, const I &_id)
Definition: Ref.h:33
T operator->(void) const
Definition: Ref.h:44
Ref< T, I > & operator=(const Ref< T, I > &value)
Definition: Ref.h:71
Ref< T, I > & operator<<=(const T &v) const
Definition: Ref.h:80
const I & id
Definition: Ref.h:48
Ref(PropList *_prop, const I &_id)
Definition: Ref.h:57
const Ref< T, I > & operator=(const T &value) const
Definition: Ref.h:70
T & operator*(void) const
Definition: Ref.h:69
Ref< T, I > & operator%=(const T &v) const
Definition: Ref.h:76
ImmutableRef(const PropList *_prop, const I &_id)
Definition: Ref.h:34
const PropList & prop
Definition: Ref.h:47
Definition: Ref.h:31
Ref< T, I > & operator--(int) const
Definition: Ref.h:85
Ref< T, I > & operator++(void) const
Definition: Ref.h:82
Ref< T, I > & operator--(void) const
Definition: Ref.h:83
sys::SystemOutStream & out
const I & id(void) const
Definition: Ref.h:62
Ref< T, I > & operator|=(const T &v) const
Definition: Ref.h:78
void removeProp(const AbstractIdentifier *id)
Remove a property matching the given identifier.
Definition: prop_PropList.cpp:409
This a list of properties.
Definition: PropList.h:63
const PropList & proplist(void) const
Definition: Ref.h:36
Ref< T, I > & operator++(int) const
Definition: Ref.h:84
Property * getProp(const AbstractIdentifier *id) const
Find a property by its identifier.
Definition: prop_PropList.cpp:357
Ref< T, I > & operator>>=(const T &v) const
Definition: Ref.h:81
Ref< T, I > & operator&=(const T &v) const
Definition: Ref.h:77