UCommon
numbers.h
Go to the documentation of this file.
1// Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2// Copyright (C) 2015 Cherokees of Idaho.
3//
4// This file is part of GNU uCommon C++.
5//
6// GNU uCommon C++ is free software: you can redistribute it and/or modify
7// it under the terms of the GNU Lesser General Public License as published
8// by the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// GNU uCommon C++ is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU Lesser General Public License for more details.
15//
16// You should have received a copy of the GNU Lesser General Public License
17// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18
27#ifndef _UCOMMON_NUMBERS_H_
28#define _UCOMMON_NUMBERS_H_
29
30#ifndef _UCOMMON_CONFIG_H_
31#include <ucommon/platform.h>
32#endif
33
34namespace ucommon {
35
47class __EXPORT Number
48{
49protected:
50 char *buffer;
51 unsigned size;
52
53public:
59 Number(char *buffer, unsigned size);
60
65 void set(long value);
66
71 inline const char *c_str() const {
72 return buffer;
73 }
74
79 long get() const;
80
85 inline long operator()() const {
86 return get();
87 }
88
93 inline operator long() const {
94 return get();
95 }
96
101 inline operator char*() const {
102 return buffer;
103 }
104
110 long operator=(long value);
111
117 long operator=(const Number& number);
118
124 long operator+=(const long value);
125
131 long operator-=(const long value);
132
138
144
145 inline bool operator==(const long value) const {
146 return get() == value;
147 }
148
149 inline bool operator!=(const long value) const {
150 return get() != value;
151 }
152
153 inline bool operator<(const long value) const {
154 return get() < value;
155 }
156
157 inline bool operator>(const long value) const {
158 return get() > value;
159 }
160
161 inline bool operator<=(const long value) const {
162 return get() <= value;
163 }
164
165 inline bool operator>=(const long value) const {
166 return get() >= value;
167 }
168};
169
176class __EXPORT ZNumber : public Number
177{
178public:
184 ZNumber(char *pointer, unsigned size);
185
191 void set(long value);
192
198 long operator=(long value);
199};
200
205
210
216template<typename T>
217inline const T abs(const T& value)
218{
219 if(value < (T)0)
220 return -value;
221 return value;
222}
223
224
231template<typename T>
232inline const T (min)(const T& v1, const T& v2)
233{
234 return ((v1 < v2) ? v1 : v2);
235}
236
243template<typename T>
244inline const T (max)(const T& v1, const T& v2)
245{
246 return ((v1 > v2) ? v1 : v2);
247}
248
249} // namespace ucommon
250
251#endif
Various miscellaneous platform specific headers and defines.
Common namespace for all ucommon objects.
Definition access.h:47
T & max(T &o1, T &o2)
Convenience function to return max of two objects.
Definition generics.h:445
Number number_t
A convenience type for number.
Definition numbers.h:204
T & min(T &o1, T &o2)
Convenience function to return min of two objects.
Definition generics.h:456
ZNumber znumber_t
A convenience type for znumber.
Definition numbers.h:209
const T abs(const T &value)
Template for absolute value of a type.
Definition numbers.h:217
Generic smart pointer class.
Definition generics.h:55
A number manipulation class.
Definition numbers.h:48
long get() const
Get value of string buffer as a long integer.
const char * c_str() const
Get string buffer representing the number.
Definition numbers.h:71
long operator--()
Decrement the number object.
void set(long value)
Set string based on a new value.
long operator=(long value)
Assign a value to the number.
long operator()() const
Get value of string buffer as expression of object.
Definition numbers.h:85
Number(char *buffer, unsigned size)
Create an instance of a number.
long operator-=(const long value)
Subtract a value from the number.
long operator+=(const long value)
Add a value to the number.
long operator=(const Number &number)
Assign another number to this number.
long operator++()
Increment the number object.
A number manipulation class that maintains a zero lead filled string.
Definition numbers.h:177
long operator=(long value)
Assign number from value.
ZNumber(char *pointer, unsigned size)
Create a number class for zero fill.
void set(long value)
Set value of zero filled number.