ucommon
numbers.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
26 #ifndef _UCOMMON_NUMBERS_H_
27 #define _UCOMMON_NUMBERS_H_
28 
29 #ifndef _UCOMMON_CONFIG_H_
30 #include <ucommon/platform.h>
31 #endif
32 
33 NAMESPACE_UCOMMON
34 
46 class __EXPORT Number
47 {
48 protected:
49  char *buffer;
50  unsigned size;
51 
52 public:
58  Number(char *buffer, unsigned size);
59 
64  void set(long value);
65 
70  inline const char *c_str() const
71  {return buffer;};
72 
77  long get() const;
78 
83  inline long operator()()
84  {return get();};
85 
90  inline operator long()
91  {return get();};
92 
97  inline operator char*()
98  {return buffer;};
99 
105  long operator=(long value);
106 
112  long operator=(const Number& number);
113 
119  long operator+=(const long value);
120 
126  long operator-=(const long value);
127 
132  long operator--();
133 
138  long operator++();
139 
140  inline bool operator==(const long value) const
141  {return get() == value;}
142 
143  inline bool operator!=(const long value) const
144  {return get() != value;}
145 
146  inline bool operator<(const long value) const
147  {return get() < value;}
148 
149  inline bool operator>(const long value) const
150  {return get() > value;}
151 
152  inline bool operator<=(const long value) const
153  {return get() <= value;}
154 
155  inline bool operator>=(const long value) const
156  {return get() >= value;}
157 };
158 
165 class __EXPORT ZNumber : public Number
166 {
167 public:
173  ZNumber(char *pointer, unsigned size);
174 
180  void set(long value);
181 
187  long operator=(long value);
188 };
189 
193 typedef Number number_t;
194 
199 
205 template<typename T>
206 inline const T abs(const T& value)
207 {
208  if(value < (T)0)
209  return -value;
210  return value;
211 }
212 
213 
220 template<typename T>
221 inline const T (min)(const T& v1, const T& v2)
222 {
223  return ((v1 < v2) ? v1 : v2);
224 }
225 
232 template<typename T>
233 inline const T (max)(const T& v1, const T& v2)
234 {
235  return ((v1 > v2) ? v1 : v2);
236 }
237 
238 END_NAMESPACE
239 
240 #endif
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
Definition: generics.h:543
void set(long value)
Set string based on a new value.
long operator=(long value)
Assign a value to the number.
Various miscellaneous platform specific headers and defines.
ZNumber znumber_t
A convenience type for znumber.
Definition: numbers.h:198
A number manipulation class that maintains a zero lead filled string.
Definition: numbers.h:165
const T abs(const T &value)
Template for absolute value of a type.
Definition: numbers.h:206
const char * c_str() const
Get string buffer representing the number.
Definition: numbers.h:70
T &() min(T &o1, T &o2)
Convenience function to return min of two objects.
Definition: generics.h:555
Number number_t
A convenience type for number.
Definition: numbers.h:193
A number manipulation class.
Definition: numbers.h:46
long operator()()
Get value of string buffer as expression of object.
Definition: numbers.h:83
Generic smart pointer class.
Definition: generics.h:56