MongoDB  2.7.0
safe_num-inl.h
1 /* Copyright 2013 10gen Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 namespace mongo {
19 
20  inline SafeNum::SafeNum() : _type(EOO) {
21  }
22 
23  inline SafeNum::~SafeNum() {
24  }
25 
26  inline SafeNum::SafeNum(const SafeNum& rhs) : _type(rhs._type), _value(rhs._value) {
27  }
28 
29  inline SafeNum& SafeNum::operator=(const SafeNum& rhs) {
30  _type = rhs._type;
31  _value = rhs._value;
32  return *this;
33  }
34 
35  inline SafeNum::SafeNum(int num) : _type(NumberInt) {
36  _value.int32Val = num;
37  }
38 
39  inline SafeNum::SafeNum(long long int num) : _type(NumberLong) {
40  _value.int64Val = num;
41  }
42 
43  inline SafeNum::SafeNum(double num) : _type(NumberDouble) {
44  _value.doubleVal = num;
45  }
46 
47  inline bool SafeNum::operator==(const SafeNum& rhs) const {
48  return isEquivalent(rhs);
49  }
50 
51  inline bool SafeNum::operator!=(const SafeNum& rhs) const {
52  return ! isEquivalent(rhs);
53  }
54 
55  inline SafeNum SafeNum::operator+(const SafeNum& rhs) const {
56  return addInternal(*this, rhs);
57  }
58 
59  inline SafeNum& SafeNum::operator+=(const SafeNum& rhs) {
60  return *this = addInternal(*this, rhs);
61  }
62 
63  inline SafeNum SafeNum::operator*(const SafeNum& rhs) const {
64  return mulInternal(*this, rhs);
65  }
66 
67  inline SafeNum& SafeNum::operator*=(const SafeNum& rhs) {
68  return *this = mulInternal(*this, rhs);
69  }
70 
71  inline SafeNum SafeNum::bitAnd(const SafeNum& rhs) const {
72  return andInternal(*this, rhs);
73  }
74 
75  inline SafeNum SafeNum::operator&(const SafeNum& rhs) const {
76  return bitAnd(rhs);
77  }
78 
79  inline SafeNum& SafeNum::operator&=(const SafeNum& rhs) {
80  return *this = bitAnd(rhs);
81  }
82 
83  inline SafeNum SafeNum::bitOr(const SafeNum& rhs) const {
84  return orInternal(*this, rhs);
85  }
86 
87  inline SafeNum SafeNum::operator|(const SafeNum& rhs) const {
88  return bitOr(rhs);
89  }
90 
91  inline SafeNum& SafeNum::operator|=(const SafeNum& rhs) {
92  return *this = bitOr(rhs);
93  }
94 
95  inline SafeNum SafeNum::bitXor(const SafeNum& rhs) const {
96  return xorInternal(*this, rhs);
97  }
98 
99  inline SafeNum SafeNum::operator^(const SafeNum& rhs) const {
100  return bitXor(rhs);
101  }
102 
103  inline SafeNum& SafeNum::operator^=(const SafeNum& rhs) {
104  return *this = bitXor(rhs);
105  }
106 
107  inline bool SafeNum::isValid() const {
108  return _type != EOO;
109  }
110 
111  inline BSONType SafeNum::type() const {
112  return _type;
113  }
114 
115 } // namespace mongo
end of object
Definition: bsontypes.h:47
SafeNum operator*(const SafeNum &rhs) const
Multiplies the 'rhs' – right-hand side – safe num with this, taking care of upconversions and overflo...
Definition: safe_num-inl.h:63
BSONType
the complete list of valid BSON types see also bsonspec.org
Definition: bsontypes.h:43
32 bit signed integer
Definition: bsontypes.h:79
double precision floating point value
Definition: bsontypes.h:49
SafeNum holds and does arithmetic on a number in a safe way, handling overflow and casting for the us...
Definition: safe_num.h:56
64 bit integer
Definition: bsontypes.h:83
bool isEquivalent(const SafeNum &rhs) const
Returns true if the numeric quantity of 'rhs' and 'this' are the same.
Definition: safe_num.cpp:75
SafeNum operator+(const SafeNum &rhs) const
Sums the 'rhs' – right-hand side – safe num with this, taking care of upconversions and overflow (see...
Definition: safe_num-inl.h:55