Commit | Line | Data |
---|---|---|
970ed795 | 1 | /////////////////////////////////////////////////////////////////////////////// |
3abe9331 | 2 | // Copyright (c) 2000-2015 Ericsson Telecom AB |
970ed795 EL |
3 | // All rights reserved. This program and the accompanying materials |
4 | // are made available under the terms of the Eclipse Public License v1.0 | |
5 | // which accompanies this distribution, and is available at | |
6 | // http://www.eclipse.org/legal/epl-v10.html | |
7 | /////////////////////////////////////////////////////////////////////////////// | |
8 | #ifndef Int_HH_ | |
9 | #define Int_HH_ | |
10 | ||
11 | #include "Real.hh" | |
12 | #include "string.hh" | |
13 | ||
14 | #include <openssl/bn.h> | |
15 | ||
16 | #include <limits.h> | |
17 | ||
18 | // Better together. | |
19 | #if !defined(LLONG_MAX) || !defined(LLONG_MIN) | |
20 | #define LLONG_MAX 9223372036854775807LL | |
21 | #define LLONG_MIN (-LLONG_MAX-1) | |
22 | #else | |
23 | #if __GNUC__ < 3 | |
24 | #undef LLONG_MIN | |
25 | #define LLONG_MIN ((long long)(-LLONG_MAX-1)) | |
26 | #endif | |
27 | #endif | |
28 | ||
29 | namespace Common { | |
30 | ||
31 | class Location; | |
32 | ||
33 | typedef long long Int; | |
34 | typedef int RInt; | |
35 | ||
36 | // Converts the Common::Int value to string. | |
37 | string Int2string(const Int& i); | |
38 | ||
39 | // Converts the string value to Common::Int. Throws Error_Int if conversion | |
40 | // is not possible (e.g. value is out of range, or s is not a valid integer | |
41 | // representation). | |
42 | Int string2Int(const char *s, const Location& loc); | |
43 | inline Int string2Int(const string& s, const Location& loc) | |
44 | { return string2Int(s.c_str(), loc); } | |
45 | ||
46 | class int_val_t | |
47 | { | |
48 | private: | |
49 | bool native_flag; | |
50 | union { | |
51 | Int native; | |
52 | BIGNUM *openssl; | |
53 | } val; | |
54 | // Hide all BIGNUM related stuff here. | |
55 | BIGNUM *to_openssl() const; | |
56 | BIGNUM *get_val_openssl() const; | |
57 | explicit int_val_t(BIGNUM *v) : native_flag(false), val() { val.openssl = v; } | |
58 | ||
59 | public: | |
60 | int_val_t(); | |
61 | int_val_t(const int_val_t& v); | |
62 | int_val_t(const char *s, const Location& loc); | |
63 | explicit int_val_t(Int v) : native_flag(true), val() { val.native = v; } | |
64 | ~int_val_t(); | |
65 | string t_str() const; | |
66 | int_val_t operator-() const; | |
67 | int_val_t operator>>(Int right) const; | |
68 | int_val_t operator+(const int_val_t& right) const; | |
69 | int_val_t operator-(const int_val_t& right) const; | |
70 | inline int_val_t operator+(Int right) const { return operator+(int_val_t(right)); } | |
71 | inline int_val_t operator-(Int right) const { return operator-(int_val_t(right)); } | |
72 | int_val_t operator*(const int_val_t& right) const; | |
73 | int_val_t operator/(const int_val_t& right) const; | |
74 | int_val_t operator&(Int right) const; | |
75 | bool operator<(const int_val_t& right) const; | |
76 | inline bool operator<(Int right) const { return *this < int_val_t(right); } | |
77 | inline bool operator>(Int right) const { return *this > int_val_t(right); } | |
78 | inline bool operator>(const int_val_t& right) const { return *this != right && !(*this < right); } | |
79 | inline bool operator<=(Int right) const { return *this == right || *this < right; } | |
80 | inline bool operator>=(Int right) const { return *this == right || *this > right; } | |
81 | inline bool operator<=(const int_val_t& right) const { return *this == right || *this < right; } | |
82 | inline bool operator>=(const int_val_t& right) const { return *this == right || *this > right; } | |
83 | inline bool operator!=(Int right) const { return !(*this == right); } | |
84 | inline bool operator!=(const int_val_t& right) const { return !(*this == right); } | |
85 | bool operator==(const int_val_t& right) const; | |
86 | bool operator==(Int right) const; | |
87 | int_val_t& operator=(const int_val_t& right); | |
88 | int_val_t& operator+=(Int right); | |
89 | int_val_t& operator<<=(Int right); | |
90 | int_val_t& operator>>=(Int right); | |
91 | const Int& get_val() const; | |
92 | Real to_real() const; | |
93 | inline bool is_native_fit() const | |
94 | // It seems to give correct results (-2147483648..2147483647). | |
95 | { return native_flag && val.native == static_cast<RInt>(val.native); } | |
96 | inline bool is_native() const { return native_flag; } | |
97 | inline bool is_openssl() const { return !native_flag; } | |
98 | inline bool is_negative() const | |
99 | { return native_flag ? val.native < 0 : BN_is_negative(val.openssl); } | |
100 | }; | |
101 | ||
102 | } // Common | |
103 | ||
104 | #endif // Int_HH_ |