MongoDB  2.7.0
Public Member Functions | Static Public Attributes | Friends | List of all members
mongo::SafeNum Class Reference

SafeNum holds and does arithmetic on a number in a safe way, handling overflow and casting for the user. More...

#include <safe_num.h>

Public Member Functions

 SafeNum (const SafeNum &rhs)
 
SafeNumoperator= (const SafeNum &rhs)
 
 SafeNum (const BSONElement &element)
 
 SafeNum (int num)
 
 SafeNum (long long int num)
 
 SafeNum (double num)
 
bool isEquivalent (const SafeNum &rhs) const
 Returns true if the numeric quantity of 'rhs' and 'this' are the same. More...
 
bool operator== (const SafeNum &rhs) const
 
bool operator!= (const SafeNum &rhs) const
 
bool isIdentical (const SafeNum &rhs) const
 Returns true if 'rsh' is equivalent to 'this' (see isEquivalent) and both types are exactly the same. More...
 
SafeNum operator+ (const SafeNum &rhs) const
 Sums the 'rhs' – right-hand side – safe num with this, taking care of upconversions and overflow (see class header).
 
SafeNumoperator+= (const SafeNum &rhs)
 
SafeNum operator* (const SafeNum &rhs) const
 Multiplies the 'rhs' – right-hand side – safe num with this, taking care of upconversions and overflow (see class header).
 
SafeNumoperator*= (const SafeNum &rhs)
 
SafeNum bitAnd (const SafeNum &rhs) const
 
SafeNum operator& (const SafeNum &rhs) const
 
SafeNumoperator&= (const SafeNum &rhs)
 
SafeNum bitOr (const SafeNum &rhs) const
 
SafeNum operator| (const SafeNum &rhs) const
 
SafeNumoperator|= (const SafeNum &rhs)
 
SafeNum bitXor (const SafeNum &rhs) const
 
SafeNum operator^ (const SafeNum &rhs) const
 
SafeNumoperator^= (const SafeNum &rhs)
 
bool isValid () const
 
BSONType type () const
 
std::string debugString () const
 

Static Public Attributes

static const long long maxIntInDouble = 9007199254740992LL
 

Friends

class mutablebson::Element
 
class mutablebson::Document
 

Detailed Description

SafeNum holds and does arithmetic on a number in a safe way, handling overflow and casting for the user.

32-bit integers will overflow into 64-bit integers. But 64-bit integers will NOT overflow to doubles. Also, this class does NOT downcast. This class should be as conservative as possible about upcasting, but should never lose precision.

This class does not throw any exceptions, so the user should call type() before using a SafeNum to ensure that it is valid. A SafeNum could be invalid from creation (if, for example, a non-numeric BSONElement was passed to the constructor) or due to overflow. NAN is a valid value.

Usage example:

 SafeNum counter(doc["count"]);

 SafeNum newValue = counter + 10;
 // check if valid
 if (newValue.type() == EOO) {
     return;
 }
 // append SafeNum to a BSONObj
 bsonObjBuilder.append(newValue);

Member Function Documentation

bool mongo::SafeNum::isEquivalent ( const SafeNum rhs) const

Returns true if the numeric quantity of 'rhs' and 'this' are the same.

That is, an int32(10), an int64(10), and a double(10) are equivalent. An EOO-typed safe num is equivalent only to another EOO-typed instance. Otherwise, returns false.

bool mongo::SafeNum::isIdentical ( const SafeNum rhs) const

Returns true if 'rsh' is equivalent to 'this' (see isEquivalent) and both types are exactly the same.

An EOO-typed safe num is never identical to anything else, even another EOO-typed instance. Otherwise, returns false.


The documentation for this class was generated from the following files: