TextInterfaceElement.h
1 //-*-C++-*-
2 
3 /***************************************************************************
4  *
5  * Copyright (C) 2003 - 2022 by Willem van Straten
6  * Licensed under the Academic Free License version 2.1
7  *
8  ***************************************************************************/
9 
10 // psrchive/psrchive/Util/units/TextInterfaceElement.h
11 
12 #ifndef __TextInterfaceElement_h
13 #define __TextInterfaceElement_h
14 
15 #include "TextInterfaceAttribute.h"
16 
17 namespace TextInterface
18 {
19 
21 
25  template<class C, class Get, class Size>
26  class ElementGet : public Attribute<C>
27  {
28  public:
29 
31  ElementGet (const std::string& _name, Get _get, Size _size)
32  { name = _name; get = _get; size = _size; }
33 
35  Attribute<C>* clone () const { return new ElementGet(*this); }
36 
38  std::string get_name () const { return name + "*"; }
39 
41  std::string get_description () const { return description; }
42 
44  void set_description (const std::string& d) { description = d; }
45 
47  std::string get_detailed_description () const
49 
51  void set_detailed_description (const std::string& d)
52  { detailed_description = d; }
53 
55  std::string get_value (const C* ptr) const;
56 
58  void set_value (C*, const std::string&)
59  { throw Error (InvalidState, "ElementGet::set_value",
60  name + " cannot be set"); }
61 
63  bool matches (const std::string& name) const;
64 
65  void set_modifiers (const std::string& modifiers) const
66  {
67  tostring_precision = fromstring<unsigned> (modifiers);
68  }
69 
70  void reset_modifiers () const
71  {
72  tostring_precision = 0;
73  }
74 
75  protected:
76 
78  std::string name;
79 
81  std::string description;
82 
84  std::string detailed_description;
85 
87  Get get;
88 
90  Size size;
91 
93  mutable std::string range;
94 
95  };
96 
98  template<class C, class Type, class Get, class Set, class Size>
99  class ElementGetSet : public ElementGet<C, Get, Size> {
100 
101  public:
102 
104  ElementGetSet (const std::string& _name, Get _get, Set _set, Size _size)
105  : ElementGet<C,Get,Size> (_name, _get, _size) { set = _set; }
106 
108  Attribute<C>* clone () const { return new ElementGetSet(*this); }
109 
111  void set_value (C* ptr, const std::string& value);
112 
113  protected:
114 
116  Set set;
117 
118  };
119 
121 
124  template<class C, class Type>
125  class VAllocator {
126 
127  public:
128 
130  template<class Get, class Size>
132  operator () (const std::string& n, Get g, Size z)
133  { return new ElementGet<C,Get,Size> (n, g, z); }
134 
136  template<class Get, class Set, class Size>
138  operator () (const std::string& n, Get g, Set s, Size z)
139  { return new ElementGetSet<C,Type,Get,Set,Size> (n, g, s, z); }
140 
142  template<class Get, class Size>
144  operator () (const std::string& n, const std::string& d, Get g, Size z)
145  {
146  ElementGet<C,Get,Size>* get = operator () (n,g,z);
147  get->set_description (d); return get;
148  }
149 
151  template<class Get, class Set, class Size>
153  operator () (const std::string& n, const std::string& d,
154  Get g, Set s, Size z)
155  {
157  get->set_description (d); return get;
158  }
159 
160  };
161 
163  extern bool label_elements;
164 
165 }
166 
167 template<class C,class Get,class Size>
169  (const std::string& var) const
170 {
171 #ifdef _DEBUG
172  std::cerr << "TextInterface::ElementGet::matches" << std::endl;
173 #endif
174 
175  if (!match (name, var, &range))
176  return false;
177 
178  return true;
179 }
180 
181 template<class C, class G, class S>
182 std::string TextInterface::ElementGet<C,G,S>::get_value (const C* ptr) const
183 {
184 #ifdef _DEBUG
185  std::cerr << "TextInterface::ElementGet::get_value name=" << name
186  << " range=" << range << std::endl;
187 #endif
188 
189  std::vector<unsigned> ind;
190  parse_indeces (ind, range, (ptr->*(size))());
191  std::string result;
192 
193  if (!this->parent)
194  throw Error (InvalidState, "ElementGet["+name+"]", "no parent");
195 
196  for (unsigned i=0; i<ind.size(); i++)
197  {
198  // place a delimiter between elements
199  if (i)
200  result += this->parent->get_delimiter();
201 
202  if (label_elements && ind.size() > 1)
203  result += tostring(ind[i]) + ")"; // label the elements
204 
205  result += tostring( (ptr->*get)(ind[i]) );
206  }
207 
208  return result;
209 }
210 
211 template<class C, class T, class G, class S, class Z>
213  const std::string& v)
214 {
215  std::vector<unsigned> ind;
216  parse_indeces (ind, this->range, (ptr->*(this->size))());
217 
218  for (unsigned i=0; i<ind.size(); i++)
219  (ptr->*set)(ind[i], fromstring<T>(v));
220 }
221 
222 #endif
void set_modifiers(const std::string &modifiers) const
Parse any modifiers that will alter the behaviour of the output stream.
Definition: TextInterfaceElement.h:75
void reset_modifiers() const
Reset any output stream modifiers.
Definition: TextInterfaceElement.h:80
Set set
The set method.
Definition: TextInterfaceElement.h:121
ElementGet and ElementGetSet factory.
Definition: TextInterfaceElement.h:130
std::string get_name() const
Get the name of the attribute.
Definition: TextInterfaceElement.h:48
void set_value(C *ptr, const std::string &value)
Set the value of the attribute.
Definition: TextInterfaceElement.h:212
void set_detailed_description(const std::string &d)
Get the detailed description of the attribute.
Definition: TextInterfaceElement.h:61
Attribute< C > * clone() const
Return a clone.
Definition: TextInterfaceElement.h:113
ElementGetSet(const std::string &_name, Get _get, Set _set, Size _size)
Constructor.
Definition: TextInterfaceElement.h:109
void set_description(const std::string &d)
Get the description of the attribute.
Definition: TextInterfaceElement.h:54
Pointers to attribute get and set methods, Type C::Get() and C::Set(Type)
Definition: TextInterfaceElement.h:104
A convenient exception handling class.
Definition: Error.h:54
Interface to an array of attributes.
Definition: TextInterfaceElement.h:31
ElementGet< C, Get, Size > * operator()(const std::string &n, Get g, Size z)
Generate a new ElementGet instance.
Definition: TextInterfaceElement.h:137
std::string get_detailed_description() const
Get the detailed description of the attribute.
Definition: TextInterfaceElement.h:57
Size size
The size method.
Definition: TextInterfaceElement.h:100
std::string get_value() const
Get the value of the attribute.
Definition: TextInterfaceAttribute.h:42
std::string get_description() const
Get the description of the attribute.
Definition: TextInterfaceElement.h:51
ElementGet(const std::string &_name, Get _get, Size _size)
Constructor.
Definition: TextInterfaceElement.h:41
std::string name
The name of the attribute.
Definition: TextInterfaceElement.h:88
std::string description
The description of the attribute.
Definition: TextInterfaceElement.h:91
Attribute< C > * clone() const
Return a clone.
Definition: TextInterfaceElement.h:45
std::string range
Range parsed from name during matches.
Definition: TextInterfaceElement.h:103
Text interface to a class attribute.
Definition: TextInterfaceAttribute.h:27
Get get
The get method.
Definition: TextInterfaceElement.h:97
std::string detailed_description
The detailed description of the attribute.
Definition: TextInterfaceElement.h:94
bool matches(const std::string &name) const
Return true if the name argument matches.
Definition: TextInterfaceElement.h:169
void set_value(C *, const std::string &)
Set the value of the attribute.
Definition: TextInterfaceElement.h:68

Generated using doxygen 1.8.17