TextInterfaceAttribute.h
1//-*-C++-*-
2
3/***************************************************************************
4 *
5 * Copyright (C) 2003 - 2016 by Willem van Straten
6 * Licensed under the Academic Free License version 2.1
7 *
8 ***************************************************************************/
9
10// psrchive/psrchive/Util/units/TextInterfaceAttribute.h
11
12#ifndef __TextInterfaceAttribute_h
13#define __TextInterfaceAttribute_h
14
15#include "TextInterfaceValue.h"
16
17namespace TextInterface
18{
19
21 template<class C>
22 class Attribute : public Value
23 {
24
25 public:
26
28 Attribute () { }
29
31 std::string get_value () const
32 { if (!instance) return "N/A"; else return get_value (instance); }
33
35 void set_value (const std::string& value)
36 { if (instance) set_value (instance, value); }
37
39 virtual Attribute* clone () const = 0;
40
42 virtual std::string get_value (const C*) const = 0;
43
45 virtual void set_value (C*, const std::string& value) = 0;
46
48 virtual void set_description (const std::string&) = 0;
49
51 virtual void set_detailed_description (const std::string&) = 0;
52
55
56 };
57
59
60 template<class Type>
62 {
63 protected:
64
65 mutable ToString tostr;
66
67 public:
68
69 void set_modifiers (const std::string& modifiers) const
70 {
71#ifdef _DEBUG
72 std::cerr << "ToStringPolicy<Type=" << typeid(Type).name()
73 << ">::set_modifiers " << modifiers << " tostr=" << (void*) &tostr << std::endl;
74#endif
75 tostr.set_precision ( fromstring<unsigned>(modifiers) );
76 }
77
78 void reset_modifiers () const
79 {
80 tostr.reset_modifiers ();
81 }
82
83 std::string operator () (const Type& t) const
84 {
85#ifdef _DEBUG
86 std::cerr << "ToStringPolicy<Type=" << typeid(Type).name()
87 << ">::operator ()" << " tostr=" << (void*) &tostr << std::endl;
88#endif
89 return tostr( t );
90 }
91 };
92
94
95 template<class C, class Get>
96 class GetToStringPolicy : public ToStringPolicy<typename Get::result_type>
97 {
98 public:
99 std::string operator () (const C* ptr, Get get) const
100 {
101#ifdef _DEBUG
102 std::cerr << "GetToStringPolicy<C=" << typeid(C).name()
103 << ">::operator (const C* ptr, Get get)" << " tostr=" << (void*) &this->tostr << std::endl;
104#endif
105 if (!ptr)
106 return "";
107 return this->tostr( get (ptr) );
108 }
109 };
110
111 template<class C, class P, class Type>
112 class GetToStringPolicy<C, Type (P::*)() const> : public ToStringPolicy<Type>
113 {
114 public:
115 std::string operator () (const C* ptr, Type (P::*get)() const) const
116 {
117#ifdef _DEBUG
118 std::cerr << "GetToStringPolicy<C=" << typeid(C).name()
119 << ">::operator (const C* ptr, Type (P::*get)() const)" << " tostr=" << (void*) &this->tostr << std::endl;
120#endif
121 if (!ptr)
122 return "";
123 return this->tostr( (ptr->*get) () );
124 }
125 };
126
127 template<class C, class Type>
128 class GetToStringPolicy<C, Type (C*)> : public ToStringPolicy<Type>
129 {
130 public:
131 std::string operator () (const C* ptr, Type (*func)(C*)) const
132 {
133#ifdef _DEBUG
134 std::cerr << "GetToStringPolicy<Type=" << typeid(Type).name()
135 << ">::operator (const C* ptr, Type (*func)(C*))" << " tostr=" << (void*) &this->tostr << std::endl;
136#endif
137 if (!ptr)
138 return "";
139 return this->tostr( (*func) (ptr) );
140 }
141 };
142
144
145 template<class C, class Set>
147 {
148 public:
149 void operator () (C* ptr, Set set, const std::string& value)
150 { (set) (ptr, fromstring<typename Set::second_argument_type>(value)); }
151 };
152
153 template<class C, class P, class T>
154 class SetFromStringPolicy<C, void (P::*)(const T&)>
155 {
156 public:
157 void operator () (C* ptr, void (P::*set)(const T&), const std::string& val)
158 { (ptr->*set) (fromstring<T>(val)); }
159 };
160
161 template<class C, class P, class Type>
162 class SetFromStringPolicy<C, void (P::*)(Type)>
163 {
164 public:
165 void operator () (C* ptr, void (P::*set)(Type), const std::string& value)
166 { (ptr->*set) (fromstring<Type>(value)); }
167 };
168
169 template<class C, class P, class T>
170 class SetFromStringPolicy<C, void (*)(P*,const T&)>
171 {
172 public:
173 void operator () (C* ptr, void (*set)(P*, const T&), const std::string& val)
174 { (*set) (ptr, fromstring<T>(val)); }
175 };
176
177 template<class C, class P, class Type>
178 class SetFromStringPolicy<C, void (*)(P*,Type)>
179 {
180 public:
181 void operator () (C* ptr, void (*set)(P*,Type), const std::string& value)
182 { (*set) (ptr, fromstring<Type>(value)); }
183 };
184
186 template<class C, class Get>
187 class AttributeGet : public Attribute<C>
188 {
189
190 public:
191
193 AttributeGet (const std::string& _name, Get _get) : get(_get)
194 { name = _name; }
195
197 Attribute<C>* clone () const { return new AttributeGet(*this); }
198
200 std::string get_name () const { return name; }
201
203 std::string get_description () const { return description; }
204
206 void set_description (const std::string& d) { description = d; }
207
209 std::string get_detailed_description () const
210 { return detailed_description; }
211
213 void set_detailed_description (const std::string& d)
214 { detailed_description = d; }
215
217 std::string get_value (const C* ptr) const
218 { return tostring (ptr,get); }
219
221 void set_value (C*, const std::string&)
222 { throw Error (InvalidState, "AttributeGet::set_value",
223 name + " cannot be set"); }
224
225 void set_modifiers (const std::string& modifiers) const
226 { tostring.set_modifiers (modifiers); }
227
228 void reset_modifiers () const
229 { tostring.reset_modifiers (); }
230
231 protected:
232
234 std::string name;
235
237 std::string description;
238
241
243 Get get;
244
246 };
247
249 template<class C, class Type, class Get, class Set>
250 class AttributeGetSet : public AttributeGet<C, Get>
251 {
252
253 public:
254
256 AttributeGetSet (const std::string& _name, Get _get, Set _set)
257 : AttributeGet<C,Get> (_name, _get), set(_set) { }
258
260 Attribute<C>* clone () const { return new AttributeGetSet(*this); }
261
263 void set_value (C* ptr, const std::string& value)
264 { from_string (ptr, set, value); }
265
266 protected:
267
269 Set set;
270
271 SetFromStringPolicy<C,Set> from_string;
272 };
273
275
278 template<class C, class Type>
279 class Allocator {
280
281 public:
282
284 template<class Get>
286 operator () (const std::string& n, Get g)
287 { return new AttributeGet<C,Get> (n, g); }
288
290 template<class Get, class Set>
292 operator () (const std::string& n, Get g, Set s)
293 { return new AttributeGetSet<C,Type,Get,Set> (n, g, s); }
294
296 template<class Get>
298 operator () (const std::string& n, const std::string& d, Get g)
299 {
301 get->set_description (d); return get;
302 }
303
305 template<class Get, class Set>
307 operator () (const std::string& n, const std::string& d, Get g, Set s)
308 {
310 get->set_description (d); return get;
311 }
312
313 };
314
316 void parse_indeces (std::vector<unsigned>& indeces, const std::string&,
317 unsigned size);
318
320 bool match (const std::string& name, const std::string& text,
321 std::string* range, std::string* remainder = 0);
322
323}
324
325#endif
A convenient exception handling class.
Definition Error.h:54
Template class manages Reference::Able objects.
Definition ReferenceTo.h:25
AttributeGet and AttributeGetSet factory.
Definition TextInterfaceAttribute.h:279
AttributeGet< C, Get > * operator()(const std::string &n, Get g)
Generate a new AttributeGet instance.
Definition TextInterfaceAttribute.h:286
Interface to a class attribute with an accessor and modifier methods.
Definition TextInterfaceAttribute.h:251
Set set
The set method.
Definition TextInterfaceAttribute.h:269
Attribute< C > * clone() const
Return a clone.
Definition TextInterfaceAttribute.h:260
AttributeGetSet(const std::string &_name, Get _get, Set _set)
Constructor.
Definition TextInterfaceAttribute.h:256
void set_value(C *ptr, const std::string &value)
Set the value of the attribute.
Definition TextInterfaceAttribute.h:263
Interface to a class attribute with an accessor method, C::Get()
Definition TextInterfaceAttribute.h:188
void set_description(const std::string &d)
Get the description of the attribute.
Definition TextInterfaceAttribute.h:206
std::string detailed_description
The detailed description of the attribute.
Definition TextInterfaceAttribute.h:240
Attribute< C > * clone() const
Return a clone.
Definition TextInterfaceAttribute.h:197
AttributeGet(const std::string &_name, Get _get)
Constructor.
Definition TextInterfaceAttribute.h:193
std::string get_value(const C *ptr) const
Get the value of the attribute.
Definition TextInterfaceAttribute.h:217
std::string description
The description of the attribute.
Definition TextInterfaceAttribute.h:237
Get get
The get method.
Definition TextInterfaceAttribute.h:243
std::string get_detailed_description() const
Get the detailed description of the attribute.
Definition TextInterfaceAttribute.h:209
void reset_modifiers() const
Reset any output stream modifiers.
Definition TextInterfaceAttribute.h:228
std::string get_name() const
Get the name of the attribute.
Definition TextInterfaceAttribute.h:200
void set_detailed_description(const std::string &d)
Get the detailed description of the attribute.
Definition TextInterfaceAttribute.h:213
void set_modifiers(const std::string &modifiers) const
Parse any modifiers that will alter the behaviour of the output stream.
Definition TextInterfaceAttribute.h:225
void set_value(C *, const std::string &)
Set the value of the attribute.
Definition TextInterfaceAttribute.h:221
std::string get_description() const
Get the description of the attribute.
Definition TextInterfaceAttribute.h:203
std::string name
The name of the attribute.
Definition TextInterfaceAttribute.h:234
Text interface to a class attribute.
Definition TextInterfaceAttribute.h:23
std::string get_value() const
Get the value of the attribute.
Definition TextInterfaceAttribute.h:31
virtual void set_value(C *, const std::string &value)=0
Set the value of the attribute.
virtual std::string get_value(const C *) const =0
Get the value of the attribute.
Reference::To< C, false > instance
Pointer to the instance from which attribute value will be obtained.
Definition TextInterfaceAttribute.h:54
Attribute()
Default constructor.
Definition TextInterfaceAttribute.h:28
virtual void set_description(const std::string &)=0
Set the description of the value.
void set_value(const std::string &value)
Set the value of the attribute.
Definition TextInterfaceAttribute.h:35
virtual Attribute * clone() const =0
Retun a newly constructed copy.
virtual void set_detailed_description(const std::string &)=0
Set the detailed description of the value.
Policy for converting a value to a string.
Definition TextInterfaceAttribute.h:97
Policy for converting a string to a value.
Definition TextInterfaceAttribute.h:147
Policy for converting a value to a string.
Definition TextInterfaceAttribute.h:62
Value()
Explicit default constructor required to delay definition of Reference::To<Parser>
Definition TextInterfaceValue.C:13

Generated using doxygen 1.14.0