bpp-core3  3.0.0
Number.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_NUMERIC_NUMBER_H
6 #define BPP_NUMERIC_NUMBER_H
7 
8 #include <string>
9 
10 #include "../Clonable.h"
11 #include "../Text/TextTools.h"
12 
13 namespace bpp
14 {
20 class BppNumberI : public Clonable
21 {
22 public:
24 
25  virtual ~BppNumberI() {}
26 
27 public:
28  virtual BppNumberI* clone() const = 0;
29 
30 public:
31  virtual std::string toString() const = 0;
32 };
33 
34 
35 class BppNotANumber : public virtual BppNumberI
36 {
37 public:
39 
40  virtual ~BppNotANumber() {}
41 
42 public:
43  virtual BppNotANumber* clone() const { return new BppNotANumber(); }
44 
45 public:
46  virtual std::string toString() const { return "NaN"; }
47 };
48 
49 
55 template<class T> class Number : public virtual BppNumberI
56 {
57 protected:
59  T value_;
60 
61 public:
67  Number(const T& value = 0) : value_(value) {}
68 
69  virtual ~Number() {}
70 
71  Number<T>& operator=(const T& t)
72  {
73  value_ = t;
74  return *this;
75  }
76 
77 public:
83  Number<T>* clone() const { return new Number<T>(value_); }
86 public:
92  T getValue() const { return value_; }
93 
94  std::string toString() const { return TextTools::toString(value_); }
95 };
96 
100 class BppDouble : public virtual Number<double>
101 {
102 public:
108  BppDouble(double value = 0) : Number<double>(value) {}
109 
110  virtual ~BppDouble() {}
111 
112 public:
118  BppDouble* clone() const { return new BppDouble(*this); }
120 };
121 
125 class BppInteger : public virtual Number<int>
126 {
127 public:
133  BppInteger(int value = 0) : Number<int>(value) {}
134 
135  virtual ~BppInteger() {}
136 
137 public:
143  BppInteger* clone() const { return new BppInteger(*this); }
145 };
146 
150 class BppUnsignedInteger : public virtual Number<unsigned int>
151 {
152 public:
158  BppUnsignedInteger(unsigned int value = 0) : Number<unsigned int>(value) {}
159 
160  virtual ~BppUnsignedInteger() {}
161 
162 public:
168  BppUnsignedInteger* clone() const { return new BppUnsignedInteger(*this); }
170 };
171 } // end of namespace bpp.
172 #endif // BPP_NUMERIC_NUMBER_H
std::string toString() const
Definition: Number.h:94
T getValue() const
Get the value of this number.
Definition: Number.h:92
BppInteger(int value=0)
Build a new BppInteger number object with a specific value.
Definition: Number.h:133
virtual std::string toString() const
Definition: Number.h:46
The Number interface.
Definition: Number.h:20
An object wrapper for double values.
Definition: Number.h:100
An object wrapper for integer values.
Definition: Number.h:125
Number< T > & operator=(const T &t)
Definition: Number.h:71
T value_
The value of this parameter.
Definition: Number.h:59
virtual ~BppNotANumber()
Definition: Number.h:40
virtual ~BppNumberI()
Definition: Number.h:25
virtual BppNotANumber * clone() const
Create a copy of this object and send a pointer to it.
Definition: Number.h:43
BppDouble * clone() const
Create a copy of this object and send a pointer to it.
Definition: Number.h:118
BppUnsignedInteger * clone() const
Create a copy of this object and send a pointer to it.
Definition: Number.h:168
virtual BppNumberI * clone() const =0
Create a copy of this object and send a pointer to it.
BppUnsignedInteger(unsigned int value=0)
Build a new BppUnsignedInteger number object with a specific value.
Definition: Number.h:158
virtual ~BppDouble()
Definition: Number.h:110
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:63
The Number object template class.
Definition: Number.h:55
BppDouble(double value=0)
Build a new BppDouble number object with a specific value.
Definition: Number.h:108
virtual ~Number()
Definition: Number.h:69
BppInteger * clone() const
Create a copy of this object and send a pointer to it.
Definition: Number.h:143
virtual std::string toString() const =0
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:115
Number< T > * clone() const
Create a copy of this object and send a pointer to it.
Definition: Number.h:83
An object wrapper for unsigned integer values.
Definition: Number.h:150
virtual ~BppUnsignedInteger()
Definition: Number.h:160
Number(const T &value=0)
Build a new Number object with a specific value.
Definition: Number.h:67
virtual ~BppInteger()
Definition: Number.h:135