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
typeinfo.h
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include <exception>
10 #include <string>
11 #include <boost/concept_check.hpp>
12 
13 namespace bson
14 {
15  const std::string NAMES[] = {"unknown", "floating point number", "string", "document", "array",
16  "binary data", "undefined", "object ID", "boolean", "datetime",
17  "null value", "regular expression", "database pointer",
18  "javascript code", "depreicated", "scoped javascript", "int 32",
19  "timestamp", "int64"};
20  enum TypeInfo {_UNKNOWN=0, FLOATING=1, STRING, DOCUMENT, ARRAY, BINARY, UNDEF, OID, BOOL, DATETIME,
21  NIL, REGEX, DBPTR, JS, DEPRECATED, JS_SCOPE, INT32, TIMESTAMP, INT64,
22  MINKEY=0xFF, MAXKEY=0x7F
23  };
24 
25  inline char to_char(const TypeInfo & ti)
26  {
27  return static_cast<char>((int)ti);
28  }
29 
30  //forward declaration
31  template <class T>
32  std::string to_string();
33 
34  inline std::string to_string(const TypeInfo & ti)
35  {
36  return (ti == 0xFF?"max key": (ti == 0x7F?"min key": NAMES[ti]));
37  }
38 
39  template <class T>
40  class type_error: public std::exception
41  {
42  public:
43  type_error(const TypeInfo & ti): m_ti(ti) {}
44  virtual const char* what() const noexcept
45  {
46  std::string err = "Invalid conversion from C++ type: " + to_string<T>() + " to BSON type: " + to_string(m_ti);
47  return err.c_str();
48  }
49  private:
50  TypeInfo m_ti;
51  };
52 
53  class type_UNKNOWN: public std::exception
54  {
55  public:
56  virtual const char* what() const noexcept
57  {
58  return "_UNKNOWN type not implemented";
59  }
60  };
61 
62 
63 
64 }
Definition: typeinfo.h:40
Definition: typeinfo.h:53