tbx  0.7.6
stringviewvalue.h
1 /*
2  * tbx RISC OS toolbox library
3  *
4  * Copyright (C) 2013 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  * stringviewvalue.h
26  *
27  * Created on: 18 Jul 2013
28  * Author: alanb
29  */
30 
31 #ifndef TBX_STRINGVIEWVALUE_H_
32 #define TBX_STRINGVIEWVALUE_H_
33 
34 #include "viewvalue.h"
35 #include "../stringutils.h"
36 #include <string>
37 
38 namespace tbx
39 {
40 namespace view
41 {
42 
49 class ItemViewStringValue : public ItemViewValue<std::string>
50 {
51  public:
52  virtual ~ItemViewStringValue() {};
53 
57  virtual std::string value(unsigned int index) const = 0;
58 
59 };
60 
68 template<class C> class IndexItemViewStringValue : public ItemViewStringValue
69 {
70  private:
71  const C &_collection;
72 
73  public:
77  IndexItemViewStringValue(const C &collection) : _collection(collection) {}
78 
82  virtual std::string value(unsigned int index) const
83  {
84  return to_string(_collection[index]);
85  }
86 };
87 
96 template<class T, class C, class I> class MethodItemViewStringValue : public ItemViewStringValue
97 {
98  private:
99  C *_collection;
100  T (I::*_method)() const;
101 
102  public:
107  MethodItemViewStringValue(C *collection, T (I::*method)() const) :
108  _collection(collection), _method(method) {}
109 
114  virtual std::string value(unsigned int index) const
115  {
116  return to_string((((*_collection)[index]).*_method)());
117  }
118 };
119 
120 
121 
130 template<class T, class C, class I> class MethodItemPtrViewStringValue : public ItemViewStringValue
131 {
132  private:
133  C *_collection;
134  T (I::*_method)() const;
135 
136  public:
141  MethodItemPtrViewStringValue(C *collection, T (I::*method)() const) :
142  _collection(collection), _method(method) {}
143 
148  virtual std::string value(unsigned int index) const
149  {
150  return to_string((((*_collection)[index])->*_method)());
151  }
152 };
153 
154 // end of namespaces
155 }
156 }
157 
158 #endif /* TBX_STRINGVIEWVALUE_H_ */
159 
tbx::view::MethodItemPtrViewStringValue::MethodItemPtrViewStringValue
MethodItemPtrViewStringValue(C *collection, T(I::*method)() const)
Construct for the given collection with the given data retrieval method.
Definition: stringviewvalue.h:141
tbx
A library for creating RISC OS toolbox applications.
Definition: abouttobeshownlistener.cc:35
tbx::view::IndexItemViewStringValue::value
virtual std::string value(unsigned int index) const
Return value for given index.
Definition: stringviewvalue.h:82
tbx::view::ItemViewValue
Base class to provide a value of a given type for an item view.
Definition: viewvalue.h:40
tbx::view::MethodItemPtrViewStringValue
Convenience template to return a member of the class in a collection of pointers as the value for a v...
Definition: stringviewvalue.h:131
tbx::view::IndexItemViewStringValue::IndexItemViewStringValue
IndexItemViewStringValue(const C &collection)
Construct with collection to index.
Definition: stringviewvalue.h:77
tbx::view::ItemViewStringValue
Base class to provide a value as a string.
Definition: stringviewvalue.h:50
tbx::view::IndexItemViewStringValue
Convenience template to return an item of a collection as the value for a view as a string.
Definition: stringviewvalue.h:69
tbx::view::MethodItemViewStringValue::value
virtual std::string value(unsigned int index) const
Get the value for the index by calling the method from the constructor on the object.
Definition: stringviewvalue.h:114
tbx::to_string
std::string to_string(const T &value)
Convert a value to a string.
Definition: stringutils.h:44
tbx::view::MethodItemPtrViewStringValue::value
virtual std::string value(unsigned int index) const
Get the value for the index by calling the method from the constructor on the object.
Definition: stringviewvalue.h:148
tbx::view::MethodItemViewStringValue
Convenience template to return a member of the class in a collection as the value for a view.
Definition: stringviewvalue.h:97
tbx::view::MethodItemViewStringValue::MethodItemViewStringValue
MethodItemViewStringValue(C *collection, T(I::*method)() const)
Construct for the given collection with the given data retrieval method.
Definition: stringviewvalue.h:107
tbx::view::ItemViewStringValue::value
virtual std::string value(unsigned int index) const =0
Provide the value for the view.