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 
17 namespace TextInterface
18 {
19 
21  template<class C>
22  class Attribute : public Value
23  {
24 
25  public:
26 
28  Attribute ()
29  { instance = 0; }
30 
32  std::string get_value () const
33  { if (!instance) return "N/A"; else return get_value (instance); }
34 
36  void set_value (const std::string& value)
37  { if (instance) set_value (instance, value); }
38 
40  virtual Attribute* clone () const = 0;
41 
43  virtual std::string get_value (const C*) const = 0;
44 
46  virtual void set_value (C*, const std::string& value) = 0;
47 
49  virtual void set_description (const std::string&) = 0;
50 
52  virtual void set_detailed_description (const std::string&) = 0;
53 
56 
57  };
58 
60 
61  template<class Type>
62  class ToStringPolicy
63  {
64  protected:
65 
66  mutable ToString tostr;
67 
68  public:
69 
70  void set_modifiers (const std::string& modifiers) const
71  {
72 #ifdef _DEBUG
73  std::cerr << "ToStringPolicy<Type=" << typeid(Type).name()
74  << ">::set_modifiers " << modifiers << " tostr=" << (void*) &tostr << std::endl;
75 #endif
76  tostr.set_precision ( fromstring<unsigned>(modifiers) );
77  }
78 
79  void reset_modifiers () const
80  {
81  tostr.reset_modifiers ();
82  }
83 
84  std::string operator () (const Type& t) const
85  {
86 #ifdef _DEBUG
87  std::cerr << "ToStringPolicy<Type=" << typeid(Type).name()
88  << ">::operator ()" << " tostr=" << (void*) &tostr << std::endl;
89 #endif
90  return tostr( t );
91  }
92  };
93 
95 
96  template<class C, class Get>
97  class GetToStringPolicy : public ToStringPolicy<typename Get::result_type>
98  {
99  public:
100  std::string operator () (const C* ptr, Get get) const
101  {
102 #ifdef _DEBUG
103  std::cerr << "GetToStringPolicy<C=" << typeid(C).name()
104  << ">::operator (const C* ptr, Get get)" << " tostr=" << (void*) &this->tostr << std::endl;
105 #endif
106  if (!ptr)
107  return "";
108  return this->tostr( get (ptr) );
109  }
110  };
111 
112  template<class C, class P, class Type>
113  class GetToStringPolicy<C, Type (P::*)() const> : public ToStringPolicy<Type>
114  {
115  public:
116  std::string operator () (const C* ptr, Type (P::*get)() const) const
117  {
118 #ifdef _DEBUG
119  std::cerr << "GetToStringPolicy<C=" << typeid(C).name()
120  << ">::operator (const C* ptr, Type (P::*get)() const)" << " tostr=" << (void*) &this->tostr << std::endl;
121 #endif
122  if (!ptr)
123  return "";
124  return this->tostr( (ptr->*get) () );
125  }
126  };
127 
128  template<class C, class Type>
129  class GetToStringPolicy<C, Type (C*)> : public ToStringPolicy<Type>
130  {
131  public:
132  std::string operator () (const C* ptr, Type (*func)(C*)) const
133  {
134 #ifdef _DEBUG
135  std::cerr << "GetToStringPolicy<Type=" << typeid(Type).name()
136  << ">::operator (const C* ptr, Type (*func)(C*))" << " tostr=" << (void*) &this->tostr << std::endl;
137 #endif
138  if (!ptr)
139  return "";
140  return this->tostr( (*func) (ptr) );
141  }
142  };
143 
145 
146  template<class C, class Set>
147  class SetFromStringPolicy
148  {
149  public:
150  void operator () (C* ptr, Set set, const std::string& value)
151  { (set) (ptr, fromstring<typename Set::second_argument_type>(value)); }
152  };
153 
154  template<class C, class P, class T>
155  class SetFromStringPolicy<C, void (P::*)(const T&)>
156  {
157  public:
158  void operator () (C* ptr, void (P::*set)(const T&), const std::string& val)
159  { (ptr->*set) (fromstring<T>(val)); }
160  };
161 
162  template<class C, class P, class Type>
163  class SetFromStringPolicy<C, void (P::*)(Type)>
164  {
165  public:
166  void operator () (C* ptr, void (P::*set)(Type), const std::string& value)
167  { (ptr->*set) (fromstring<Type>(value)); }
168  };
169 
170  template<class C, class P, class T>
171  class SetFromStringPolicy<C, void (*)(P*,const T&)>
172  {
173  public:
174  void operator () (C* ptr, void (*set)(P*, const T&), const std::string& val)
175  { (*set) (ptr, fromstring<T>(val)); }
176  };
177 
178  template<class C, class P, class Type>
179  class SetFromStringPolicy<C, void (*)(P*,Type)>
180  {
181  public:
182  void operator () (C* ptr, void (*set)(P*,Type), const std::string& value)
183  { (*set) (ptr, fromstring<Type>(value)); }
184  };
185 
187  template<class C, class Get>
188  class AttributeGet : public Attribute<C>
189  {
190 
191  public:
192 
194  AttributeGet (const std::string& _name, Get _get) : get(_get)
195  { name = _name; }
196 
198  Attribute<C>* clone () const { return new AttributeGet(*this); }
199 
201  std::string get_name () const { return name; }
202 
204  std::string get_description () const { return description; }
205 
207  void set_description (const std::string& d) { description = d; }
208 
210  std::string get_detailed_description () const
211  { return detailed_description; }
212 
214  void set_detailed_description (const std::string& d)
216 
218  std::string get_value (const C* ptr) const
219  { return tostring (ptr,get); }
220 
222  void set_value (C*, const std::string&)
223  { throw Error (InvalidState, "AttributeGet::set_value",
224  name + " cannot be set"); }
225 
226  void set_modifiers (const std::string& modifiers) const
227  { tostring.set_modifiers (modifiers); }
228 
229  void reset_modifiers () const
230  { tostring.reset_modifiers (); }
231 
232  protected:
233 
235  std::string name;
236 
238  std::string description;
239 
241  std::string detailed_description;
242 
244  Get get;
245 
247  };
248 
250  template<class C, class Type, class Get, class Set>
251  class AttributeGetSet : public AttributeGet<C, Get>
252  {
253 
254  public:
255 
257  AttributeGetSet (const std::string& _name, Get _get, Set _set)
258  : AttributeGet<C,Get> (_name, _get), set(_set) { }
259 
261  Attribute<C>* clone () const { return new AttributeGetSet(*this); }
262 
264  void set_value (C* ptr, const std::string& value)
265  { from_string (ptr, set, value); }
266 
267  protected:
268 
270  Set set;
271 
272  SetFromStringPolicy<C,Set> from_string;
273  };
274 
276 
279  template<class C, class Type>
280  class Allocator {
281 
282  public:
283 
285  template<class Get>
287  operator () (const std::string& n, Get g)
288  { return new AttributeGet<C,Get> (n, g); }
289 
291  template<class Get, class Set>
293  operator () (const std::string& n, Get g, Set s)
294  { return new AttributeGetSet<C,Type,Get,Set> (n, g, s); }
295 
297  template<class Get>
299  operator () (const std::string& n, const std::string& d, Get g)
300  {
301  AttributeGet<C,Get>* get = operator () (n,g);
302  get->set_description (d); return get;
303  }
304 
306  template<class Get, class Set>
308  operator () (const std::string& n, const std::string& d, Get g, Set s)
309  {
311  get->set_description (d); return get;
312  }
313 
314  };
315 
317  void parse_indeces (std::vector<unsigned>& indeces, const std::string&,
318  unsigned size);
319 
321  bool match (const std::string& name, const std::string& text,
322  std::string* range, std::string* remainder = 0);
323 
324 }
325 
326 #endif
std::string get_name() const
Get the name of the attribute.
Definition: TextInterfaceAttribute.h:206
Reference::To< C, false > instance
Pointer to the instance from which attribute value will be obtained.
Definition: TextInterfaceAttribute.h:65
std::string description
The description of the attribute.
Definition: TextInterfaceAttribute.h:243
void set_value(C *ptr, const std::string &value)
Set the value of the attribute.
Definition: TextInterfaceAttribute.h:269
Attribute()
Default constructor.
Definition: TextInterfaceAttribute.h:38
void set_value(const std::string &value)
Set the value of the attribute.
Definition: TextInterfaceAttribute.h:46
Policy for converting a string to a value.
Definition: TextInterfaceAttribute.h:152
Policy for converting a value to a string.
Definition: TextInterfaceAttribute.h:67
virtual void set_description(const std::string &)=0
Set the description of the value.
A convenient exception handling class.
Definition: Error.h:54
AttributeGet(const std::string &_name, Get _get)
Constructor.
Definition: TextInterfaceAttribute.h:199
std::string get_detailed_description() const
Get the detailed description of the attribute.
Definition: TextInterfaceAttribute.h:215
virtual Attribute * clone() const =0
Retun a newly constructed copy.
std::string detailed_description
The detailed description of the attribute.
Definition: TextInterfaceAttribute.h:246
std::string get_description() const
Get the description of the attribute.
Definition: TextInterfaceAttribute.h:209
void set_detailed_description(const std::string &d)
Get the detailed description of the attribute.
Definition: TextInterfaceAttribute.h:219
Interface to a class attribute with an accessor method, C::Get()
Definition: TextInterfaceAttribute.h:193
AttributeGetSet(const std::string &_name, Get _get, Set _set)
Constructor.
Definition: TextInterfaceAttribute.h:262
Set set
The set method.
Definition: TextInterfaceAttribute.h:275
Get get
The get method.
Definition: TextInterfaceAttribute.h:249
AttributeGet< C, Get > * operator()(const std::string &n, Get g)
Generate a new AttributeGet instance.
Definition: TextInterfaceAttribute.h:292
void reset_modifiers() const
Reset any output stream modifiers.
Definition: TextInterfaceAttribute.h:234
Interface to a class attribute with an accessor and modifier methods.
Definition: TextInterfaceAttribute.h:256
std::string get_value() const
Get the value of the attribute.
Definition: TextInterfaceAttribute.h:42
void set_modifiers(const std::string &modifiers) const
Parse any modifiers that will alter the behaviour of the output stream.
Definition: TextInterfaceAttribute.h:231
std::string name
The name of the attribute.
Definition: TextInterfaceAttribute.h:240
AttributeGet and AttributeGetSet factory.
Definition: TextInterfaceAttribute.h:285
void set_value(C *, const std::string &)
Set the value of the attribute.
Definition: TextInterfaceAttribute.h:227
void set_description(const std::string &d)
Get the description of the attribute.
Definition: TextInterfaceAttribute.h:212
Attribute< C > * clone() const
Return a clone.
Definition: TextInterfaceAttribute.h:203
Policy for converting a value to a string.
Definition: TextInterfaceAttribute.h:102
Text interface to a class attribute.
Definition: TextInterfaceAttribute.h:27
Attribute< C > * clone() const
Return a clone.
Definition: TextInterfaceAttribute.h:266
virtual void set_detailed_description(const std::string &)=0
Set the detailed description of the value.

Generated using doxygen 1.8.17