TextInterfaceEstimate.h
1//-*-C++-*-
2
3/***************************************************************************
4 *
5 * Copyright (C) 2022 by Willem van Straten
6 * Licensed under the Academic Free License version 2.1
7 *
8 ***************************************************************************/
9
10// psrchive/psrchive/Util/units/TextInterfaceEstimate.h
11
12#ifndef __TextInterfaceEstimate_h
13#define __TextInterfaceEstimate_h
14
15#include "TextInterfaceAttribute.h"
16#include "Estimate.h"
17
18#include "separate.h"
19#include "stringtok.h"
20
21namespace TextInterface
22{
24
27 template<class V, class T, class U, class Get, class Size>
28 class VectorOfEstimate : public Attribute<V>
29 {
30
31 public:
32
34 VectorOfEstimate (const std::string& p, const std::string& t, Get g, Size s)
35 { prefix = p; type = t; get = g; size = s; }
36
38 Attribute<V>* clone () const { return new VectorOfEstimate(*this); }
39
41 std::string get_name () const
42 { return prefix + "*:" + type; }
43
45 std::string get_description () const { return description; }
46
48 void set_description (const std::string& d) { description = d; }
49
51 std::string get_detailed_description () const
52 { return detailed_description; }
53
55 void set_detailed_description (const std::string& d)
57
59 std::string get_value (const V* ptr) const;
60
62 void set_value (V*, const std::string&)
63 { throw Error (InvalidState, "VectorOfEstimate::set_value",
64 prefix + " cannot be set"); }
65
66 // void set_value (V* ptr, const std::string& value);
67
69 bool matches (const std::string& name) const;
70
71 void set_modifiers (const std::string& modifiers) const
72 {
73 tostring_precision = fromstring<unsigned> (modifiers);
74 }
75
76 void reset_modifiers () const
77 {
78 tostring_precision = 0;
79 }
80
81 protected:
82
84 Get get;
85
87 Size size;
88
90 std::string prefix;
91
93 std::string type;
94
96 std::string description;
97
100
102 mutable std::string range;
103
104 };
105
107
110 template<class C, class T, class U>
111 class VAllocator< C, Estimate<T,U> >
112 {
113
114 public:
115
117 template<class Get, class Size>
118 void operator () (To<C>* to, const std::string& n, Get g, Size z)
119 {
120 to->add_value( new VectorOfEstimate<C,T,U,Get,Size> (n, "val", g, z) );
121 to->add_value( new VectorOfEstimate<C,T,U,Get,Size> (n, "var", g, z) );
122 }
123
125 template<class Get, class Size>
126 void operator () (To<C>* to, const std::string& n,
127 const std::string& d, Get g, Size z)
128 {
129 auto get = new VectorOfEstimate<C,T,U,Get,Size> (n, "val", g, z);
130 get->set_description (d + " value");
131 to->add_value( get );
132
133 get = new VectorOfEstimate<C,T,U,Get,Size> (n, "var", g, z);
134 get->set_description (d + " variance");
135 to->add_value( get );
136 }
137 };
138
139}
140
141template<class V, class T, class U, class G, class S>
142std::string
144{
145 std::vector<unsigned> ind;
146 parse_indeces (ind, range, (ptr->*size)());
147 std::string result;
148
149 if (!this->parent)
150 throw Error (InvalidState, "VectorOfEstimate["+prefix+"]", "no parent");
151
152 for (unsigned i=0; i<ind.size(); i++)
153 {
154 // place a delimiter between elements
155 if (i)
156 result += this->parent->get_delimiter();
157
158 // label the elements
159 if (label_elements && ind.size() > 1)
160 result += tostring(ind[i]) + ")";
161
162 Estimate<T,U> est = (ptr->*get)(ind[i]);
163
164 if (type == "var")
165 result += tostring( est.var );
166 else
167 result += tostring( est.val );
168 }
169
170 return result;
171}
172
173template<class V, class T, class U, class Get, class Size>
175 (const std::string& name) const
176{
177#ifdef _DEBUG
178 std::cerr << "TextInterface::VectorOfEstimate::matches" << std::endl;
179#endif
180
181 std::string remainder;
182 if (!match (prefix, name, &range, &remainder))
183 return false;
184
185 return remainder == type;
186}
187
188#endif
189
A convenient exception handling class.
Definition Error.h:54
T val
Definition Estimate.h:43
U var
Definition Estimate.h:45
std::string get_value() const
Definition TextInterfaceAttribute.h:31
Attribute()
Definition TextInterfaceAttribute.h:28
Class text interface: an instance of C and a vector of Attribute<C>
Definition TextInterfaceTo.h:25
ElementGet and ElementGetSet factory.
Definition TextInterfaceElement.h:125
ElementGet< C, Get, Size > * operator()(const std::string &n, Get g, Size z)
Generate a new ElementGet instance.
Definition TextInterfaceElement.h:132
virtual std::string get_value() const =0
Get the value as text.
Reference::To< Parser, false > parent
The Parser composite in which this Value component is integrated.
Definition TextInterfaceValue.h:75
Proxy enables attribute interface of Estimates in a vector.
Definition TextInterfaceEstimate.h:29
std::string detailed_description
The detailed description of the attribute.
Definition TextInterfaceEstimate.h:99
void set_modifiers(const std::string &modifiers) const
Parse any modifiers that will alter the behaviour of the output stream.
Definition TextInterfaceEstimate.h:71
std::string get_description() const
Get the description of the attribute.
Definition TextInterfaceEstimate.h:45
std::string type
Either "val" or "var".
Definition TextInterfaceEstimate.h:93
std::string description
The description of the attribute.
Definition TextInterfaceEstimate.h:96
std::string get_detailed_description() const
Get the detailed description of the attribute.
Definition TextInterfaceEstimate.h:51
Size size
Method of V that returns size of vector.
Definition TextInterfaceEstimate.h:87
VectorOfEstimate(const std::string &p, const std::string &t, Get g, Size s)
Construct from a pointer to element attribute interface.
Definition TextInterfaceEstimate.h:34
void reset_modifiers() const
Reset any output stream modifiers.
Definition TextInterfaceEstimate.h:76
void set_value(V *, const std::string &)
Set the value of the attribute.
Definition TextInterfaceEstimate.h:62
std::string get_name() const
Get the name of the attribute.
Definition TextInterfaceEstimate.h:41
Attribute< V > * clone() const
Retun a newly constructed copy.
Definition TextInterfaceEstimate.h:38
bool matches(const std::string &name) const
Return true if the name argument matches.
Definition TextInterfaceEstimate.h:175
void set_description(const std::string &d)
Get the description of the attribute.
Definition TextInterfaceEstimate.h:48
std::string range
Range parsed from name during matches.
Definition TextInterfaceEstimate.h:102
Get get
Method of V that returns Estimate<T,U>
Definition TextInterfaceEstimate.h:84
std::string prefix
The name of the vector instance.
Definition TextInterfaceEstimate.h:90
void set_detailed_description(const std::string &d)
Get the detailed description of the attribute.
Definition TextInterfaceEstimate.h:55

Generated using doxygen 1.14.0