@@ -46,8 +46,12 @@ class MongoDB_API ObjectId
4646public:
4747 typedef SharedPtr<ObjectId> Ptr;
4848
49- ObjectId (const std::string& id = " " );
50- // / Constructor
49+ ObjectId (const std::string& id);
50+ // / Constructor. The string must contain a hexidecimal representation
51+ // / of an object id. This means a string of 24 characters.
52+
53+ ObjectId (const ObjectId& copy);
54+ // / Copy constructor.
5155
5256 virtual ~ObjectId ();
5357 // / Destructor
@@ -61,10 +65,19 @@ class MongoDB_API ObjectId
6165 // / of the ID char array.
6266
6367private:
68+
69+ ObjectId ();
70+ // / Constructor. Creates an empty Id
71+
6472 unsigned char _id[12 ];
6573
6674 friend class BSONWriter ;
6775 friend class BSONReader ;
76+ friend class Document ;
77+
78+ static int fromHex (char c);
79+
80+ static char fromHex (const char * c);
6881};
6982
7083
@@ -79,6 +92,21 @@ inline Timestamp ObjectId::timestamp() const
7992 return Timestamp::fromEpochTime ((time_t ) time);
8093}
8194
95+ inline int ObjectId::fromHex (char c)
96+ {
97+ if ( ' 0' <= c && c <= ' 9' )
98+ return c - ' 0' ;
99+ if ( ' a' <= c && c <= ' f' )
100+ return c - ' a' + 10 ;
101+ if ( ' A' <= c && c <= ' F' )
102+ return c - ' A' + 10 ;
103+ return 0xff ;
104+ }
105+
106+ inline char ObjectId::fromHex (const char * c)
107+ {
108+ return (char )((fromHex (c[0 ]) << 4 ) | fromHex (c[1 ]));
109+ }
82110
83111// BSON Embedded Document
84112// spec: ObjectId
@@ -89,7 +117,7 @@ struct ElementTraits<ObjectId::Ptr>
89117
90118 static std::string toString (const ObjectId::Ptr& id,
91119 int indent = 0 ,
92- const std::string& fmt = " %x " )
120+ const std::string& fmt = " %02x " )
93121 {
94122 return id->toString (fmt);
95123 }
0 commit comments