Jpp  18.0.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTemplate.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JTEMPLATE__
2 #define __JLANG__JTEMPLATE__
3 
4 #include <istream>
5 #include <ostream>
6 
7 #include "JLang/JTypeList.hh"
8 #include "JLang/JConversion.hh"
9 #include "JLang/JConstructor.hh"
10 #include "JLang/JBool.hh"
11 
12 
13 /**
14  * \file
15  *
16  * Auxiliary class to alleviate the so-called diamond problem due to multiple inheritance.
17  * \author mdejong
18  */
19 namespace JLANG {}
20 namespace JPP { using namespace JLANG; }
21 
22 namespace JLANG {
23 
24  /**
25  * Auxiliary class for managing multiple objects.
26  *
27  * This class can be used to replace regular base classes by corresponding data members.
28  * The various operators and methods facilitate the access to these data members.
29  *
30  * The template value <tt>use_polymorfism</tt> in methods <tt>get</tt> and <tt>set</tt>
31  * applies to the given template parameter of these methods.
32  * If <tt>false</tt>, only exact type matches are allowed.
33  * If <tt>true</tt>,
34  * method <tt>get</tt> will return a reference to the first data member
35  * of which the type derives from the specified class and
36  * method <tt>set</tt> will assign the given value to the first data member
37  * for which a matching constructor exists.
38  */
39  template<class JType_t>
40  struct JTemplate {
41 
43 
44 
45  /**
46  * Default constructor.
47  */
49  object()
50  {}
51 
52 
53  /**
54  * Constructor.
55  *
56  * \param value value
57  */
58  JTemplate(const JType_t& value) :
59  object(value)
60  {}
61 
62 
63  /**
64  * Type conversion.
65  *
66  * \return reference to object
67  */
68  inline operator const JType_t&() const
69  {
70  return object;
71  }
72 
73 
74  /**
75  * Type conversion.
76  *
77  * \return reference to object
78  */
79  inline operator JType_t&()
80  {
81  return object;
82  }
83 
84 
85  /**
86  * Smart pointer operator.
87  *
88  * \return pointer to object
89  */
90  const JType_t* operator->() const
91  {
92  return &object;
93  }
94 
95 
96  /**
97  * Smart pointer operator.
98  *
99  * \return pointer to object
100  */
101  JType_t* operator->()
102  {
103  return &object;
104  }
105 
106 
107  /**
108  * Get reference to object
109  *
110  * \return reference to object
111  */
112  template<class T, bool use_polymorphism = false>
113  const T& get() const
114  {
116  }
117 
118 
119  /**
120  * Get reference to object
121  *
122  * \return reference to object
123  */
124  template<class T, bool use_polymorphism = false>
125  T& get()
126  {
128  }
129 
130 
131  /**
132  * Set value.
133  *
134  * \param value value
135  * \return this object
136  */
137  inline template_type& set(const JType_t& value)
138  {
139  return c_set(value, JBool<true>());
140  }
141 
142 
143  /**
144  * Set value.
145  *
146  * \param value value
147  * \return this object
148  */
149  template<bool use_polymorphism, class T>
150  inline template_type& set(const T& value)
151  {
153  }
154 
155 
156  /**
157  * Read object from input.
158  *
159  * \param in input stream
160  * \param object object
161  * \return input stream
162  */
163  friend inline std::istream& operator>>(std::istream& in, JTemplate<JType_t>& object)
164  {
165  return in >> object.object;
166  }
167 
168 
169  /**
170  * Write object to output.
171  *
172  * \param out output stream
173  * \param object object
174  * \return output stream
175  */
176  friend inline std::ostream& operator<<(std::ostream& out, const JTemplate<JType_t>& object)
177  {
178  return out << object.object;
179  }
180 
181  protected:
182  /**
183  * Get reference to object
184  *
185  * \param option true
186  * \return reference to object
187  */
188  const JType_t& c_get(JBool<true> option) const
189  {
190  return object;
191  }
192 
193 
194  /**
195  * Get reference to object
196  *
197  * \param option true
198  * \return reference to object
199  */
200  JType_t& c_get(JBool<true> option)
201  {
202  return object;
203  }
204 
205 
206  /**
207  * Set value.
208  *
209  * \param value value
210  * \param option true
211  * \return this object
212  */
213  template<class T>
214  inline template_type& c_set(const T& value, JBool<true> option)
215  {
216  object = value;
217 
218  return *this;
219  }
220 
221 
222  JType_t object; //!< object
223  };
224 
225 
226  /**
227  * Auxiliary class for managing multiple objects.
228  */
229  template<class JHead_t, class JTail_t>
230  struct JTemplate< JTypeList<JHead_t, JTail_t> > :
231  public JTemplate<JHead_t>,
232  public JTemplate<JTail_t>
233  {
234 
236 
237 
238  /**
239  * Default constructor.
240  */
242  JTemplate<JHead_t>(),
243  JTemplate<JTail_t>()
244  {}
245 
246 
247  /**
248  * Constructor.
249  *
250  * \param head value
251  * \param tail value
252  */
253  JTemplate(const JHead_t& head,
254  const JTail_t& tail) :
255  JTemplate<JHead_t>(head),
256  JTemplate<JTail_t>(tail)
257  {}
258 
259 
260  /**
261  * Get reference to object
262  *
263  * \return reference to object
264  */
265  template<class T, bool use_polymorphism = false>
266  const T& get() const
267  {
268  return this->c_get<T, use_polymorphism>(JBool<use_polymorphism>().template c_switch<JConversion<JHead_t, T>::is_derived, JConversion<JHead_t, T>::is_same>());
269  }
270 
271 
272  /**
273  * Get reference to object
274  *
275  * \return reference to object
276  */
277  template<class T, bool use_polymorphism = false>
278  T& get()
279  {
280  return this->c_get<T, use_polymorphism>(JBool<use_polymorphism>().template c_switch<JConversion<JHead_t, T>::is_derived, JConversion<JHead_t, T>::is_same>());
281  }
282 
283 
284  /**
285  * Set value.
286  *
287  * \param value value
288  * \return this object
289  */
290  template<class T>
291  inline template_type& set(const T& value)
292  {
293  return this->c_set<false>(value, JBool<false>::template c_switch<JConstructor<JHead_t, T>::has_constructor, JConversion<JHead_t, T>::is_same>());
294  }
295 
296 
297  /**
298  * Set value.
299  *
300  * \param value value
301  * \return this object
302  */
303  template<bool use_polymorphism, class T>
304  inline template_type& set(const T& value)
305  {
306  return this->c_set<use_polymorphism>(value, JBool<use_polymorphism>::template c_switch<JConstructor<JHead_t, T>::has_constructor, JConversion<JHead_t, T>::is_same>());
307  }
308 
309 
310  /**
311  * Read object from input.
312  *
313  * \param in input stream
314  * \param object object
315  * \return input stream
316  */
317  friend inline std::istream& operator>>(std::istream& in, JTemplate< JTypeList<JHead_t, JTail_t> >& object)
318  {
319  return in >> static_cast<JTemplate<JHead_t>&>(object)
320  >> static_cast<JTemplate<JTail_t>&>(object);
321  }
322 
323 
324  /**
325  * Write object to output.
326  *
327  * \param out output stream
328  * \param object object
329  * \return output stream
330  */
331  friend inline std::ostream& operator<<(std::ostream& out, const JTemplate< JTypeList<JHead_t, JTail_t> >& object)
332  {
333  return out << static_cast<const JTemplate<JHead_t>&>(object) << ' '
334  << static_cast<const JTemplate<JTail_t>&>(object);
335  }
336 
337  protected:
338  /**
339  * Get reference to object
340  *
341  * \param option true
342  * \return reference to object
343  */
344  template<class T, bool use_polymorphism>
345  const T& c_get(JBool<true> option) const
346  {
347  return static_cast<JTemplate<JHead_t>&>(*this).template get<T, use_polymorphism>();
348  }
349 
350 
351  /**
352  * Get reference to object
353  *
354  * \param option false
355  * \return reference to object
356  */
357  template<class T, bool use_polymorphism>
358  const T& c_get(JBool<false> option) const
359  {
360  return static_cast<JTemplate<JTail_t>&>(*this).template get<T, use_polymorphism>();
361  }
362 
363 
364  /**
365  * Get reference to object
366  *
367  * \param option true
368  * \return reference to object
369  */
370  template<class T, bool use_polymorphism>
371  T& c_get(JBool<true> option)
372  {
373  return static_cast<JTemplate<JHead_t>&>(*this).template get<T, use_polymorphism>();
374  }
375 
376 
377  /**
378  * Get reference to object
379  *
380  * \param option false
381  * \return reference to object
382  */
383  template<class T, bool use_polymorphism>
385  {
386  return static_cast<JTemplate<JTail_t>&>(*this).template get<T, use_polymorphism>();
387  }
388 
389 
390  /**
391  * Set value.
392  *
393  * \param value value
394  * \param option true
395  * \return this object
396  */
397  template<bool use_polymorphism, class T>
398  inline template_type& c_set(const T& value, JBool<true> option)
399  {
400  static_cast<JTemplate<JHead_t>&>(*this).template set<use_polymorphism>(value);
401 
402  return *this;
403  }
404 
405 
406  /**
407  * Set value.
408  *
409  * \param value value
410  * \param option false
411  * \return this object
412  */
413  template<bool use_polymorphism, class T>
414  inline template_type& c_set(const T& value, JBool<false> option)
415  {
416  static_cast<JTemplate<JTail_t>&>(*this).template set<use_polymorphism>(value);
417 
418  return *this;
419  }
420  };
421 
422 
423  /**
424  * Auxiliary class for managing multiple objects.
425  */
426  template<class JHead_t>
427  struct JTemplate< JTypeList<JHead_t> > :
428  public JTemplate<JHead_t>
429  {
430  /**
431  * Default constructor.
432  */
434  JTemplate<JHead_t>()
435  {}
436 
437 
438  /**
439  * Constructor.
440  *
441  * \param value value
442  */
443  JTemplate(const JHead_t& value) :
444  JTemplate<JHead_t>(value)
445  {}
446  };
447 }
448 
449 #endif
const T & c_get(JBool< false > option) const
Get reference to object.
Definition: JTemplate.hh:358
template_type & set(const T &value)
Set value.
Definition: JTemplate.hh:291
JTemplate< JTypeList< JHead_t, JTail_t > > template_type
Definition: JTemplate.hh:235
const JType_t * operator->() const
Smart pointer operator.
Definition: JTemplate.hh:90
const JType_t & c_get(JBool< true > option) const
Get reference to object.
Definition: JTemplate.hh:188
JType_t object
object
Definition: JTemplate.hh:222
JTemplate()
Default constructor.
Definition: JTemplate.hh:48
template_type & c_set(const T &value, JBool< true > option)
Set value.
Definition: JTemplate.hh:214
Template class test for polymorphism.
Definition: JConversion.hh:21
Auxiliary class for managing multiple objects.
Definition: JTemplate.hh:40
Auxiliary class for managing multiple objects.
Definition: JTemplate.hh:230
friend std::istream & operator>>(std::istream &in, JTemplate< JType_t > &object)
Read object from input.
Definition: JTemplate.hh:163
template_type & c_set(const T &value, JBool< false > option)
Set value.
Definition: JTemplate.hh:414
friend std::istream & operator>>(std::istream &in, JTemplate< JTypeList< JHead_t, JTail_t > > &object)
Read object from input.
Definition: JTemplate.hh:317
JTemplate(const JHead_t &head, const JTail_t &tail)
Constructor.
Definition: JTemplate.hh:253
T & c_get(JBool< true > option)
Get reference to object.
Definition: JTemplate.hh:371
Type list.
Definition: JTypeList.hh:22
template_type & set(const T &value)
Set value.
Definition: JTemplate.hh:150
Auxiliary template class for type bool.
Definition: JBool.hh:20
do set_variable OUTPUT_DIRECTORY $WORKDIR T
template_type & set(const JType_t &value)
Set value.
Definition: JTemplate.hh:137
template_type & set(const T &value)
Set value.
Definition: JTemplate.hh:304
JType_t & c_get(JBool< true > option)
Get reference to object.
Definition: JTemplate.hh:200
JTemplate(const JType_t &value)
Constructor.
Definition: JTemplate.hh:58
JType_t * operator->()
Smart pointer operator.
Definition: JTemplate.hh:101
template_type & c_set(const T &value, JBool< true > option)
Set value.
Definition: JTemplate.hh:398
Template class test for availability of a suitable constructor JType_t(const JArgument_t).
Definition: JConstructor.hh:19
JTemplate< JType_t > template_type
Definition: JTemplate.hh:42
const T & c_get(JBool< true > option) const
Get reference to object.
Definition: JTemplate.hh:345
T & c_get(JBool< false > option)
Get reference to object.
Definition: JTemplate.hh:384
JTemplate(const JHead_t &value)
Constructor.
Definition: JTemplate.hh:443
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46