Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
delegate.h
1 /*
2  * $Id$
3  * delegate classes interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2007-08, 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 ELM_UTIL_DELEGATE_H
23 #define ELM_UTIL_DELEGATE_H
24 
25 #include <elm/assert.h>
26 #include <elm/util/Option.h>
27 #include <elm/util/Exception.h>
28 
29 namespace elm {
30 
31 // ArrayDelegate class
32 template <class C, class I, class T>
34 public:
35  inline ArrayDelegate(C& container, const I& identifier)
36  : cont(&container), id(identifier) { }
37  inline ArrayDelegate(const ArrayDelegate& delegate)
38  : cont(delegate.cont), id(delegate.id) { }
39 
40  inline operator T(void) const { return ((const C *)cont)->get(id); }
41  inline const T& operator*(void) const { return ((const C *)cont)->get(id); }
43  { cont->set(id, value); return *this; }
45  { cont = delegate.cont; id = delegate.id; return *this; }
46 
47 private:
48  C *cont;
49  I id;
50 };
51 
52 
53 // Default value
54 template <class T>
55 class Default {
56 public:
57  static T def;
58 };
59 template <class T> T Default<T>::def = 0;
60 
61 
62 // MapDelegate class
63 template <class C, class I, class T, class D = Default<T> >
64 class MapDelegate {
65 public:
66  inline MapDelegate(C& container, const I& identifier)
67  : cont(&container), id(identifier) { }
68  inline MapDelegate(const MapDelegate& delegate)
69  : cont(delegate.cont), id(delegate.id) { }
70 
71  inline operator T(void) const
72  { return ((const C *)cont)->get(id, D::def); }
73  inline const T& operator*(void) const
74  { return ((const C *)cont)->get(id, D::def); }
76  { cont->put(id, value); return *this; }
78  { cont = delegate.cont; id = delegate.id; return *this; }
79 
80 private:
81  C *cont;
82  I id;
83 };
84 
85 
86 // KeyException class
88 public:
89  string message(void) override;
90 };
91 
92 
93 // MapDelegate class
94 template <class C>
96 public:
98  typedef typename C::key_t key_t;
99  typedef typename C::val_t val_t;
100 
101  inline StrictMapDelegate(C& map, const key_t& key)
102  : _map(map), _key(key) { }
103 
104  inline val_t get(void) const {
105  Option<val_t> v = _map.get(_key);
106  if(v.isNone()) throw KeyException();
107  return *v;
108  }
109  inline void put(const val_t& val) { _map.put(_key, val); }
110 
111  inline operator val_t(void) const { return get(); }
112  inline val_t operator*(void) const { return get(); }
113  inline self_t& operator=(const val_t& val) { put(val); return *this; }
114 
115 private:
116  C& _map;
117  const key_t& _key;
118 };
119 
120 } // elm
121 
122 #endif /* ELM_UTIL_DELEGATE_H */
elm::Option
Definition: Option.h:35
elm::MapDelegate::operator*
const T & operator*(void) const
Definition: delegate.h:73
elm::ArrayDelegate::operator*
const T & operator*(void) const
Definition: delegate.h:41
elm::MapDelegate::operator=
MapDelegate< C, I, T > & operator=(const MapDelegate< C, I, T > &delegate)
Definition: delegate.h:77
elm::StrictMapDelegate::StrictMapDelegate
StrictMapDelegate(C &map, const key_t &key)
Definition: delegate.h:101
elm::ArrayDelegate::ArrayDelegate
ArrayDelegate(C &container, const I &identifier)
Definition: delegate.h:35
elm::StrictMapDelegate::operator=
self_t & operator=(const val_t &val)
Definition: delegate.h:113
elm::Exception
Definition: Exception.h:29
value
elm::StrictMapDelegate::get
val_t get(void) const
Definition: delegate.h:104
elm::map
void map(const C &c, const F &f, D &d)
Definition: util.h:89
elm::MapDelegate::MapDelegate
MapDelegate(C &container, const I &identifier)
Definition: delegate.h:66
elm::StrictMapDelegate::operator*
val_t operator*(void) const
Definition: delegate.h:112
elm::Option::isNone
bool isNone() const
Definition: Option.h:71
elm::ArrayDelegate::operator=
ArrayDelegate< C, I, T > & operator=(const T &value)
Definition: delegate.h:42
elm
Definition: adapter.h:26
elm::StrictMapDelegate::self_t
StrictMapDelegate< C > self_t
Definition: delegate.h:97
elm::ArrayDelegate
Definition: delegate.h:33
elm::MapDelegate::operator=
MapDelegate< C, I, T > & operator=(const T &value)
Definition: delegate.h:75
elm::KeyException
Definition: delegate.h:87
elm::StrictMapDelegate::val_t
C::val_t val_t
Definition: delegate.h:99
elm::StrictMapDelegate
Definition: delegate.h:95
elm::Default::def
static T def
Definition: delegate.h:57
elm::ArrayDelegate::ArrayDelegate
ArrayDelegate(const ArrayDelegate &delegate)
Definition: delegate.h:37
elm::KeyException::message
string message(void) override
Definition: utility.cpp:91
elm::StrictMapDelegate::put
void put(const val_t &val)
Definition: delegate.h:109
elm::MapDelegate::MapDelegate
MapDelegate(const MapDelegate &delegate)
Definition: delegate.h:68
elm::MapDelegate
Definition: delegate.h:64
elm::StrictMapDelegate::key_t
C::key_t key_t
Definition: delegate.h:98
elm::ArrayDelegate::operator=
ArrayDelegate< C, I, T > & operator=(const ArrayDelegate< C, I, T > &delegate)
Definition: delegate.h:44
elm::Default
Definition: delegate.h:55