TextInterfaceProxy.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/TextInterfaceProxy.h
11
12#ifndef __TextInterfaceProxy_h
13#define __TextInterfaceProxy_h
14
15#include "TextInterfaceAttribute.h"
16#include "TextInterfaceAdapter.h"
17
18#include "separate.h"
19#include "stringtok.h"
20
21namespace TextInterface
22{
24
25 template<class C, class P>
26 class IsAProxy : public AttributeAdapter<C,P>
27 {
28
29 public:
30
33 { this->value = pa->clone(); }
34
36 IsAProxy (const IsAProxy& copy)
37 { this->value = copy.value->clone(); }
38
41 { return new IsAProxy(*this); }
42
44 std::string get_value (const C* ptr) const
45 { if (!ptr) return "null";
46 return this->value->get_value (ptr); }
47
49 void set_value (C* ptr, const std::string& value)
50 { if (ptr) this->value->set_value (ptr, value); }
51 };
52
54
55 template<class C, class M, class Get>
56 class HasAProxy : public AttributeAdapter<C,M>
57 {
58
59 public:
60
62 HasAProxy (const std::string& pre, const Attribute<M>* pa, Get g)
63 { prefix = pre; this->value = pa->clone(); get = g; }
64
66 HasAProxy (const HasAProxy& copy)
67 { this->value=copy.value->clone(); get=copy.get; prefix=copy.prefix; }
68
71 { return new HasAProxy(*this); }
72
74 std::string get_name () const
75 {
76 if (prefix.length())
77 return prefix + ":" + this->value->get_name();
78 else
79 return this->value->get_name();
80 }
81
83 std::string get_value (const C* ptr) const
84 { if (!ptr) return "null";
85 return this->value->get_value ((const_cast<C*>(ptr)->*get)()); }
86
88 void set_value (C* ptr, const std::string& val)
89 { if (ptr) this->value->set_value ((ptr->*get)(), val); }
90
92 bool matches (const std::string& name) const
93 { return TextInterface::matches (name, prefix, this->value); }
94
95 protected:
96
98 Get get;
99
101 std::string prefix;
102 };
103
105
108 template<class V, class E, class Get, class Size>
109 class VectorOfProxy : public AttributeAdapter<V,E>
110 {
111
112 public:
113
115 VectorOfProxy (const std::string& p, const Attribute<E>* a, Get g, Size s)
116 { prefix = p; this->value = a->clone(); get = g; size = s; }
117
120 { this->value=copy.value->clone();
121 get=copy.get; size=copy.size; prefix=copy.prefix; }
122
125 { return new VectorOfProxy(*this); }
126
128 std::string get_name () const
129 { return prefix + "*:" + this->value->get_name(); }
130
132 std::string get_value (const V* ptr) const;
133
135 void set_value (V* ptr, const std::string& value);
136
138 bool matches (const std::string& name) const;
139
140 protected:
141
143 Get get;
144
146 Size size;
147
149 std::string prefix;
150
152 mutable std::string range;
153
154 };
155
156
158
160 template<class M, class K, class E, class Get>
161 class MapOfProxy : public AttributeAdapter<M,E>
162 {
163
164 public:
165
167 MapOfProxy (const std::string& pre, const Attribute<E>* pa, Get g)
168 { prefix = pre; this->value = pa->clone(); get = g; }
169
171 MapOfProxy (const MapOfProxy& copy)
172 { prefix=copy.prefix; this->value=copy.value->clone(); get=copy.get; }
173
175
178 { return new MapOfProxy(*this); }
179
181 std::string get_name () const
182 { return prefix + "?:" + this->value->get_name(); }
183
185 std::string get_value (const M* ptr) const;
186
188 void set_value (M* ptr, const std::string& value);
189
191 bool matches (const std::string& name) const;
192
193 protected:
194
196 Get get;
197
199 std::string prefix;
200
202 mutable std::string range;
203
205 void get_indeces (std::vector<K>& keys,
206 const std::string& param) const;
207
208 };
209
210}
211
212template<class V, class E, class G, class S>
213std::string
215{
216 std::vector<unsigned> ind;
217 parse_indeces (ind, range, (ptr->*size)());
218 std::string result;
219
220 if (!this->parent)
221 throw Error (InvalidState, "VectorOfProxy["+prefix+"]", "no parent");
222
223 for (unsigned i=0; i<ind.size(); i++)
224 {
225 // place a delimiter between elements
226 if (i)
227 result += this->parent->get_delimiter();
228
229 // label the elements
230 if (label_elements && ind.size() > 1)
231 result += tostring(ind[i]) + ")";
232
233 E* element = (const_cast<V*>(ptr)->*get)(ind[i]);
234#ifdef _DEBUG
235 std::cerr << "VectorOfProxy[" << prefix << "]::get_value ("
236 << ptr << ") element=" << element << std::endl;
237#endif
238 result += this->value->get_value (element);
239 }
240
241 return result;
242}
243
244template<class V, class E, class G, class S>
246 const std::string& val)
247{
248 std::vector<unsigned> ind;
249 parse_indeces (ind, range, (ptr->*size)());
250
251 for (unsigned i=0; i<ind.size(); i++) {
252 E* element = (ptr->*get)(ind[i]);
253#ifdef _DEBUG
254 std::cerr << "VectorOfProxy[" << prefix << "]::set_value ("
255 << ptr << "," << val << ")" << std::endl;
256#endif
257 this->value->set_value (element, val);
258 }
259}
260
261
262template<class C,class M,class Get,class Size>
264 (const std::string& name) const
265{
266#ifdef _DEBUG
267 std::cerr << "TextInterface::VectorOfProxy::matches" << std::endl;
268#endif
269
270 std::string remainder;
271 if (!match (prefix, name, &range, &remainder))
272 return false;
273
274 return this->value->matches (remainder);
275}
276
277
278
279template<class M, class K, class E, class G>
280std::string
282{
283 std::vector<K> ind;
284 get_indeces (ind, range);
285 std::string result;
286
287 if (!this->parent)
288 throw Error (InvalidState, "MapOfProxy["+prefix+"]", "no parent");
289
290 for (unsigned i=0; i<ind.size(); i++)
291 {
292 // place a delimiter between elements
293 if (i)
294 result += this->parent->get_delimiter();
295
296 // label the elements
297 if (label_elements && ind.size() > 1)
298 result += tostring(ind[i]) + ")";
299
300 E* element = (const_cast<M*>(ptr)->*get) (ind[i]);
301 if (element)
302 result += this->value->get_value (element);
303 }
304
305 return result;
306}
307
308template<class M, class K, class E, class G>
310 const std::string& val)
311{
312 std::vector<K> ind;
313 get_indeces (ind, range);
314
315 for (unsigned i=0; i<ind.size(); i++)
316 this->value->set_value ((ptr->*get)(ind[i]), val);
317}
318
319template<class M, class K, class E, class G>
320void
322 const std::string& par) const
323{
324#ifdef _DEBUG
325 std::cerr << "MapOfProxy::get_indeces " << par << std::endl;
326#endif
327
328 std::string::size_type length = par.length();
329
330 std::string range = par;
331
332 if (prefix.length()) {
333 if (par[0] != '[' || par[length-1] != ']')
334 return;
335 range = par.substr (1, length-2);
336 }
337 else if (par == "?")
338 return;
339
340 std::vector<std::string> key_str;
341 separate (range, key_str, ", ");
342
343 indeces.resize (key_str.size());
344 for (unsigned i=0; i<key_str.size(); i++)
345 indeces[i] = fromstring<K>(key_str[i]);
346}
347
348template<class M, class K, class E, class G>
350 (const std::string& name) const
351{
352#ifdef _DEBUG
353 std::cerr << "TextInterface::MapOfProxy::matches" << std::endl;
354#endif
355
356 std::string remainder;
357 if (!match (prefix, name, &range, &remainder))
358 return false;
359
360 return this->value->matches (remainder);
361}
362
363#endif
A convenient exception handling class.
Definition Error.h:54
Reference::To< Attribute< P > > value
Definition TextInterfaceAdapter.h:54
Can be used as an Adapter (IsA != HasA) or a Decorator (IsA == HasA)
Definition TextInterfaceAdapter.h:79
Text interface to a class attribute.
Definition TextInterfaceAttribute.h:23
std::string get_value() const
Definition TextInterfaceAttribute.h:31
virtual Attribute * clone() const =0
Retun a newly constructed copy.
std::string get_value(const C *ptr) const
Get the value of the attribute.
Definition TextInterfaceProxy.h:83
bool matches(const std::string &name) const
Return true if the name argument matches.
Definition TextInterfaceProxy.h:92
HasAProxy(const HasAProxy &copy)
Copy constructor.
Definition TextInterfaceProxy.h:66
std::string get_name() const
Get the name of the attribute.
Definition TextInterfaceProxy.h:74
HasAProxy(const std::string &pre, const Attribute< M > *pa, Get g)
Construct from a pointer to member attribute interface.
Definition TextInterfaceProxy.h:62
Get get
Method of C that returns M*.
Definition TextInterfaceProxy.h:98
Attribute< C > * clone() const
Retun a newly constructed copy.
Definition TextInterfaceProxy.h:70
std::string prefix
The name of the M attribute within C.
Definition TextInterfaceProxy.h:101
void set_value(C *ptr, const std::string &val)
Set the value of the attribute.
Definition TextInterfaceProxy.h:88
std::string get_value(const C *ptr) const
Get the value of the attribute.
Definition TextInterfaceProxy.h:44
void set_value(C *ptr, const std::string &value)
Set the value of the attribute.
Definition TextInterfaceProxy.h:49
IsAProxy(const Attribute< P > *pa)
Construct from a pointer to parent class attribute interface.
Definition TextInterfaceProxy.h:32
IsAProxy(const IsAProxy &copy)
Copy constructor.
Definition TextInterfaceProxy.h:36
Attribute< C > * clone() const
Retun a newly constructed copy.
Definition TextInterfaceProxy.h:40
std::string range
Range parsed from name during matches.
Definition TextInterfaceProxy.h:202
MapOfProxy(const std::string &pre, const Attribute< E > *pa, Get g)
Construct from a pointer to parent class attribute interface.
Definition TextInterfaceProxy.h:167
void get_indeces(std::vector< K > &keys, const std::string &param) const
Worker function parses keys for get_value and set_value.
Definition TextInterfaceProxy.h:321
Get get
Method of M that returns E*, given K.
Definition TextInterfaceProxy.h:196
Attribute< M > * clone() const
Set the prefix to be added before attribute name.
Definition TextInterfaceProxy.h:177
std::string prefix
The name of the map instance.
Definition TextInterfaceProxy.h:199
bool matches(const std::string &name) const
Return true if the name argument matches.
Definition TextInterfaceProxy.h:350
std::string get_name() const
Get the name of the attribute.
Definition TextInterfaceProxy.h:181
void set_value(M *ptr, const std::string &value)
Set the value of the attribute.
Definition TextInterfaceProxy.h:309
MapOfProxy(const MapOfProxy &copy)
Copy constructor.
Definition TextInterfaceProxy.h:171
virtual std::string get_value() const =0
Get the value as text.
Size size
Method of V that returns size of vector.
Definition TextInterfaceProxy.h:146
std::string get_name() const
Get the name of the attribute.
Definition TextInterfaceProxy.h:128
std::string prefix
The name of the vector instance.
Definition TextInterfaceProxy.h:149
void set_value(V *ptr, const std::string &value)
Set the value of the attribute.
Definition TextInterfaceProxy.h:245
bool matches(const std::string &name) const
Return true if the name argument matches.
Definition TextInterfaceProxy.h:264
std::string range
Range parsed from name during matches.
Definition TextInterfaceProxy.h:152
Get get
Method of V that returns E*.
Definition TextInterfaceProxy.h:143
Attribute< V > * clone() const
Retun a newly constructed copy.
Definition TextInterfaceProxy.h:124
VectorOfProxy(const std::string &p, const Attribute< E > *a, Get g, Size s)
Construct from a pointer to element attribute interface.
Definition TextInterfaceProxy.h:115
VectorOfProxy(const VectorOfProxy &copy)
Copy constructor.
Definition TextInterfaceProxy.h:119

Generated using doxygen 1.14.0