Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 namespace elm {
26 
27 // ArrayDelegate class
28 template <class C, class I, class T>
30 public:
31  inline ArrayDelegate(C& container, const I& identifier)
32  : cont(&container), id(identifier) { }
33  inline ArrayDelegate(const ArrayDelegate& delegate)
34  : cont(delegate.cont), id(delegate.id) { }
35 
36  inline operator T(void) const { return ((const C *)cont)->get(id); }
37  inline const T& operator*(void) const { return ((const C *)cont)->get(id); }
39  { cont->set(id, value); return *this; }
41  { cont = delegate.cont; id = delegate.id; return *this; }
42 
43 private:
44  C *cont;
45  I id;
46 };
47 
48 
49 // Default value
50 template <class T>
51 class Default {
52 public:
53  static T def;
54 };
55 template <class T> T Default<T>::def = 0;
56 
57 
58 // MapDelegate class
59 template <class C, class I, class T, class D = Default<T> >
60 class MapDelegate {
61 public:
62  inline MapDelegate(C& container, const I& identifier)
63  : cont(&container), id(identifier) { }
64  inline MapDelegate(const MapDelegate& delegate)
65  : cont(delegate.cont), id(delegate.id) { }
66 
67  inline operator T(void) const
68  { return ((const C *)cont)->get(id, D::def); }
69  inline const T& operator*(void) const
70  { return ((const C *)cont)->get(id, D::def); }
72  { cont->put(id, value); return *this; }
74  { cont = delegate.cont; id = delegate.id; return *this; }
75 
76 private:
77  C *cont;
78  I id;
79 };
80 
81 } // elm
82 
83 #endif /* ELM_UTIL_DELEGATE_H */
MapDelegate< C, I, T > & operator=(const T &value)
Definition: delegate.h:71
static T def
Definition: delegate.h:53
ArrayDelegate< C, I, T > & operator=(const T &value)
Definition: delegate.h:38
Definition: delegate.h:51
MapDelegate(const MapDelegate &delegate)
Definition: delegate.h:64
Definition: delegate.h:29
value_t value(CString name, int value)
Definition: rtti.h:40
const T & operator*(void) const
Definition: delegate.h:69
ArrayDelegate(C &container, const I &identifier)
Definition: delegate.h:31
MapDelegate< C, I, T > & operator=(const MapDelegate< C, I, T > &delegate)
Definition: delegate.h:73
Definition: delegate.h:60
ArrayDelegate< C, I, T > & operator=(const ArrayDelegate< C, I, T > &delegate)
Definition: delegate.h:40
const T & operator*(void) const
Definition: delegate.h:37
MapDelegate(C &container, const I &identifier)
Definition: delegate.h:62
ArrayDelegate(const ArrayDelegate &delegate)
Definition: delegate.h:33