Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JZero.hh
Go to the documentation of this file.
1 #ifndef __JMATH__JZERO__
2 #define __JMATH__JZERO__
3 
4 /**
5  * \file
6  *
7  * Definition of zero value for any class.
8  * \author mdejong
9  */
10 namespace JMATH {}
11 namespace JPP { using namespace JMATH; }
12 
13 namespace JMATH {
14 
15  /**
16  * Get zero value for a given data type.
17  *
18  * The default implementation of this method returns an object which
19  * is created with the default constructor.
20  * This method should be specialised if this value does not correspond
21  * to the equivalent of a zero result.
22  *
23  * \return zero
24  */
25  template<class T>
26  inline T getZero()
27  {
28  return T();
29  }
30 
31 
32  /**
33  * Get zero value for float.
34  *
35  * \return zero
36  */
37  template<> inline float getZero<float>()
38  {
39  return float(0.0);
40  }
41 
42 
43  /**
44  * Get zero value for double.
45  *
46  * \return zero
47  */
48  template<>
49  inline double getZero<double>()
50  {
51  return double(0.0);
52  }
53 
54 
55  /**
56  * Get zero value for long double.
57  *
58  * \return zero
59  */
60  template<>
61  inline long double getZero<long double>()
62  {
63  return (long double)(0.0);
64  }
65 
66 
67  /**
68  * Auxiliary class to assign zero value.
69  */
70  struct JZero {
71  /**
72  * Default constructor.
73  */
75  {}
76 
77 
78  /**
79  * Type conversion operator.
80  *
81  * \return zero
82  */
83  template<class T>
84  operator T() const
85  {
86  return getZero<T>();
87  }
88  };
89 
90 
91  /**
92  * Function object to assign zero value.
93  */
94  static const JZero zero;
95 }
96 
97 #endif
double getZero< double >()
Get zero value for double.
Definition: JZero.hh:49
JZero()
Default constructor.
Definition: JZero.hh:74
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:94
T getZero()
Get zero value for a given data type.
Definition: JZero.hh:26
Auxiliary class to assign zero value.
Definition: JZero.hh:70
long double getZero< long double >()
Get zero value for long double.
Definition: JZero.hh:61
float getZero< float >()
Get zero value for float.
Definition: JZero.hh:37