bpp-core3  3.0.0
Number.h
Go to the documentation of this file.
1 //
2 // File: Number.h
3 // Authors:
4 // Julien Dutheil
5 // Created: 2003-11-13 16:29:03
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
10 
11  This software is a computer program whose purpose is to provide utilitary
12  classes. This file belongs to the Bio++ Project.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 #ifndef BPP_NUMERIC_NUMBER_H
42 #define BPP_NUMERIC_NUMBER_H
43 
44 #include <string>
45 
46 #include "../Clonable.h"
47 #include "../Text/TextTools.h"
48 
49 namespace bpp
50 {
56 class BppNumberI : public Clonable
57 {
58 public:
60 
61  virtual ~BppNumberI() {}
62 
63 public:
64  virtual BppNumberI* clone() const = 0;
65 
66 public:
67  virtual std::string toString() const = 0;
68 };
69 
70 
71 class BppNotANumber : public virtual BppNumberI
72 {
73 public:
75 
76  virtual ~BppNotANumber() {}
77 
78 public:
79  virtual BppNotANumber* clone() const { return new BppNotANumber(); }
80 
81 public:
82  virtual std::string toString() const { return "NaN"; }
83 };
84 
85 
91 template<class T> class Number : public virtual BppNumberI
92 {
93 protected:
95  T value_;
96 
97 public:
103  Number(const T& value = 0) : value_(value) {}
104 
105  virtual ~Number() {}
106 
107  Number<T>& operator=(const T& t)
108  {
109  value_ = t;
110  return *this;
111  }
112 
113 public:
119  Number<T>* clone() const { return new Number<T>(value_); }
122 public:
128  T getValue() const { return value_; }
129 
130  std::string toString() const { return TextTools::toString(value_); }
131 };
132 
136 class BppDouble : public virtual Number<double>
137 {
138 public:
144  BppDouble(double value = 0) : Number<double>(value) {}
145 
146  virtual ~BppDouble() {}
147 
148 public:
154  BppDouble* clone() const { return new BppDouble(*this); }
156 };
157 
161 class BppInteger : public virtual Number<int>
162 {
163 public:
169  BppInteger(int value = 0) : Number<int>(value) {}
170 
171  virtual ~BppInteger() {}
172 
173 public:
179  BppInteger* clone() const { return new BppInteger(*this); }
181 };
182 
186 class BppUnsignedInteger : public virtual Number<unsigned int>
187 {
188 public:
194  BppUnsignedInteger(unsigned int value = 0) : Number<unsigned int>(value) {}
195 
196  virtual ~BppUnsignedInteger() {}
197 
198 public:
204  BppUnsignedInteger* clone() const { return new BppUnsignedInteger(*this); }
206 };
207 } // end of namespace bpp.
208 #endif // BPP_NUMERIC_NUMBER_H
An object wrapper for double values.
Definition: Number.h:137
virtual ~BppDouble()
Definition: Number.h:146
BppDouble(double value=0)
Build a new BppDouble number object with a specific value.
Definition: Number.h:144
BppDouble * clone() const
Create a copy of this object and send a pointer to it.
Definition: Number.h:154
An object wrapper for integer values.
Definition: Number.h:162
BppInteger * clone() const
Create a copy of this object and send a pointer to it.
Definition: Number.h:179
virtual ~BppInteger()
Definition: Number.h:171
BppInteger(int value=0)
Build a new BppInteger number object with a specific value.
Definition: Number.h:169
virtual std::string toString() const
Definition: Number.h:82
virtual BppNotANumber * clone() const
Create a copy of this object and send a pointer to it.
Definition: Number.h:79
virtual ~BppNotANumber()
Definition: Number.h:76
The Number interface.
Definition: Number.h:57
virtual BppNumberI * clone() const =0
Create a copy of this object and send a pointer to it.
virtual std::string toString() const =0
virtual ~BppNumberI()
Definition: Number.h:61
An object wrapper for unsigned integer values.
Definition: Number.h:187
BppUnsignedInteger * clone() const
Create a copy of this object and send a pointer to it.
Definition: Number.h:204
virtual ~BppUnsignedInteger()
Definition: Number.h:196
BppUnsignedInteger(unsigned int value=0)
Build a new BppUnsignedInteger number object with a specific value.
Definition: Number.h:194
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:103
The Number object template class.
Definition: Number.h:92
virtual ~Number()
Definition: Number.h:105
T value_
The value of this parameter.
Definition: Number.h:95
Number(const T &value=0)
Build a new Number object with a specific value.
Definition: Number.h:103
Number< T > & operator=(const T &t)
Definition: Number.h:107
std::string toString() const
Definition: Number.h:130
Number< T > * clone() const
Create a copy of this object and send a pointer to it.
Definition: Number.h:119
T getValue() const
Get the value of this number.
Definition: Number.h:128
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:153