All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UnsignedValue.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_UTIL_UNSIGNEDVALUE_H_
23 #define _BLAZE_UTIL_UNSIGNEDVALUE_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <istream>
31 #include <ostream>
33 
34 
35 namespace blaze {
36 
37 //=================================================================================================
38 //
39 // CLASS DEFINITION
40 //
41 //=================================================================================================
42 
43 //*************************************************************************************************
50 template< typename T > // Type of the unsigned value
52 {
53  public:
54  //**Constructors********************************************************************************
57  explicit inline UnsignedValue( T value=0 );
58  // No explicitly declared copy constructor.
60  //**********************************************************************************************
61 
62  //**Destructor**********************************************************************************
63  // No explicitly declared destructor.
64  //**********************************************************************************************
65 
66  //**Assignment operator*************************************************************************
69  inline UnsignedValue& operator=( T value );
70  // No explicitly declared copy assignment operator.
72  //**********************************************************************************************
73 
74  //**Conversion operator*************************************************************************
77  inline operator T() const;
79  //**********************************************************************************************
80 
81  //**Access function*****************************************************************************
84  inline T get() const;
86  //**********************************************************************************************
87 
88  private:
89  //**Member variables****************************************************************************
92  T value_;
93 
94  //**********************************************************************************************
95 
96  //**Compile time checks*************************************************************************
100  //**********************************************************************************************
101 };
102 //*************************************************************************************************
103 
104 
105 
106 
107 //=================================================================================================
108 //
109 // CONSTRUCTOR
110 //
111 //=================================================================================================
112 
113 //*************************************************************************************************
118 template< typename T > // Type of the unsigned value
120  : value_( value ) // The wrapped built-in unsigned integral value
121 {}
122 //*************************************************************************************************
123 
124 
125 
126 
127 //=================================================================================================
128 //
129 // ASSIGNMENT OPERATOR
130 //
131 //=================================================================================================
132 
133 //*************************************************************************************************
139 template< typename T > // Type of the unsigned value
141 {
142  value_ = value;
143  return *this;
144 }
145 //*************************************************************************************************
146 
147 
148 
149 
150 //=================================================================================================
151 //
152 // CONVERSION OPERATOR
153 //
154 //=================================================================================================
155 
156 //*************************************************************************************************
161 template< typename T > // Type of the unsigned value
163 {
164  return value_;
165 }
166 //*************************************************************************************************
167 
168 
169 
170 
171 //=================================================================================================
172 //
173 // ACCESS FUNCTIONS
174 //
175 //=================================================================================================
176 
177 //*************************************************************************************************
182 template< typename T > // Type of the unsigned value
183 inline T UnsignedValue<T>::get() const
184 {
185  return value_;
186 }
187 //*************************************************************************************************
188 
189 
190 
191 
192 //=================================================================================================
193 //
194 // GLOBAL OPERATORS
195 //
196 //=================================================================================================
197 
198 //*************************************************************************************************
201 template< typename T1, typename T2 >
202 inline bool operator==( const UnsignedValue<T1>& lhs, const UnsignedValue<T2>& rhs );
203 
204 template< typename T1, typename T2 >
205 inline bool operator!=( const UnsignedValue<T1>& lhs, const UnsignedValue<T2>& rhs );
206 
207 template< typename T1, typename T2 >
208 inline bool operator< ( const UnsignedValue<T1>& lhs, const UnsignedValue<T2>& rhs );
209 
210 template< typename T1, typename T2 >
211 inline bool operator> ( const UnsignedValue<T1>& lhs, const UnsignedValue<T2>& rhs );
212 
213 template< typename T1, typename T2 >
214 inline bool operator<=( const UnsignedValue<T1>& lhs, const UnsignedValue<T2>& rhs );
215 
216 template< typename T1, typename T2 >
217 inline bool operator>=( const UnsignedValue<T1>& lhs, const UnsignedValue<T2>& rhs );
218 
219 template< typename T >
220 inline std::ostream& operator<<( std::ostream& os, const UnsignedValue<T>& uv );
221 
222 template< typename T >
223 std::istream& operator>>( std::istream& is, UnsignedValue<T>& uv );
225 //*************************************************************************************************
226 
227 
228 //*************************************************************************************************
235 template< typename T1 // Type of the left-hand side unsigned value
236  , typename T2 > // Type of the right-hand side unsigned value
237 inline bool operator==( const UnsignedValue<T1>& lhs, const UnsignedValue<T2>& rhs )
238 {
239  return lhs.get() == rhs.get();
240 }
241 //*************************************************************************************************
242 
243 
244 //*************************************************************************************************
251 template< typename T1 // Type of the left-hand side unsigned value
252  , typename T2 > // Type of the right-hand side unsigned value
253 inline bool operator!=( const UnsignedValue<T1>& lhs, const UnsignedValue<T2>& rhs )
254 {
255  return lhs.get() != rhs.get();
256 }
257 //*************************************************************************************************
258 
259 
260 //*************************************************************************************************
267 template< typename T1 // Type of the left-hand side unsigned value
268  , typename T2 > // Type of the right-hand side unsigned value
269 inline bool operator<( const UnsignedValue<T1>& lhs, const UnsignedValue<T2>& rhs )
270 {
271  return lhs.get() < rhs.get();
272 }
273 //*************************************************************************************************
274 
275 
276 //*************************************************************************************************
283 template< typename T1 // Type of the left-hand side unsigned value
284  , typename T2 > // Type of the right-hand side unsigned value
285 inline bool operator>( const UnsignedValue<T1>& lhs, const UnsignedValue<T2>& rhs )
286 {
287  return lhs.get() > rhs.get();
288 }
289 //*************************************************************************************************
290 
291 
292 //*************************************************************************************************
299 template< typename T1 // Type of the left-hand side unsigned value
300  , typename T2 > // Type of the right-hand side unsigned value
301 inline bool operator<=( const UnsignedValue<T1>& lhs, const UnsignedValue<T2>& rhs )
302 {
303  return lhs.get() <= rhs.get();
304 }
305 //*************************************************************************************************
306 
307 
308 //*************************************************************************************************
315 template< typename T1 // Type of the left-hand side unsigned value
316  , typename T2 > // Type of the right-hand side unsigned value
317 inline bool operator>=( const UnsignedValue<T1>& lhs, const UnsignedValue<T2>& rhs )
318 {
319  return lhs.get() >= rhs.get();
320 }
321 //*************************************************************************************************
322 
323 
324 //*************************************************************************************************
331 template< typename T > // Type of the unsigned value
332 inline std::ostream& operator<<( std::ostream& os, const UnsignedValue<T>& uv )
333 {
334  return os << uv.get();
335 }
336 //*************************************************************************************************
337 
338 
339 //*************************************************************************************************
351 template< typename T > // Type of the unsigned value
352 std::istream& operator>>( std::istream& is, UnsignedValue<T>& uv )
353 {
354  T tmp;
355  const std::istream::pos_type pos( is.tellg() );
356 
357  // Skipping any leading whitespaces
358  is >> std::ws;
359 
360  // Extracting the value
361  if( is.peek() == '-' || !(is >> tmp) )
362  {
363  is.clear();
364  is.seekg( pos );
365  is.setstate( std::istream::failbit );
366  return is;
367  }
368 
369  // Transfering the input to the unsigned integer value
370  uv = tmp;
371 
372  return is;
373 }
374 //*************************************************************************************************
375 
376 } // namespace blaze
377 
378 #endif