FastBSON
A BSON library that will hopefully be faster and more robust than the one built into mongo-cxx-driver
 All Classes Files Functions Friends
element.h
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include "typeinfo.h"
10 #include <sstream>
11 #include <memory>
12 #include <vector>
13 
14 namespace bson
15 {
16  const char X00 = '\0'; // null character, useful for encoding in bson
17  const int OID_SIZE = 12;
18  const int DB_PTR_SIZE = 12;
19 
20  class Element; //forward declaration for typedefs
21  class Document; // forward declaration for friending and typedefs
22 
23  typedef std::vector<Element> array;
24  typedef std::array<unsigned char, OID_SIZE> oid;
25  typedef std::pair<std::string, std::string> regex;
26  typedef std::pair<std::string, std::array<unsigned char, DB_PTR_SIZE>> dbptr;
27  typedef std::pair<std::string, Document> jscode_scope;
28  typedef std::pair<unsigned char, std::string> binary;
29 
30  class Element
31  {
32  public:
38  Element(): m_data(nullptr), m_type(_UNKNOWN) {}
39 
46  template <typename T>
47  Element(const T& data, const TypeInfo type = _UNKNOWN);
48 
49  Element(const char* data, const TypeInfo type = _UNKNOWN);
50 
56  Element(const unsigned char* data, const TypeInfo & type) {decode(data, type);}
57 
64  unsigned decode(const unsigned char* data, const TypeInfo m_type);
65 
71  void encode(std::ostringstream& oss) const;
72 
79  TypeInfo get_type() const {return m_type;}
87  template <typename T>
88  const T& data() const;
95  template <typename T>
96  void data(T& t) const {t = data<T>();}
97 
103  static void encode(std::ostringstream& oss, const Element& e) {e.encode(oss);}
104  static void decode(const unsigned char* data, const TypeInfo type, Element& e) {e.decode(data, type);}
105 
111  operator std::string() const;
112 
113  private:
114  friend class Document;
115 
116  std::shared_ptr<void> m_data;
117  TypeInfo m_type;
118 
119 /* ------------ ALL FUNCTIONS BELOW THIS LINE MUST BE SPECIALIZED FOR TYPES --------- */
120 
127  template <typename T>
128  bool check_convert() const;
129 
136  template <typename T>
137  void type_check() const;
138 
145  template<typename T>
146  unsigned deserialize_bytes(const unsigned char* bytes);
147 
153  template<typename T>
154  void serialize_bson(std::ostringstream& oss) const;
155 
162  template <typename T>
163  std::string _to_std_str() const;
164  };
165 
172  template <typename T>
173  std::string to_string();
174 
181  template <typename T>
182  TypeInfo default_type();
183 
184 }
185 
186 #include "template_spec/document.hpp"
187 #include "template_spec/floats.hpp"
188 #include "template_spec/strings.hpp"
189 #include "template_spec/ints.hpp"
190 #include "template_spec/bools.hpp"
191 #include "template_spec/vectors.hpp"
192 #include "template_spec/chararrs.hpp"
193 #include "template_spec/voids.hpp"
194 #include "template_spec/pairs.hpp"
195 #include "template_spec/jsscope.hpp"
196 #include "template_spec/binary.hpp"
197 #include "element.hpp"
198 
Element(const unsigned char *data, const TypeInfo &type)
decoding constructor
Definition: element.h:56
Definition: element.h:30
void encode(std::ostringstream &oss) const
encodes this object ias a bytestring
Definition: element.cpp:74
Element()
Default Constructor.
Definition: element.h:38
static void encode(std::ostringstream &oss, const Element &e)
static encoders and decoders
Definition: element.h:103
Enumeration to keep track of types for bson.
Implementation of the BSON element class.
unsigned decode(const unsigned char *data, const TypeInfo m_type)
decodes a byte string into the calling object
Definition: element.cpp:22
const T & data() const
data accessor
Definition: element.hpp:26
void data(T &t) const
data accessor
Definition: element.h:96
TypeInfo get_type() const
type accessor
Definition: element.h:79