TextInterfaceTo.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/Util/units/TextInterfaceTo.h
11
12#ifndef __TextInterfaceTo_h
13#define __TextInterfaceTo_h
14
15#include "TextInterfaceAttribute.h"
16#include "TextInterfaceFunction.h"
17#include "TextInterfaceElement.h"
18#include "TextInterfaceProxy.h"
19#include "TextInterfaceEmbed.h"
20
21namespace TextInterface
22{
24 template<class C>
25 class To : public Parser {
26
27 public:
28
29 template<class Type> class Generator : public Allocator<C,Type> { };
30
31 template<class Type> class VGenerator : public VAllocator<C,Type> { };
32
34 virtual void set_instance (C* c)
35 {
36 instance.set(c);
37
38 for (unsigned i=0; i<values.size(); i++)
39 setup (values[i]);
40 }
41
43 void setup (const Value* value)
44 {
45#if _DEBUG
46 std::cerr << "TextInterface::To<C>::setup"
47 " name=" << value->get_name() <<
48 " instance=" << (void*) instance.ptr() << std::endl;
49#endif
50 const Attribute<C>* attribute = dynamic_cast<const Attribute<C>*>(value);
51 if (attribute)
52 attribute->instance = instance;
53#if _DEBUG
54 else
55 std::cerr << "TextInterface::To<C>::setup " << value->get_name() <<
56 " is not an Attribute<C>" << std::endl;
57#endif
58 }
59
60 void add_value( Attribute<C>* value )
61 {
62 if (instance)
63 value->instance = instance;
64
65 Parser::add_value( value );
66 }
67
69 template<class P>
70 void import (const To<P>* parent)
71 {
72 for (unsigned i=0; i < parent->size(); i++)
73 if (!import_filter || !found(parent->get(i)->get_name()))
74 add_value( new IsAProxy<C,P>(parent->get(i)) );
75 }
76
78
80 template<class M, class G>
81 void import (const std::string& name, const To<M>* member, G get)
82 {
83 for (unsigned i=0; i < member->size(); i++)
84 if (!import_filter || !found(name, member->get(i)->get_name()))
85 add_value( new HasAProxy<C,M,G>(name, member->get(i), get) );
86 }
87
89
93 template<class E, class G, class S>
94 void import (const std::string& name, const To<E>* member, G g, S s)
95 {
96 for (unsigned i=0; i < member->size(); i++)
97 {
98 const Attribute<E>* value = member->get(i);
99 if (!value)
100 continue;
101
102#ifdef _DEBUG
103 std::cerr << "TextInterface::import name="
104 << value->get_name() << std::endl;
105#endif
106
107 if (!import_filter || !found(name, value->get_name()))
108 add_value(new VectorOfProxy<C,E,G,S>(name, value, g, s));
109 }
110 }
111
113
116 template<class K, class E, class G>
117 void import (const std::string& name, K, const To<E>* member, G g)
118 {
119 for (unsigned i=0; i < member->size(); i++)
120 if (!import_filter || !found(name, member->get(i)->get_name()))
121 add_value( new MapOfProxy<C,K,E,G>(name, member->get(i), g) );
122 }
123
125 template<class P>
126 void import (const To<P>& parent)
127 { import (&parent); }
128
130 template<class M, class G>
131 void import ( const std::string& name, const To<M>& member, G get )
132 { import (name, &member, get); }
133
135 template<class M, class G>
136 void import ( const To<M>& member, G get )
137 { import (std::string(), &member, get); }
138
139 template<class E, class G, class S>
140 void import ( const std::string& name, const To<E>& element, G g, S size)
141 { import (name, &element, g, size); }
142
143 template<class K, class E, class G>
144 void import ( const std::string& name, K k, const To<E>& element, G g)
145 { import (name, k, &element, g); }
146
148 unsigned size () const { return values.size(); }
149
151 const Attribute<C>* get (unsigned i) const
152 { return dynamic_cast< const Attribute<C>* >( values[i].get() ); }
153
154 protected:
155
157 template<class P, typename T>
158 void add (T(P::*get)()const,
159 const char* name, const char* description = 0)
160 {
161 Generator<T> gen;
162 Attribute<C>* getset = gen (name, get);
163 if (description)
164 getset->set_description (description);
165 add_value (getset);
166 }
167
169 template<class P, typename T, typename U>
170 void add (T(P::*get)()const, void(P::*set)(const U&),
171 const char* name, const char* description = 0)
172 {
173 Generator<U> gen;
174 Attribute<C>* getset = gen (name, get, set);
175 if (description)
176 getset->set_description (description);
177 add_value (getset);
178 }
179
181 template<class P, typename T, typename U>
182 void add (T(P::*get)()const, void(P::*set)(U),
183 const char* name, const char* description = 0)
184 {
185 Generator<U> gen;
186 Attribute<C>* getset = gen (name, get, set);
187 if (description)
188 getset->set_description (description);
189 add_value (getset);
190 }
191
193 template<class P, typename T, typename U>
194 void add (T(P::*get)()const, void(P::*set)(const U&),
195 Parser*(P::*get_parser)(),
196 const char* name, const char* description = 0)
197 {
198 EmbedAllocator<C,U> gen;
199 Attribute<C>* getset = gen.direct (name, get, set, get_parser);
200 if (description)
201 getset->set_description (description);
202 add_value (getset);
203 }
204
206 template<class P, typename T, typename U>
207 void add (T(P::*get)()const, void(P::*set)(U),
208 Parser*(P::*get_parser)(),
209 const char* name, const char* description = 0)
210 {
211 EmbedAllocator<C,U> gen;
212 Attribute<C>* getset = gen.direct (name, get, set, get_parser);
213 if (description)
214 getset->set_description (description);
215 add_value (getset);
216 }
217
219 template<class P, typename U, typename Parent>
220 void add (U*(P::*get)()const, void(P::*set)(U*),
221 Parser*(Parent::*get_parser)(),
222 const char* name, const char* description = 0)
223 {
224 EmbedAllocator<C,U*> gen;
225 Attribute<C>* getset = gen.indirect (name, get, set, get_parser);
226 if (description)
227 getset->set_description (description);
228 add_value (getset);
229 }
230
232 template<class Get, class Set, class Parser>
233 void add (Get get, Set set, Parser parser,
234 const char* name, const char* description = 0)
235 {
236 EmbedAllocator<C,typename Set::second_argument_type> gen;
237 Attribute<C>* getset = gen.direct (name, get, set, parser);
238 if (description)
239 getset->set_description (description);
240 add_value (getset);
241 }
242
244 template<class Get>
245 void add (Get get, const char* name, const char* description = 0)
246 {
247 Attribute<C>* fget = new GetFunction<C,Get> (name, get);
248 if (description)
249 fget->set_description (description);
250 add_value (fget);
251 }
252
254 template<class Get, class Set>
255 void add (Get get, Set set, const char* name, const char* description = 0)
256 {
257 Attribute<C>* fget = new GetSetFunction<C,Get,Set> (name, get, set);
258 if (description)
259 fget->set_description (description);
260 add_value (fget);
261 }
262
265
266 };
267
268}
269
270#endif
Template class manages Reference::Able objects.
Definition ReferenceTo.h:25
AttributeGet and AttributeGetSet factory.
Definition TextInterfaceAttribute.h:279
Text interface to a class attribute.
Definition TextInterfaceAttribute.h:23
Reference::To< C, false > instance
Pointer to the instance from which attribute value will be obtained.
Definition TextInterfaceAttribute.h:54
virtual void set_description(const std::string &)=0
Set the description of the value.
Get is function object that receives C* and returns some type.
Definition TextInterfaceFunction.h:23
Interface to a class attribute with an accessor and modifier methods.
Definition TextInterfaceFunction.h:93
Proxy enables attribute interface of member to be used by class.
Definition TextInterfaceProxy.h:57
Proxy enables attribute interface from parent to be used by child.
Definition TextInterfaceProxy.h:27
Proxy enables attribute interface of elements in a map.
Definition TextInterfaceProxy.h:162
std::vector< Reference::To< Value > > values
The vector of values.
Definition TextInterfaceParser.h:146
bool import_filter
Filter duplicate value names during import.
Definition TextInterfaceParser.h:128
void add_value(Value *value)
Add a new value interface.
Definition TextInterfaceParser.C:172
Parser()
Default constructor.
Definition TextInterfaceParser.C:205
bool found(const std::string &name) const
Return true if the named value is found.
Definition TextInterfaceParser.C:290
Class text interface: an instance of C and a vector of Attribute<C>
Definition TextInterfaceTo.h:25
Reference::To< C, false > instance
The instance of the class with which this interfaces.
Definition TextInterfaceTo.h:264
void add(Get get, const char *name, const char *description=0)
Add adaptable unary function object template.
Definition TextInterfaceTo.h:245
const Attribute< C > * get(unsigned i) const
Provide access to the attributes.
Definition TextInterfaceTo.h:151
void add(T(P::*get)() const, const char *name, const char *description=0)
Factory generates a new AttributeGet instance.
Definition TextInterfaceTo.h:158
void void void void void unsigned size() const
Import the attribute interfaces from a parent text interface.
Definition TextInterfaceTo.h:148
void add(Get get, Set set, const char *name, const char *description=0)
Add adaptable unary function object template.
Definition TextInterfaceTo.h:255
void i< parent-> size()
Import the attribute interfaces from a parent text interface.
void add(T(P::*get)() const, void(P::*set)(const U &), const char *name, const char *description=0)
Factory generates a new AttributeGetSet instance with description.
Definition TextInterfaceTo.h:170
virtual void set_instance(C *c)
Set the instance.
Definition TextInterfaceTo.h:34
void add(T(P::*get)() const, void(P::*set)(U), Parser *(P::*get_parser)(), const char *name, const char *description=0)
Factory generates a new DirectInterface instance with description.
Definition TextInterfaceTo.h:207
void add(T(P::*get)() const, void(P::*set)(const U &), Parser *(P::*get_parser)(), const char *name, const char *description=0)
Factory generates a new DirectInterface instance with description.
Definition TextInterfaceTo.h:194
void add(T(P::*get)() const, void(P::*set)(U), const char *name, const char *description=0)
Factory generates a new AttributeGetSet instance with description.
Definition TextInterfaceTo.h:182
void setup(const Value *value)
Set the instance of the Attribute<C>
Definition TextInterfaceTo.h:43
void add(U *(P::*get)() const, void(P::*set)(U *), Parser *(Parent::*get_parser)(), const char *name, const char *description=0)
Factory generates a new IndirectInterface instance with description.
Definition TextInterfaceTo.h:220
void add(Get get, Set set, Parser parser, const char *name, const char *description=0)
Factory generates a new DirectInterface instance with description.
Definition TextInterfaceTo.h:233
ElementGet and ElementGetSet factory.
Definition TextInterfaceElement.h:125
Text interface to a value of undefined type.
Definition TextInterfaceValue.h:31
virtual std::string get_name() const =0
Get the name of the value.
Proxy enables attribute interface of elements in a vector.
Definition TextInterfaceProxy.h:110

Generated using doxygen 1.14.0