Jpp
JParameter.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JPARAMETER__
2 #define __JLANG__JPARAMETER__
3 
4 #include <istream>
5 #include <ostream>
6 
7 #include "JLang/JClass.hh"
8 #include "JLang/JComparable.hh"
9 
10 
11 /**
12  * \author mdejong
13  */
14 
15 namespace JLANG {}
16 namespace JPP { using namespace JLANG; }
17 
18 namespace JLANG {
19 
20  /**
21  * Parameter class.
22  *
23  * This class is a simple wrapper around the template parameter with a status value.
24  * The status value indicates whether the parameter has been defined or not.
25  * A parameter is defined when a value has been assigned or correctly read.
26  * Note that the comparisons between parameter objects are based on the philosphy "undefined = any value".
27  * Hence, if any of the two parameter values is undefined, they are considered equal.
28  * The comparison between a paramater object with a template value is based
29  * on the internal value of the parameter object, regardless of its state.
30  */
31  template<class T>
32  class JParameter :
33  JComparable< JParameter<T> >
34  {
35  public:
36 
38 
39  /**
40  * Default constructor.
41  */
43  __value(),
44  is_defined(false)
45  {}
46 
47 
48  /**
49  * Constructor.
50  *
51  * \param value value
52  */
53  explicit JParameter(argument_type value) :
54  __value(value),
55  is_defined(true)
56  {}
57 
58 
59  /**
60  * Assignment operator.
61  *
62  * \param value value
63  * \return this parameter
64  */
66  {
67  setValue(value);
68 
69  return *this;
70  }
71 
72 
73  /**
74  * Get value of parameter.
75  *
76  * \return value
77  */
78  const T& getValue() const
79  {
80  return __value;
81  }
82 
83 
84  /**
85  * Get value of parameter.
86  *
87  * \return value
88  */
89  T& getValue()
90  {
91  return __value;
92  }
93 
94 
95  /**
96  * Set value.
97  *
98  * \param value value
99  */
101  {
102  __value = value;
103  is_defined = true;
104  }
105 
106 
107  /**
108  * Type conversion operator.
109  *
110  * \return value
111  */
112  operator const T&() const
113  {
114  return getValue();
115  }
116 
117 
118  /**
119  * Type conversion operator.
120  *
121  * \return value
122  */
123  operator T&()
124  {
125  return getValue();
126  }
127 
128 
129  /**
130  * Get status of parameter.
131  *
132  * \return true if value has been defined (by read or assignment); else false
133  */
134  const bool isDefined() const
135  {
136  return is_defined;
137  }
138 
139 
140  /**
141  * Less than method.
142  *
143  * This method evaluates to true if both parameter values are defined and
144  * this value is less than the value of the given parameter object.
145  *
146  * \param parameter parameter
147  * \return true if both defined and first value less than second value; else false
148  */
149  inline bool less(const JParameter& parameter) const
150  {
151  return this->isDefined() && parameter.isDefined() && this->getValue() < parameter.getValue();
152  }
153 
154 
155  /**
156  * Stream input.
157  *
158  * \param in input stream
159  * \param parameter parameter
160  * \return input stream
161  */
162  friend inline std::istream& operator>>(std::istream& in, JParameter<T>& parameter)
163  {
164  in >> parameter.__value;
165 
166  parameter.is_defined = (bool) in;
167 
168  return in;
169  }
170 
171 
172  /**
173  * Stream output.
174  *
175  * \param out output stream
176  * \param parameter parameter
177  * \return output stream
178  */
179  friend inline std::ostream& operator<<(std::ostream& out, const JParameter<T>& parameter)
180  {
181  return out << parameter.__value;
182  }
183 
184 
185  protected:
188  };
189 }
190 
191 #endif
JLANG::JParameter::operator<<
friend std::ostream & operator<<(std::ostream &out, const JParameter< T > &parameter)
Stream output.
Definition: JParameter.hh:179
JLANG::JClass::argument_type
JArgument< T >::argument_type argument_type
Definition: JClass.hh:82
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JLANG::JParameter::operator>>
friend std::istream & operator>>(std::istream &in, JParameter< T > &parameter)
Stream input.
Definition: JParameter.hh:162
JLANG::JParameter::getValue
const T & getValue() const
Get value of parameter.
Definition: JParameter.hh:78
JComparable.hh
JLANG::JParameter::__value
T __value
Definition: JParameter.hh:186
JLANG::JParameter::JParameter
JParameter(argument_type value)
Constructor.
Definition: JParameter.hh:53
JLANG::JParameter::JParameter
JParameter()
Default constructor.
Definition: JParameter.hh:42
JLANG::JParameter
Parameter class.
Definition: JParameter.hh:32
JLANG::JComparable
Template definition of auxiliary base class for comparison of data structures.
Definition: JComparable.hh:24
JLANG::JParameter::argument_type
JClass< T >::argument_type argument_type
Definition: JParameter.hh:37
JLANG::JParameter::setValue
void setValue(argument_type value)
Set value.
Definition: JParameter.hh:100
JClass.hh
JLANG
Auxiliary classes and methods for language specific functionality.
Definition: JAbstractClass.hh:10
JLANG::JParameter::is_defined
bool is_defined
Definition: JParameter.hh:187
JLANG::JParameter::isDefined
const bool isDefined() const
Get status of parameter.
Definition: JParameter.hh:134
JLANG::JParameter::getValue
T & getValue()
Get value of parameter.
Definition: JParameter.hh:89
JLANG::JParameter::less
bool less(const JParameter &parameter) const
Less than method.
Definition: JParameter.hh:149
JLANG::JParameter::operator=
JParameter< T > & operator=(argument_type value)
Assignment operator.
Definition: JParameter.hh:65