Numerics.hpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.ibm.com/developerworks/oss/CPLv1.0.htm */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /******************************************************************************/
39 /* REXX Kernel */
40 /* */
41 /* Utility class to manage the various sorts of numeric conversions required */
42 /* by Rexx. These conversions are all just static methods. */
43 /* */
44 /******************************************************************************/
45 #ifndef Included_Numerics
46 #define Included_Numerics
47 
48 
49 class NumericSettings // "global" numeric settings */
50 {
51  public:
53  size_t digits; /* numeric digits setting */
54  size_t fuzz; /* numeric fuzz setting */
55  bool form; /* numeric form setting */
56 }; /* global activation settings */
57 
58 
59 class Numerics
60 {
61 public:
66  static const size_t DEFAULT_DIGITS;
67  // a digits setting for full range integer conversion
68  static const size_t ARGUMENT_DIGITS;
69  // the digits setting used internally for function/method arguments to allow
70  // for the full range
71  static const size_t SIZE_DIGITS;
72  static const size_t MAX_STRINGSIZE;
73 
74  // max numeric digits value for explicit 64-bit conversions
75  static const size_t DIGITS64;
76  static const bool FORM_SCIENTIFIC;
77  static const bool FORM_ENGINEERING;
78 
79  static const size_t DEFAULT_FUZZ;
80  /* default numeric form setting */
81  static const bool DEFAULT_FORM;
82 
83  static const wholenumber_t validMaxWhole[]; // table of maximum values per digits setting
84 
87  static RexxObject *int64ToObject(int64_t v);
91 
93  static bool objectToStringSize(RexxObject *o, stringsize_t &result, stringsize_t max);
94  static bool objectToSignedInteger(RexxObject *o, ssize_t &result, ssize_t max, ssize_t min);
95  static bool objectToUnsignedInteger(RexxObject *o, size_t &result, size_t max);
96  static bool objectToInt64(RexxObject *o, int64_t &result);
97  static bool objectToUnsignedInt64(RexxObject *o, uint64_t &result);
98  static bool objectToUintptr(RexxObject *source, uintptr_t &result);
99  static bool objectToIntptr(RexxObject *source, intptr_t &result);
100  static RexxObject *int64Object(RexxObject *source);
101 
102  static size_t formatWholeNumber(wholenumber_t integer, char *dest);
103  static size_t formatStringSize(stringsize_t integer, char *dest);
104  static size_t formatInt64(int64_t integer, char *dest);
105  static size_t formatUnsignedInt64(uint64_t integer, char *dest);
106 
107  static size_t normalizeWholeNumber(wholenumber_t integer, char *dest);
108 
109  static size_t digits() { return settings->digits; }
110  static size_t fuzz() { return settings->fuzz; }
111  static bool form() { return settings->form; }
112  static void setCurrentSettings(NumericSettings *s) { settings = s; }
115  static inline wholenumber_t abs(wholenumber_t n) { return n < 0 ? -n : n; }
116  static inline wholenumber_t minVal(wholenumber_t n1, wholenumber_t n2) { return n2 > n1 ? n1 : n2; }
117  static inline stringsize_t minVal(stringsize_t n1, stringsize_t n2) { return n2 > n1 ? n1 : n2; }
118  static inline wholenumber_t maxVal(wholenumber_t n1, wholenumber_t n2) { return n2 > n1 ? n2 : n1; }
119  static inline stringsize_t maxVal(stringsize_t n1, stringsize_t n2) { return n2 > n1 ? n2 : n1; }
120  static inline wholenumber_t maxValueForDigits(size_t d)
121  {
122  if (d > ARGUMENT_DIGITS)
123  {
124  return validMaxWhole[ARGUMENT_DIGITS - 1];
125  }
126  else
127  {
128  return validMaxWhole[d - 1];
129  }
130  }
131 
132  static inline wholenumber_t multiplierForExponent(size_t e)
133  {
134  return validMaxWhole[e - 1];
135  }
136 
137  static RexxString *pointerToString(void *);
138 
139 
140 protected:
141 
144 };
145 
146 
147 inline size_t number_digits() { return Numerics::digits(); }
148 inline size_t number_fuzz() { return Numerics::fuzz(); }
149 inline bool number_form() { return Numerics::form(); }
150 inline size_t number_fuzzydigits() { return number_digits() - number_fuzz(); }
151 #endif
152 
#define min(a, b)
Definition: ArrayClass.cpp:82
bool number_form()
Definition: Numerics.hpp:149
size_t number_digits()
Definition: Numerics.hpp:147
size_t number_fuzzydigits()
Definition: Numerics.hpp:150
size_t number_fuzz()
Definition: Numerics.hpp:148
ssize_t integer(RexxObject *obj, const char *errorMessage)
Definition: TextClass.cpp:289
static const wholenumber_t MIN_WHOLENUMBER
Definition: Numerics.hpp:63
static wholenumber_t multiplierForExponent(size_t e)
Definition: Numerics.hpp:132
static bool objectToUintptr(RexxObject *source, uintptr_t &result)
Definition: Numerics.cpp:526
static NumericSettings * setDefaultSettings()
Definition: Numerics.hpp:113
static NumericSettings * getDefaultSettings()
Definition: Numerics.hpp:114
static RexxObject * stringsizeToObject(stringsize_t v)
Definition: Numerics.cpp:203
static const wholenumber_t MIN_EXPONENT
Definition: Numerics.hpp:65
static size_t digits()
Definition: Numerics.hpp:109
static stringsize_t minVal(stringsize_t n1, stringsize_t n2)
Definition: Numerics.hpp:117
static size_t formatStringSize(stringsize_t integer, char *dest)
Definition: Numerics.cpp:698
static const size_t DIGITS64
Definition: Numerics.hpp:75
static stringsize_t maxVal(stringsize_t n1, stringsize_t n2)
Definition: Numerics.hpp:119
static const wholenumber_t MAX_WHOLENUMBER
Definition: Numerics.hpp:62
static wholenumber_t abs(wholenumber_t n)
Definition: Numerics.hpp:115
static bool form()
Definition: Numerics.hpp:111
static const wholenumber_t validMaxWhole[]
Definition: Numerics.hpp:83
static RexxObject * uintptrToObject(uintptr_t v)
Definition: Numerics.cpp:844
static RexxObject * int64Object(RexxObject *source)
Definition: Numerics.cpp:447
static const bool DEFAULT_FORM
Definition: Numerics.hpp:81
static const wholenumber_t MAX_EXPONENT
Definition: Numerics.hpp:64
static wholenumber_t maxVal(wholenumber_t n1, wholenumber_t n2)
Definition: Numerics.hpp:118
static NumericSettings * settings
Definition: Numerics.hpp:142
static size_t formatUnsignedInt64(uint64_t integer, char *dest)
Definition: Numerics.cpp:804
static bool objectToUnsignedInt64(RexxObject *o, uint64_t &result)
Definition: Numerics.cpp:483
static size_t fuzz()
Definition: Numerics.hpp:110
static size_t formatInt64(int64_t integer, char *dest)
Definition: Numerics.cpp:739
static wholenumber_t maxValueForDigits(size_t d)
Definition: Numerics.hpp:120
static const size_t MAX_STRINGSIZE
Definition: Numerics.hpp:72
static bool objectToUnsignedInteger(RexxObject *o, size_t &result, size_t max)
Definition: Numerics.cpp:361
static bool objectToWholeNumber(RexxObject *o, wholenumber_t &result, wholenumber_t max, wholenumber_t min)
Definition: Numerics.cpp:226
static wholenumber_t minVal(wholenumber_t n1, wholenumber_t n2)
Definition: Numerics.hpp:116
static RexxObject * int64ToObject(int64_t v)
Definition: Numerics.cpp:140
static const bool FORM_SCIENTIFIC
Definition: Numerics.hpp:76
static const size_t DEFAULT_FUZZ
Definition: Numerics.hpp:79
static NumericSettings defaultSettings
Definition: Numerics.hpp:143
static const bool FORM_ENGINEERING
Definition: Numerics.hpp:77
static size_t normalizeWholeNumber(wholenumber_t integer, char *dest)
Definition: Numerics.cpp:637
static bool objectToIntptr(RexxObject *source, intptr_t &result)
Definition: Numerics.cpp:549
static RexxObject * wholenumberToObject(wholenumber_t v)
Definition: Numerics.cpp:182
static bool objectToStringSize(RexxObject *o, stringsize_t &result, stringsize_t max)
Definition: Numerics.cpp:312
static bool objectToSignedInteger(RexxObject *o, ssize_t &result, ssize_t max, ssize_t min)
Definition: Numerics.cpp:269
static RexxString * pointerToString(void *)
Definition: Numerics.cpp:888
static RexxObject * intptrToObject(intptr_t v)
Definition: Numerics.cpp:866
static const size_t SIZE_DIGITS
Definition: Numerics.hpp:71
static void setCurrentSettings(NumericSettings *s)
Definition: Numerics.hpp:112
static const size_t ARGUMENT_DIGITS
Definition: Numerics.hpp:68
static size_t formatWholeNumber(wholenumber_t integer, char *dest)
Definition: Numerics.cpp:572
static bool objectToInt64(RexxObject *o, int64_t &result)
Definition: Numerics.cpp:409
static RexxObject * uint64ToObject(uint64_t v)
Definition: Numerics.cpp:161
static const size_t DEFAULT_DIGITS
Definition: Numerics.hpp:66
ssize_t wholenumber_t
Definition: rexx.h:230
size_t stringsize_t
Definition: rexx.h:228
UINT_PTR uintptr_t
signed __int64 int64_t
SSIZE_T ssize_t
INT_PTR intptr_t
unsigned __int64 uint64_t