Jpp  15.0.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JValve.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JVALVE__
2 #define __JLANG__JVALVE__
3 
4 #include "JLang/JDefault.hh"
5 #include "JLang/JTypeList.hh"
6 #include "JLang/JType.hh"
7 
8 /**
9  * \author mdejong
10  */
11 
12 namespace JLANG {}
13 namespace JPP { using namespace JLANG; }
14 
15 namespace JLANG {
16 
17  /**
18  * Auxiliary class for selection of data type.
19  */
20  template<class T>
21  struct JValve :
22  public JDefault< JValve<T> >
23  {
24  /**
25  * Default constructor.
26  *
27  * By default, the valve is open.
28  */
29  JValve() :
30  status(true)
31  {}
32 
33 
34  /**
35  * Constructor.
36  *
37  * \param controller controller of valve
38  */
39  template<class JController_t>
40  JValve(const JController_t& controller)
41  {
42  (*this)(controller);
43  }
44 
45 
46  /**
47  * Check if valve is open.
48  *
49  * \return true if open; else false
50  */
51  bool is_open() const
52  {
53  return status;
54  }
55 
56 
57  /**
58  * Open valve.
59  */
60  void open()
61  {
62  status = true;
63  }
64 
65 
66  /**
67  * Close valve.
68  */
69  void close()
70  {
71  status = false;
72  }
73 
74 
75  /**
76  * Set valve.
77  *
78  * The template argument <tt>JController_t</tt> refers to a data structure
79  * which should provide for the function object operator:
80  * <pre>
81  * bool operator()(JType<T>& type) const; // get status of valve
82  * </pre>
83  *
84  * \param controller controller of valve
85  */
86  template<class JController_t>
87  JValve& operator()(const JController_t& controller)
88  {
89  if (controller(JType<T>()))
90  this->open();
91  else
92  this->close();
93 
94  return *this;
95  }
96 
97  protected:
98  bool status;
99  };
100 
101 
102  /**
103  * Auxiliary class for selection of multiple data types.
104  *
105  * This class recursively defines the JLANG::JValve interface
106  * for all data types by deriving from:
107  * - JValve<JHead_t>; and
108  * - JValve<JTail_t>.
109  */
110  template<class JHead_t, class JTail_t>
111  struct JValve< JTypeList<JHead_t, JTail_t> > :
112  public JDefault< JValve< JTypeList<JHead_t, JTail_t> > >,
113  public JValve<JHead_t>,
114  public JValve<JTail_t>
115  {
117 
118  /**
119  * Default constructor.
120  */
122  {}
123 
124 
125  /**
126  * Constructor.
127  *
128  * \param controller controller of valve
129  */
130  template<class JController_t>
131  JValve(const JController_t& controller)
132  {
133  (*this)(controller);
134  }
135 
136 
137  /**
138  * Set valve.
139  *
140  * The template argument <tt>JController_t</tt> refers to a data structure
141  * which should provide for the function object operator:
142  * <pre>
143  * bool operator()(JType<T>& type) const; // get status of valve
144  * </pre>
145  *
146  * \param controller controller of valve
147  */
148  template<class JController_t>
149  JValve& operator()(const JController_t& controller)
150  {
151  static_cast<JValve<JHead_t>&>(*this)(controller);
152  static_cast<JValve<JTail_t>&>(*this)(controller);
153 
154  return *this;
155  }
156  };
157 
158 
159  /**
160  * Terminator class of recursive JValve class.
161  */
162  template<class JHead_t>
163  struct JValve< JTypeList<JHead_t, JNullType> > :
164  public JValve<JHead_t>
165  {
166  /**
167  * Default constructor.
168  */
170  {}
171 
172 
173  /**
174  * Constructor.
175  *
176  * \param controller controller of valve
177  */
178  template<class JController_t>
179  JValve(const JController_t& controller)
180  {
181  (*this)(controller);
182  }
183  };
184 }
185 
186 #endif
JValve & operator()(const JController_t &controller)
Set valve.
Definition: JValve.hh:87
static const JValve< T > & getDefault()
Get default value of template class.
Definition: JDefault.hh:24
JValve(const JController_t &controller)
Constructor.
Definition: JValve.hh:40
Auxiliary class for a type holder.
Definition: JType.hh:19
bool status
Definition: JValve.hh:98
Auxiliary class for selection of data type.
Type list.
Definition: JTypeList.hh:22
JValve()
Default constructor.
Definition: JValve.hh:29
void close()
Close valve.
Definition: JValve.hh:69
JValve(const JController_t &controller)
Constructor.
Definition: JValve.hh:131
Auxiliary class for no type definition.
Definition: JNullType.hh:19
JValve & operator()(const JController_t &controller)
Set valve.
Definition: JValve.hh:149
void open()
Open valve.
Definition: JValve.hh:60
bool is_open() const
Check if valve is open.
Definition: JValve.hh:51
JValve(const JController_t &controller)
Constructor.
Definition: JValve.hh:179
Simple default class.
Definition: JDefault.hh:18