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 
34 namespace ucommon {
35 
47 class __EXPORT Number
48 {
49 protected:
50  char *buffer;
51  unsigned size;
52 
53 public:
59  Number(char *buffer, unsigned size);
60 
65  void set(long value);
66 
71  inline const char *c_str() const
72  {return buffer;}
73 
78  long get() const;
79 
84  inline long operator()() const
85  {return get();}
86 
91  inline operator long() const
92  {return get();}
93 
98  inline operator char*() const
99  {return buffer;}
100 
106  long operator=(long value);
107 
113  long operator=(const Number& number);
114 
120  long operator+=(const long value);
121 
127  long operator-=(const long value);
128 
133  long operator--();
134 
139  long operator++();
140 
141  inline bool operator==(const long value) const
142  {return get() == value;}
143 
144  inline bool operator!=(const long value) const
145  {return get() != value;}
146 
147  inline bool operator<(const long value) const
148  {return get() < value;}
149 
150  inline bool operator>(const long value) const
151  {return get() > value;}
152 
153  inline bool operator<=(const long value) const
154  {return get() <= value;}
155 
156  inline bool operator>=(const long value) const
157  {return get() >= value;}
158 };
159 
166 class __EXPORT ZNumber : public Number
167 {
168 public:
174  ZNumber(char *pointer, unsigned size);
175 
181  void set(long value);
182 
188  long operator=(long value);
189 };
190 
194 typedef Number number_t;
195 
200 
206 template<typename T>
207 inline const T abs(const T& value)
208 {
209  if(value < (T)0)
210  return -value;
211  return value;
212 }
213 
214 
221 template<typename T>
222 inline const T (min)(const T& v1, const T& v2)
223 {
224  return ((v1 < v2) ? v1 : v2);
225 }
226 
233 template<typename T>
234 inline const T (max)(const T& v1, const T& v2)
235 {
236  return ((v1 > v2) ? v1 : v2);
237 }
238 
239 } // namespace ucommon
240 
241 #endif
Various miscellaneous platform specific headers and defines.
const T abs(const T &value)
Template for absolute value of a type.
Definition: numbers.h:207
A number manipulation class.
Definition: numbers.h:47
Number number_t
A convenience type for number.
Definition: numbers.h:194
Generic smart pointer class.
Definition: generics.h:54
Common namespace for all ucommon objects.
Definition: access.h:47
const char * c_str() const
Get string buffer representing the number.
Definition: numbers.h:71
A number manipulation class that maintains a zero lead filled string.
Definition: numbers.h:166
long operator()() const
Get value of string buffer as expression of object.
Definition: numbers.h:84
T &() min(T &o1, T &o2)
Convenience function to return min of two objects.
Definition: generics.h:425
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
Definition: generics.h:414
ZNumber znumber_t
A convenience type for znumber.
Definition: numbers.h:199