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