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 
147 class __EXPORT ZNumber : public Number
148 {
149 public:
155  ZNumber(char *pointer, unsigned size);
156 
162  void set(long value);
163 
169  long operator=(long value);
170 };
171 
175 typedef Number number_t;
176 
181 
187 template<typename T>
188 inline const T abs(const T& value)
189 {
190  if(value < (T)0)
191  return -value;
192  return value;
193 }
194 
195 
202 template<typename T>
203 inline const T (min)(const T& v1, const T& v2)
204 {
205  return ((v1 < v2) ? v1 : v2);
206 }
207 
214 template<typename T>
215 inline const T (max)(const T& v1, const T& v2)
216 {
217  return ((v1 > v2) ? v1 : v2);
218 }
219 
220 END_NAMESPACE
221 
222 #endif