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 
21 namespace 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 = c;
37  for (unsigned i=0; i<values.size(); i++)
38  setup (values[i]);
39  }
40 
42  void setup (const Value* value)
43  {
44 #if _DEBUG
45  std::cerr << "TextInterface::To<C>::setup"
46  " name=" << value->get_name() <<
47  " instance=" << (void*) instance.ptr() << std::endl;
48 #endif
49  const Attribute<C>* attribute = dynamic_cast<const Attribute<C>*>(value);
50  if (attribute)
51  attribute->instance = instance.ptr();
52 #if _DEBUG
53  else
54  std::cerr << "TextInterface::To<C>::setup " << value->get_name() <<
55  " is not an Attribute<C>" << std::endl;
56 #endif
57  }
58 
59  void add_value( Attribute<C>* value )
60  {
61  if (instance)
62  value->instance = instance;
63 
64  Parser::add_value( value );
65  }
66 
68  template<class P>
69  void import (const To<P>* parent)
70  {
71  for (unsigned i=0; i < parent->size(); i++)
72  if (!import_filter || !found(parent->get(i)->get_name()))
73  add_value( new IsAProxy<C,P>(parent->get(i)) );
74  }
75 
77 
79  template<class M, class G>
80  void import (const std::string& name, const To<M>* member, G get)
81  {
82  for (unsigned i=0; i < member->size(); i++)
83  if (!import_filter || !found(name, member->get(i)->get_name()))
84  add_value( new HasAProxy<C,M,G>(name, member->get(i), get) );
85  }
86 
88 
92  template<class E, class G, class S>
93  void import (const std::string& name, const To<E>* member, G g, S s)
94  {
95  for (unsigned i=0; i < member->size(); i++)
96  {
97  const Attribute<E>* value = member->get(i);
98  if (!value)
99  continue;
100 
101 #ifdef _DEBUG
102  std::cerr << "TextInterface::import name="
103  << value->get_name() << std::endl;
104 #endif
105 
106  if (!import_filter || !found(name, value->get_name()))
107  add_value(new VectorOfProxy<C,E,G,S>(name, value, g, s));
108  }
109  }
110 
112 
115  template<class K, class E, class G>
116  void import (const std::string& name, K, const To<E>* member, G g)
117  {
118  for (unsigned i=0; i < member->size(); i++)
119  if (!import_filter || !found(name, member->get(i)->get_name()))
120  add_value( new MapOfProxy<C,K,E,G>(name, member->get(i), g) );
121  }
122 
124  template<class P>
125  void import (const To<P>& parent)
126  { import (&parent); }
127 
129  template<class M, class G>
130  void import ( const std::string& name, const To<M>& member, G get )
131  { import (name, &member, get); }
132 
134  template<class M, class G>
135  void import ( const To<M>& member, G get )
136  { import (std::string(), &member, get); }
137 
138  template<class E, class G, class S>
139  void import ( const std::string& name, const To<E>& element, G g, S size)
140  { import (name, &element, g, size); }
141 
142  template<class K, class E, class G>
143  void import ( const std::string& name, K k, const To<E>& element, G g)
144  { import (name, k, &element, g); }
145 
147  unsigned size () const { return values.size(); }
148 
150  const Attribute<C>* get (unsigned i) const
151  { return dynamic_cast< const Attribute<C>* >( values[i].get() ); }
152 
153  protected:
154 
156  template<class P, typename T>
157  void add (T(P::*get)()const,
158  const char* name, const char* description = 0)
159  {
160  Generator<T> gen;
161  Attribute<C>* getset = gen (name, get);
162  if (description)
163  getset->set_description (description);
164  add_value (getset);
165  }
166 
168  template<class P, typename T, typename U>
169  void add (T(P::*get)()const, void(P::*set)(const U&),
170  const char* name, const char* description = 0)
171  {
172  Generator<U> gen;
173  Attribute<C>* getset = gen (name, get, set);
174  if (description)
175  getset->set_description (description);
176  add_value (getset);
177  }
178 
180  template<class P, typename T, typename U>
181  void add (T(P::*get)()const, void(P::*set)(U),
182  const char* name, const char* description = 0)
183  {
184  Generator<U> gen;
185  Attribute<C>* getset = gen (name, get, set);
186  if (description)
187  getset->set_description (description);
188  add_value (getset);
189  }
190 
192  template<class P, typename T, typename U>
193  void add (T(P::*get)()const, void(P::*set)(const U&),
194  Parser*(P::*get_parser)(),
195  const char* name, const char* description = 0)
196  {
197  EmbedAllocator<C,U> gen;
198  Attribute<C>* getset = gen.direct (name, get, set, get_parser);
199  if (description)
200  getset->set_description (description);
201  add_value (getset);
202  }
203 
205  template<class P, typename T, typename U>
206  void add (T(P::*get)()const, void(P::*set)(U),
207  Parser*(P::*get_parser)(),
208  const char* name, const char* description = 0)
209  {
210  EmbedAllocator<C,U> gen;
211  Attribute<C>* getset = gen.direct (name, get, set, get_parser);
212  if (description)
213  getset->set_description (description);
214  add_value (getset);
215  }
216 
218  template<class P, typename U, typename Parent>
219  void add (U*(P::*get)()const, void(P::*set)(U*),
220  Parser*(Parent::*get_parser)(),
221  const char* name, const char* description = 0)
222  {
223  EmbedAllocator<C,U*> gen;
224  Attribute<C>* getset = gen.indirect (name, get, set, get_parser);
225  if (description)
226  getset->set_description (description);
227  add_value (getset);
228  }
229 
231  template<class Get, class Set, class Parser>
232  void add (Get get, Set set, Parser parser,
233  const char* name, const char* description = 0)
234  {
235  EmbedAllocator<C,typename Set::second_argument_type> gen;
236  Attribute<C>* getset = gen.direct (name, get, set, parser);
237  if (description)
238  getset->set_description (description);
239  add_value (getset);
240  }
241 
243  template<class Get>
244  void add (Get get, const char* name, const char* description = 0)
245  {
246  Attribute<C>* fget = new GetFunction<C,Get> (name, get);
247  if (description)
248  fget->set_description (description);
249  add_value (fget);
250  }
251 
253  template<class Get, class Set>
254  void add (Get get, Set set, const char* name, const char* description = 0)
255  {
256  Attribute<C>* fget = new GetSetFunction<C,Get,Set> (name, get, set);
257  if (description)
258  fget->set_description (description);
259  add_value (fget);
260  }
261 
264 
265  };
266 
267 }
268 
269 #endif
void add(T(P::*get)() const, const char *name, const char *description=0)
Factory generates a new AttributeGet instance.
Definition: TextInterfaceTo.h:167
ElementGet and ElementGetSet factory.
Definition: TextInterfaceElement.h:130
Proxy enables attribute interface of member to be used by class.
Definition: TextInterfaceProxy.h:61
Reference::To< C, false > instance
Pointer to the instance from which attribute value will be obtained.
Definition: TextInterfaceAttribute.h:65
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:114
virtual void set_description(const std::string &)=0
Set the description of the value.
std::vector< Reference::To< Value > > values
The vector of values.
Definition: TextInterfaceParser.h:156
An array of Value interfaces.
Definition: TextInterfaceParser.h:35
virtual void set_instance(C *c)
Set the instance.
Definition: TextInterfaceTo.h:44
bool found(const std::string &name) const
Return true if the named value is found.
Definition: TextInterfaceParser.C:287
Get is function object that receives C* and returns some type.
Definition: TextInterfaceFunction.h:28
void setup(const Value *value)
Set the instance of the Attribute<C>
Definition: TextInterfaceTo.h:52
void add(Get get, const char *name, const char *description=0)
Add adaptable unary function object template.
Definition: TextInterfaceTo.h:254
Parser()
Default constructor.
Definition: TextInterfaceParser.C:202
const Type * ptr() const
Return pointer without testing for validity.
Definition: ReferenceTo.h:234
const Attribute< C > * get(unsigned i) const
Provide access to the attributes.
Definition: TextInterfaceTo.h:160
Interface to a class attribute with an accessor and modifier methods.
Definition: TextInterfaceFunction.h:97
void add_value(Value *value)
Add a new value interface.
Definition: TextInterfaceParser.C:167
Text interface to a class attribute.
Definition: TextInterfaceAttribute.h:27
unsigned size() const
Return the number of attributes.
Definition: TextInterfaceTo.h:157
Reference::To< C, false > instance
The instance of the class with which this interfaces.
Definition: TextInterfaceTo.h:273
bool import_filter
Filter duplicate value names during import.
Definition: TextInterfaceParser.h:138
Class text interface: an instance of C and a vector of Attribute<C>
Definition: TextInterfaceTo.h:30

Generated using doxygen 1.8.17