MongoDB  1.8.5
bson.h
Go to the documentation of this file.
00001 /* NOTE: Standalone bson header for when not using MongoDB.
00002    See also: bsondemo.
00003 
00004    MongoDB includes ../db/jsobj.h instead. This file, however, pulls in much less code / dependencies.
00005 */
00006 
00011 /*
00012  *    Copyright 2009 10gen Inc.
00013  *
00014  *    Licensed under the Apache License, Version 2.0 (the "License");
00015  *    you may not use this file except in compliance with the License.
00016  *    You may obtain a copy of the License at
00017  *
00018  *    http://www.apache.org/licenses/LICENSE-2.0
00019  *
00020  *    Unless required by applicable law or agreed to in writing, software
00021  *    distributed under the License is distributed on an "AS IS" BASIS,
00022  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00023  *    See the License for the specific language governing permissions and
00024  *    limitations under the License.
00025  */
00026 
00036 #pragma once
00037 
00038 #if defined(MONGO_EXPOSE_MACROS)
00039 #error this header is for client programs, not the mongo database itself. include jsobj.h instead.
00040 /* because we define simplistic assert helpers here that don't pull in a bunch of util -- so that
00041    BSON can be used header only.
00042    */
00043 #endif
00044 
00045 #include <iostream>
00046 #include <sstream>
00047 #include <boost/utility.hpp>
00048 #include "util/builder.h"
00049 
00050 namespace bson {
00051 
00052     using std::string;
00053     using std::stringstream;
00054 
00055     class assertion : public std::exception {
00056     public:
00057         assertion( unsigned u , const string& s )
00058             : id( u ) , msg( s ) {
00059             mongo::StringBuilder ss;
00060             ss << "BsonAssertion id: " << u << " " << s;
00061             full = ss.str();
00062         }
00063 
00064         virtual ~assertion() throw() {}
00065 
00066         virtual const char* what() const throw() { return full.c_str(); }
00067 
00068         unsigned id;
00069         string msg;
00070         string full;
00071     };
00072 }
00073 
00074 namespace mongo {
00075 #if !defined(assert)
00076     inline void assert(bool expr) {
00077         if(!expr) {
00078             throw bson::assertion( 0 , "assertion failure in bson library" );
00079         }
00080     }
00081 #endif
00082 #if !defined(uassert)
00083     inline void uasserted(unsigned msgid, std::string s) {
00084         throw bson::assertion( msgid , s );
00085     }
00086 
00087     inline void uassert(unsigned msgid, std::string msg, bool expr) {
00088         if( !expr )
00089             uasserted( msgid , msg );
00090     }
00091     inline void msgasserted(int msgid, const char *msg) {
00092         throw bson::assertion( msgid , msg );
00093     }
00094     inline void msgasserted(int msgid, const std::string &msg) { msgasserted(msgid, msg.c_str()); }
00095     inline void massert(unsigned msgid, std::string msg, bool expr) {
00096         if(!expr) {
00097             std::cout << "assertion failure in bson library: " << msgid << ' ' << msg << std::endl;
00098             throw bson::assertion( msgid , msg );
00099         }
00100     }
00101 #endif
00102 }
00103 
00104 #include "../bson/bsontypes.h"
00105 #include "../bson/oid.h"
00106 #include "../bson/bsonelement.h"
00107 #include "../bson/bsonobj.h"
00108 #include "../bson/bsonmisc.h"
00109 #include "../bson/bsonobjbuilder.h"
00110 #include "../bson/bsonobjiterator.h"
00111 #include "../bson/bson-inl.h"
00112 
00113 namespace mongo {
00114 
00115     inline unsigned getRandomNumber() {
00116 #if defined(_WIN32)
00117         return rand();
00118 #else
00119         return random();
00120 #endif
00121     }
00122 
00123 }