blob: b210c8c57a75a4642edad3eef82fac4a27ecbed4 [file] [log] [blame]
Alexandre Lisionddd731e2014-01-31 11:50:08 -05001// 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
18/**
19 * Support classes for manipulation of numbers as strings. This is
20 * used for things which parse numbers out of strings, such as in the
21 * date and time classes. Other useful math related functions, templates,
22 * and macros may also be found here.
23 * @file ucommon/numbers.h
24 */
25
26#ifndef _UCOMMON_NUMBERS_H_
27#define _UCOMMON_NUMBERS_H_
28
29#ifndef _UCOMMON_CONFIG_H_
30#include <ucommon/platform.h>
31#endif
32
33NAMESPACE_UCOMMON
34
35/**
36 * A number manipulation class. This is used to extract, convert,
37 * and manage simple numbers that are represented in C ascii strings
38 * in a very quick and optimal way. This class modifies the string
39 * representation each time the value is changed. No math expressions
40 * or explicit comparison operators are supported for the Numbers class
41 * because these are best done by casting to long first.
42 *
43 * @author David Sugar <dyfet@ostel.com>
44 * @short number manipulation.
45 */
46class __EXPORT Number
47{
48protected:
49 char *buffer;
50 unsigned size;
51
52public:
53 /**
54 * Create an instance of a number.
55 * @param buffer or NULL if created internally.
56 * @param size of field if not null terminated.
57 */
58 Number(char *buffer, unsigned size);
59
60 /**
61 * Set string based on a new value.
62 * @param value to set.
63 */
64 void set(long value);
65
66 /**
67 * Get string buffer representing the number.
68 * @return string buffer.
69 */
70 inline const char *c_str() const
71 {return buffer;};
72
73 /**
74 * Get value of string buffer as a long integer.
75 * @return long integer value of string buffer.
76 */
77 long get() const;
78
79 /**
80 * Get value of string buffer as expression of object.
81 * @return long integer value of string buffer.
82 */
83 inline long operator()()
84 {return get();};
85
86 /**
87 * Cast string as long integer and get value of buffer.
88 * @return long integer value of string buffer.
89 */
90 inline operator long()
91 {return get();};
92
93 /**
94 * Cast object as a string to retrieve buffer.
95 * @return string buffer of value.
96 */
97 inline operator char*()
98 {return buffer;};
99
100 /**
101 * Assign a value to the number. This rewrites the string buffer.
102 * @param value to assign.
103 * @return new value of number object assigned.
104 */
105 long operator=(long value);
106
107 /**
108 * Assign another number to this number.
109 * @param number to assign to assign.
110 * @return new value of number object assigned.
111 */
112 long operator=(const Number& number);
113
114 /**
115 * Add a value to the number. This rewrites the string buffer.
116 * @param value to add.
117 * @return new value of number object.
118 */
119 long operator+=(const long value);
120
121 /**
122 * Subtract a value from the number. This rewrites the string buffer.
123 * @param value to subtract.
124 * @return new value of number object.
125 */
126 long operator-=(const long value);
127
128 /**
129 * Decrement the number object. This rewrites the string buffer.
130 * @return new value of number object.
131 */
132 long operator--();
133
134 /**
135 * Increment the number object. This rewrites the string buffer.
136 * @return new value of number object.
137 */
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
159/**
160 * A number manipulation class that maintains a zero lead filled string.
161 *
162 * @author David Sugar <dyfet@ostel.com>
163 * @short zero filled number manipulation.
164 */
165class __EXPORT ZNumber : public Number
166{
167public:
168 /**
169 * Create a number class for zero fill.
170 * @param pointer to field.
171 * @param size of field to fill.
172 */
173 ZNumber(char *pointer, unsigned size);
174
175 /**
176 * Set value of zero filled number.
177 * @param value to set.
178 */
179
180 void set(long value);
181
182 /**
183 * Assign number from value.
184 * @param value to assign.
185 * @return value assigned.
186 */
187 long operator=(long value);
188};
189
190/**
191 * A convenience type for number.
192 */
193typedef Number number_t;
194
195/**
196 * A convenience type for znumber.
197 */
198typedef ZNumber znumber_t;
199
200/**
201 * Template for absolute value of a type.
202 * @param value to check
203 * @return absolute value
204 */
205template<typename T>
206inline const T abs(const T& value)
207{
208 if(value < (T)0)
209 return -value;
210 return value;
211}
212
213
214/**
215 * Template for min value of a type.
216 * @param v1 value to check
217 * @param v2 value to check
218 * @return v1 if < v2, else v2
219 */
220template<typename T>
221inline const T (min)(const T& v1, const T& v2)
222{
223 return ((v1 < v2) ? v1 : v2);
224}
225
226/**
227 * Template for max value of a type.
228 * @param v1 value to check
229 * @param v2 value to check
230 * @return v1 if > v2, else v2
231 */
232template<typename T>
233inline const T (max)(const T& v1, const T& v2)
234{
235 return ((v1 > v2) ? v1 : v2);
236}
237
238END_NAMESPACE
239
240#endif