tbx  0.7.5
viewvalue.h
1 /*
2  * tbx RISC OS toolbox library
3  *
4  * Copyright (C) 2010 Alan Buckley All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #ifndef TBX_VIEWVALUE_H
26 #define TBX_VIEWVALUE_H
27 
28 namespace tbx
29 {
30 namespace view
31 {
32 
39 template<class T> class ItemViewValue
40 {
41  public:
42  virtual ~ItemViewValue() {};
43 
47  virtual T value(unsigned int index) const = 0;
48 
49 };
50 
58 template<class T, class C> class IndexItemViewValue : public ItemViewValue<T>
59 {
60  private:
61  const C &_collection;
62 
63  public:
67  IndexItemViewValue(const C &collection) : _collection(collection) {}
68 
72  virtual T value(unsigned int index) const {return _collection[index];}
73 };
74 
83 template<class T, class C, class I> class MethodItemViewValue : public ItemViewValue<T>
84 {
85  private:
86  C *_collection;
87  T (I::*_method)() const;
88 
89  public:
94  MethodItemViewValue(C *collection, T (I::*method)() const) :
95  _collection(collection), _method(method) {}
96 
101  virtual T value(unsigned int index) const
102  {
103  return (((*_collection)[index]).*_method)();
104  }
105 };
106 
115 template<class T, class C, class I> class MethodItemPtrViewValue : public ItemViewValue<T>
116 {
117  private:
118  C *_collection;
119  T (I::*_method)() const;
120 
121  public:
126  MethodItemPtrViewValue(C *collection, T (I::*method)() const) :
127  _collection(collection), _method(method) {}
128 
133  virtual T value(unsigned int index) const
134  {
135  return (((*_collection)[index])->*_method)();
136  }
137 };
138 
139 
140 // end of namespaces
141 }
142 }
143 
144 
145 #endif
A library for creating RISC OS toolbox applications.
Definition: abouttobeshownlistener.cc:34
virtual T value(unsigned int index) const
Return value for given index.
Definition: viewvalue.h:72
MethodItemPtrViewValue(C *collection, T(I::*method)() const)
Construct for the given collection with the given data retrieval method.
Definition: viewvalue.h:126
Helper classes to display and edit data.
virtual T value(unsigned int index) const
Get the value for the index by calling the method from the constructor on the object.
Definition: viewvalue.h:133
virtual T value(unsigned int index) const
Get the value for the index by calling the method from the constructor on the object.
Definition: viewvalue.h:101
Base class to provide a value of a given type for an item view.
Definition: viewvalue.h:39
Convenience template to return an item of a collection as the value for a view.
Definition: viewvalue.h:58
IndexItemViewValue(const C &collection)
Construct with collection to index.
Definition: viewvalue.h:67
Convenience template to return a member of the class in a collection as the value for a view...
Definition: viewvalue.h:83
virtual T value(unsigned int index) const =0
Provide the value for the view.
Convenience template to return a member of the class in a collection of pointers as the value for a v...
Definition: viewvalue.h:115
MethodItemViewValue(C *collection, T(I::*method)() const)
Construct for the given collection with the given data retrieval method.
Definition: viewvalue.h:94