| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> | | ||
| 4 | | All rights reserved. | | ||
| 5 | | | | ||
| 6 | | Redistribution and use in source and binary forms, with or without | | ||
| 7 | | modification, are permitted provided that the following conditions are met:| | ||
| 8 | | | | ||
| 9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
| 10 | | this list of conditions and the following disclaimer. | | ||
| 11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
| 12 | | notice, this list of conditions and the following disclaimer in the | | ||
| 13 | | documentation and/or other materials provided with the distribution. | | ||
| 14 | | | | ||
| 15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
| 16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
| 17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
| 18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
| 19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
| 20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
| 21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
| 22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
| 23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
| 24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
| 25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
| 26 | +----------------------------------------------------------------------------+ | ||
| 27 | */ | ||
| 28 | |||
| 29 | /** @file | ||
| 30 | * @brief Number encapsulation and numeric attribute combines | ||
| 31 | * @ingroup gatecore_cpp | ||
| 32 | */ | ||
| 33 | |||
| 34 | #ifndef GATE_NUMBERS_HPP_INCLUDED | ||
| 35 | #define GATE_NUMBERS_HPP_INCLUDED | ||
| 36 | #include "gate/gate_core_api.hpp" | ||
| 37 | #include "gate/strings.hpp" | ||
| 38 | |||
| 39 | namespace gate | ||
| 40 | { | ||
| 41 | |||
| 42 | /// @brief | ||
| 43 | /// @tparam T | ||
| 44 | /// @tparam ATTRIB | ||
| 45 | template<class T, class ATTRIB = void> | ||
| 46 | class Number | ||
| 47 | { | ||
| 48 | public: | ||
| 49 | typedef Number<T, ATTRIB> self_t; | ||
| 50 | typedef T value_t; | ||
| 51 | typedef ATTRIB attrib_t; | ||
| 52 | |||
| 53 | private: | ||
| 54 | value_t impl; | ||
| 55 | |||
| 56 | public: | ||
| 57 | |||
| 58 | 2 | Number() | |
| 59 | 2 | : impl(value_t()) | |
| 60 | { | ||
| 61 | 2 | } | |
| 62 | |||
| 63 | 1 | Number(self_t const& src) | |
| 64 | 1 | : impl(src.impl) | |
| 65 | { | ||
| 66 | 1 | } | |
| 67 | |||
| 68 | 58 | explicit Number(value_t const& value) | |
| 69 | 58 | : impl(value) | |
| 70 | { | ||
| 71 | 58 | } | |
| 72 | |||
| 73 | 3 | self_t& operator=(self_t const& src) | |
| 74 | { | ||
| 75 | 3 | this->impl = src.impl; | |
| 76 | 3 | return *this; | |
| 77 | } | ||
| 78 | 1 | self_t& operator=(value_t const& src) | |
| 79 | { | ||
| 80 | 1 | this->impl = src; | |
| 81 | 1 | return *this; | |
| 82 | } | ||
| 83 | 62 | ~Number() noexcept | |
| 84 | { | ||
| 85 | 62 | } | |
| 86 | |||
| 87 | 43 | value_t const& value() const | |
| 88 | { | ||
| 89 | 43 | return this->impl; | |
| 90 | } | ||
| 91 | |||
| 92 | 20 | value_t& value() | |
| 93 | { | ||
| 94 | 20 | return this->impl; | |
| 95 | } | ||
| 96 | |||
| 97 | value_t const& operator*() const | ||
| 98 | { | ||
| 99 | return this->value(); | ||
| 100 | } | ||
| 101 | |||
| 102 | value_t& operator*() | ||
| 103 | { | ||
| 104 | return this->value(); | ||
| 105 | } | ||
| 106 | |||
| 107 | 1 | self_t& operator++() | |
| 108 | { | ||
| 109 | 1 | ++this->impl; | |
| 110 | 1 | return *this; | |
| 111 | } | ||
| 112 | self_t operator++(int) | ||
| 113 | { | ||
| 114 | self_t prev(this->impl++); | ||
| 115 | return prev; | ||
| 116 | } | ||
| 117 | |||
| 118 | self_t& operator--() | ||
| 119 | { | ||
| 120 | --this->impl; | ||
| 121 | return *this; | ||
| 122 | } | ||
| 123 | self_t operator--(int) | ||
| 124 | { | ||
| 125 | self_t prev(this->impl--); | ||
| 126 | return prev; | ||
| 127 | } | ||
| 128 | |||
| 129 | ✗ | self_t& operator+=(self_t const& that) | |
| 130 | { | ||
| 131 | ✗ | this->impl += that.impl; | |
| 132 | ✗ | return *this; | |
| 133 | } | ||
| 134 | 1 | self_t& operator+=(value_t const& value) | |
| 135 | { | ||
| 136 | 1 | this->impl += value; | |
| 137 | 1 | return *this; | |
| 138 | } | ||
| 139 | self_t operator+(self_t const& that) const | ||
| 140 | { | ||
| 141 | return self_t(this->impl + that.impl); | ||
| 142 | } | ||
| 143 | 1 | self_t operator+(value_t const& value) const | |
| 144 | { | ||
| 145 | 1 | return self_t(this->impl + value); | |
| 146 | } | ||
| 147 | |||
| 148 | self_t& operator-=(self_t const& that) | ||
| 149 | { | ||
| 150 | this->impl -= that.impl; | ||
| 151 | return *this; | ||
| 152 | } | ||
| 153 | 1 | self_t& operator-=(value_t const& value) | |
| 154 | { | ||
| 155 | 1 | this->impl -= value; | |
| 156 | 1 | return *this; | |
| 157 | } | ||
| 158 | self_t operator-(self_t const& that) const | ||
| 159 | { | ||
| 160 | return self_t(this->impl - that.impl); | ||
| 161 | } | ||
| 162 | self_t operator-(value_t const& value) const | ||
| 163 | { | ||
| 164 | return self_t(this->impl - value); | ||
| 165 | } | ||
| 166 | |||
| 167 | self_t& operator*=(self_t const& that) | ||
| 168 | { | ||
| 169 | this->impl *= that.impl; | ||
| 170 | return *this; | ||
| 171 | } | ||
| 172 | 1 | self_t& operator*=(value_t const& value) | |
| 173 | { | ||
| 174 | 1 | this->impl *= value; | |
| 175 | 1 | return *this; | |
| 176 | } | ||
| 177 | self_t operator*(self_t const& that) const | ||
| 178 | { | ||
| 179 | return self_t(this->impl * that.impl); | ||
| 180 | } | ||
| 181 | self_t operator*(value_t const& value) const | ||
| 182 | { | ||
| 183 | return self_t(this->impl * value); | ||
| 184 | } | ||
| 185 | |||
| 186 | self_t& operator/=(self_t const& that) | ||
| 187 | { | ||
| 188 | this->impl /= that.impl; | ||
| 189 | return *this; | ||
| 190 | } | ||
| 191 | 1 | self_t& operator/=(value_t const& value) | |
| 192 | { | ||
| 193 | 1 | this->impl /= value; | |
| 194 | 1 | return *this; | |
| 195 | } | ||
| 196 | self_t operator/(self_t const& that) const | ||
| 197 | { | ||
| 198 | return self_t(this->impl / that.impl); | ||
| 199 | } | ||
| 200 | self_t operator/(value_t const& value) const | ||
| 201 | { | ||
| 202 | return self_t(this->impl / value); | ||
| 203 | } | ||
| 204 | |||
| 205 | self_t& operator%=(self_t const& that) | ||
| 206 | { | ||
| 207 | this->impl %= that.impl; | ||
| 208 | return *this; | ||
| 209 | } | ||
| 210 | self_t& operator%=(value_t const& value) | ||
| 211 | { | ||
| 212 | this->impl /= value; | ||
| 213 | return *this; | ||
| 214 | } | ||
| 215 | self_t operator%(self_t const& that) const | ||
| 216 | { | ||
| 217 | return self_t(this->impl % that.impl); | ||
| 218 | } | ||
| 219 | self_t operator%(value_t const& value) const | ||
| 220 | { | ||
| 221 | return self_t(this->impl % value); | ||
| 222 | } | ||
| 223 | }; | ||
| 224 | |||
| 225 | namespace numbers | ||
| 226 | { | ||
| 227 | //template<class T> String to_string(T const& n); | ||
| 228 | |||
| 229 | GATE_CORE_CPP_API String to_string(int8_t const& n); | ||
| 230 | GATE_CORE_CPP_API String to_string(int16_t const& n); | ||
| 231 | GATE_CORE_CPP_API String to_string(int32_t const& n); | ||
| 232 | GATE_CORE_CPP_API String to_string(int64_t const& n); | ||
| 233 | GATE_CORE_CPP_API String to_string(uint8_t const& n); | ||
| 234 | GATE_CORE_CPP_API String to_string(uint16_t const& n); | ||
| 235 | GATE_CORE_CPP_API String to_string(uint32_t const& n); | ||
| 236 | GATE_CORE_CPP_API String to_string(uint64_t const& n); | ||
| 237 | GATE_CORE_CPP_API String to_string(real32_t const& n, unsigned intlen = 0, unsigned decimallen = 0, unsigned grouplen = 0); | ||
| 238 | GATE_CORE_CPP_API String to_string(real64_t const& n, unsigned intlen = 0, unsigned decimallen = 0, unsigned grouplen = 0); | ||
| 239 | } | ||
| 240 | |||
| 241 | /// @brief | ||
| 242 | /// @tparam T | ||
| 243 | /// @tparam ATTRIB | ||
| 244 | template<class T, class ATTRIB = void> | ||
| 245 | class BuiltInNumber : public Number<T, ATTRIB> | ||
| 246 | { | ||
| 247 | public: | ||
| 248 | typedef T value_t; | ||
| 249 | typedef Number<T, ATTRIB> base_t; | ||
| 250 | typedef BuiltInNumber<T, ATTRIB> self_t; | ||
| 251 | |||
| 252 | 20 | BuiltInNumber(value_t const& value = 0) | |
| 253 | 20 | : base_t(value) | |
| 254 | { | ||
| 255 | 20 | } | |
| 256 | BuiltInNumber(base_t const& src) | ||
| 257 | : base_t(src) | ||
| 258 | { | ||
| 259 | } | ||
| 260 | BuiltInNumber(self_t const& src) | ||
| 261 | : base_t(src) | ||
| 262 | { | ||
| 263 | } | ||
| 264 | self_t& operator=(base_t const& src) | ||
| 265 | { | ||
| 266 | base_t::operator=(src); | ||
| 267 | return *this; | ||
| 268 | } | ||
| 269 | self_t& operator=(self_t const& src) | ||
| 270 | { | ||
| 271 | base_t::operator=(src); | ||
| 272 | return *this; | ||
| 273 | } | ||
| 274 | |||
| 275 | 9 | String toString() const | |
| 276 | { | ||
| 277 | 9 | return numbers::to_string(this->value()); | |
| 278 | } | ||
| 279 | }; | ||
| 280 | |||
| 281 | typedef BuiltInNumber<int8_t> Int8; | ||
| 282 | typedef BuiltInNumber<int16_t> Int16; | ||
| 283 | typedef BuiltInNumber<int32_t> Int32; | ||
| 284 | typedef BuiltInNumber<int64_t> Int64; | ||
| 285 | typedef BuiltInNumber<uint8_t> UInt8; | ||
| 286 | typedef BuiltInNumber<uint16_t> UInt16; | ||
| 287 | typedef BuiltInNumber<uint32_t> UInt32; | ||
| 288 | typedef BuiltInNumber<uint64_t> UInt64; | ||
| 289 | typedef BuiltInNumber<real32_t> Real32; | ||
| 290 | typedef BuiltInNumber<real64_t> Real64; | ||
| 291 | |||
| 292 | } // end of namespace gate | ||
| 293 | |||
| 294 | #endif /* GATE_NUMBERS_HPP_INCLUDED */ | ||
| 295 |