编程的时候,需要使用到将 bson::BSONObj 对象 与 std::vector 进行相互转换,所以写了一个代码进行测试。
在将 bson::BSONObj ---> std::vector 的时候只需要借助于 bson::BSONObjBuilder
作为中间转化媒介即可。
在将 bson::BSONObj ---> std::vector 的时候,需要借助于bson::BSONObjIterator 即可。
对于复杂的结构体类型,
如:
vectore
struct node
{
int i ;
char c ;
} ;
暂时还没找到解决方法
编程环境: linux
编译工具: g++ , autotools {autoscan , aclocal , autoconf , autoheader , make}
autotools 的使用方法参见文档 [c++,bson]bson 的安装与使用
示例代码如下:
1. build.sh
-
#!/bin/sh
-
autoscan
-
aclocal
-
autoconf
-
autoheader
-
automake --add-missing
-
./configure CXXFLAGS= CFLAGS=
-
make
2. configure.in
-
# -*- Autoconf -*-
-
# Process this file with autoconf to produce a configure script.
-
-
AC_INIT(Bson_Vector_test)
-
-
AC_USE_SYSTEM_EXTENSIONS
-
-
AM_INIT_AUTOMAKE(Bson_Vector_test, 1.0)
-
-
AC_CONFIG_SRCDIR([bson_vector_test.cpp])
-
-
AC_CONFIG_HEADERS([config.h])
-
-
# Checks for programs.
-
AC_PROG_CXX
-
-
AC_PROG_CC
-
-
# Checks for libraries.
-
-
# Checks for header files.
-
-
# Checks for typedefs, structures, and compiler characteristics.
-
-
# Checks for library functions.
-
-
AC_OUTPUT(Makefile)
3. Makefile.am
-
AUTOMAKE_OPTIONS=foreign
-
bin_PROGRAMS=Bson_Vector_test
-
BSON_SRC_PATH=/unixC/Bson/bson/src
-
BOOST_LIB_PATH=/unixC/Boost/boost_1_58_0
-
-
#what source files Bson_Vector_test needs ?
-
Bson_Vector_test_SOURCES=\
-
bson_vector_test.cpp \
-
$(BSON_SRC_PATH)/bsonobj.cpp $(BSON_SRC_PATH)/util/json.cpp \
-
$(BSON_SRC_PATH)/oid.cpp $(BSON_SRC_PATH)/lib/base64.cpp \
-
$(BSON_SRC_PATH)/lib/md5.cpp $(BSON_SRC_PATH)/lib/nonce.cpp
-
-
-
Bson_Vector_test_CXXFLAGS=\
-
-I$(BOOST_LIB_PATH) -I$(BSON_SRC_PATH) -D_FILE_OFFSET_BITS=64 \
-
-ggdb -Wall -O0
-
-
-
Bson_Vector_test_LDADD=\
-
-lpthread -lm -lboost_system -lboost_thread \
-
-lboost_thread -lboost_program_options -lrt
-
-
Bson_Vector_test_LDFLAGS=\
-
-fPIC -rdynamic -L$(BOOST_LIB_PATH)/stage/lib -pthread
4. bson_vector_test.cpp
-
// test how to push and extract vector object
-
// into and from a BSONObj
-
-
// first test a int type vector
-
// second test char type vector
-
// then test a struct node type vector
-
-
//
-
-
-
#include <iostream>
-
#include <cstdio>
-
#include <cstdlib>
-
#include <cstring>
-
-
#include <bson.h>
-
-
using namespace std ;
-
using namespace bson ;
-
-
int main ( int argc , char ** argv )
-
{
-
vector <int> list1, list2 ; // list2 stored content extract from obj1
-
BSONObjBuilder builder ;
-
BSONObj obj1 ; // push in obj1
-
-
// insert contents into list
-
for ( int i = 0 ; i < 30 ; i++ )
-
list1.push_back (i) ;
-
-
builder.append ("Integer_List", list1) ;
-
-
obj1 = builder.obj () ;
-
-
cout << "here is the bson obj content "<< endl ;
-
cout << obj1.toString () << endl ;
-
-
// extract from obj1
-
-
BSONObjIterator iter (obj1["Integer_List"].embeddedObjectUserCheck()) ;
-
-
while ( iter.more() )
-
list2.push_back (iter.next().number()) ;
-
-
for ( int i = 0 ; i < list2.size () ; i++ )
-
cout << list2[i] << endl ;
-
-
-
cout <<"-----------test bson vector------------------------"<< endl ;
-
vector<char> charList1, charList2 ;
-
BSONObj cObj1 ;
-
BSONObjBuilder cBuilder ;
-
-
for ( int i = 0 ; i < 100 ; i++ )
-
{
-
char c = 0x01 ;
-
charList1.push_back(c) ;
-
}
-
-
for ( int i = 0 ; i < charList1.size() ; i++ )
-
cout << charList1[i] << endl ;
-
cBuilder.append ("bit_field",charList1) ;
-
-
cObj1 = cBuilder.obj () ;
-
-
cout << "here is cObj1 " << cObj1.toString () << endl ;
-
-
// extract info from BSONObj stroed it into charList2
-
-
BSONObjIterator cIter (cObj1["bit_field"].embeddedObjectUserCheck()) ;
-
-
while (cIter.more () )
-
{
-
charList2.push_back ((char)cIter.next().number()) ;
-
}
-
-
cout << "output contents in charList2 " << endl ;
-
-
for ( int i = 0 ; i < charList2.size () ; i++ )
-
cout << charList2[i] << endl ;
-
-
-
return 0 ;
-
}
end
阅读(3167) | 评论(0) | 转发(0) |