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
document.h
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include "element.h"
10 #include "typeinfo"
11 #include <initializer_list>
12 #include <map>
13 #include <set>
14 #include <string>
15 #include <sstream>
16 #include <ostream>
17 
18 namespace bson
19 {
20 
21  class Document
22  {
23  private:
24  friend class Element;
25  std::map<std::string, Element> m_data;
26  std::set<std::string> m_field_names;
27 
28  public:
29  Document() = default;
35  Document(std::initializer_list<std::pair<std::string, Element>> list){for (auto i: list) {m_data.emplace(i.first, i.second); m_field_names.emplace(i.first);}}
43  const Element& operator[] (const std::string & index) const {return m_data.at(index);}
44  Element& operator[] (const std::string & index) {return m_data.at(index);}
45 
51  void add (const std::string name, const Element& data) {m_data.emplace(name, data); m_field_names.insert(name);}
58  template <class T>
59  void add (const std::string name, const T& data, const TypeInfo ti = _UNKNOWN) {m_data[name] = Element(data, ti); m_field_names.insert(name);}
60 
67  void set(const std::string name, const Element& data) {m_data.at(name) = data;}
76  template <class T>
77  void set(const std::string & name, const T& data, const TypeInfo ti = _UNKNOWN){m_data.at(name) = Element(data, ti);}
78 
84  friend std::ostream& operator << (std::ostream& o, const Document& d);
85 
92  auto begin() -> decltype(m_data.begin()) {return m_data.begin();}
93  auto end() -> decltype(m_data.end()) {return m_data.end();}
94  auto rbegin() -> decltype(m_data.rbegin()) {return m_data.rbegin();}
95  auto rend() -> decltype(m_data.rend()) {return m_data.rend();}
96  auto cbegin() -> decltype(m_data.cbegin()) {return m_data.cbegin();}
97  auto cend() -> decltype(m_data.cend()) {return m_data.cbegin();}
98  auto crbegin() -> decltype(m_data.crbegin()) {return m_data.crbegin();}
99  auto crend() -> decltype(m_data.crend()) {return m_data.crend();}
100 
107  std::set<std::string> field_names() const {return m_field_names;}
108  };
109 
110  inline std::ostream& operator << (std::ostream& oss, const Document& d)
111  {
112  bool first = true;
113  oss << "{ ";
114  for (std::pair<std::string, Element> p: d.m_data)
115  {
116  if (!first)
117  {
118  oss << ", ";
119  }
120  first = false;
121  oss << p.first << " : " << static_cast<std::string>(p.second);
122  }
123  oss << " }";
124  return oss;
125  }
126 }
Definition: element.h:30
Declaration of a BSON element.
Document(std::initializer_list< std::pair< std::string, Element >> list)
Initialization list constructor.
Definition: document.h:35
void add(const std::string name, const Element &data)
element addition
Definition: document.h:51
void add(const std::string name, const T &data, const TypeInfo ti=_UNKNOWN)
element addition
Definition: document.h:59
void set(const std::string &name, const T &data, const TypeInfo ti=_UNKNOWN)
element modification
Definition: document.h:77
const Element & operator[](const std::string &index) const
accessor
Definition: document.h:43
void set(const std::string name, const Element &data)
element modification
Definition: document.h:67
Definition: document.h:21
friend std::ostream & operator<<(std::ostream &o, const Document &d)
insertion operator overloading
Definition: document.h:110
auto begin() -> decltype(m_data.begin())
iterator functions
Definition: document.h:92
std::set< std::string > field_names() const
returns the set of field names
Definition: document.h:107