Chinaunix首页 | 论坛 | 博客
  • 博客访问: 563966
  • 博文数量: 104
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1559
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-21 00:58
个人简介

锻炼精神,首先要锻炼肉体

文章分类

全部博文(104)

文章存档

2018年(1)

2016年(1)

2015年(101)

2014年(1)

我的朋友

分类: C/C++

2015-04-28 11:09:52

编程的时候,需要使用到将 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

点击(此处)折叠或打开

  1. #!/bin/sh
  2. autoscan
  3. aclocal
  4. autoconf
  5. autoheader
  6. automake --add-missing
  7. ./configure CXXFLAGS= CFLAGS=
  8. make
2. configure.in

点击(此处)折叠或打开

  1. # -*- Autoconf -*-
  2. # Process this file with autoconf to produce a configure script.
  3. AC_INIT(Bson_Vector_test)
  4. AC_USE_SYSTEM_EXTENSIONS
  5. AM_INIT_AUTOMAKE(Bson_Vector_test, 1.0)
  6. AC_CONFIG_SRCDIR([bson_vector_test.cpp])
  7. AC_CONFIG_HEADERS([config.h])
  8. # Checks for programs.
  9. AC_PROG_CXX
  10. AC_PROG_CC
  11. # Checks for libraries.
  12. # Checks for header files.
  13. # Checks for typedefs, structures, and compiler characteristics.
  14. # Checks for library functions.
  15. AC_OUTPUT(Makefile)
3. Makefile.am

点击(此处)折叠或打开

  1. AUTOMAKE_OPTIONS=foreign
  2. bin_PROGRAMS=Bson_Vector_test
  3. BSON_SRC_PATH=/unixC/Bson/bson/src
  4. BOOST_LIB_PATH=/unixC/Boost/boost_1_58_0
  5. #what source files Bson_Vector_test needs ?
  6. Bson_Vector_test_SOURCES=\
  7. bson_vector_test.cpp \
  8. $(BSON_SRC_PATH)/bsonobj.cpp $(BSON_SRC_PATH)/util/json.cpp \
  9. $(BSON_SRC_PATH)/oid.cpp $(BSON_SRC_PATH)/lib/base64.cpp \
  10. $(BSON_SRC_PATH)/lib/md5.cpp $(BSON_SRC_PATH)/lib/nonce.cpp
  11. Bson_Vector_test_CXXFLAGS=\
  12. -I$(BOOST_LIB_PATH) -I$(BSON_SRC_PATH) -D_FILE_OFFSET_BITS=64 \
  13. -ggdb -Wall -O0
  14. Bson_Vector_test_LDADD=\
  15. -lpthread -lm -lboost_system -lboost_thread \
  16. -lboost_thread -lboost_program_options -lrt
  17. Bson_Vector_test_LDFLAGS=\
  18. -fPIC -rdynamic -L$(BOOST_LIB_PATH)/stage/lib -pthread
4. bson_vector_test.cpp

点击(此处)折叠或打开

  1. // test how to push and extract vector object
  2. // into and from a BSONObj

  3. // first test a int type vector
  4. // second test char type vector
  5. // then test a struct node type vector

  6. //


  7. #include <iostream>
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include <cstring>

  11. #include <bson.h>

  12. using namespace std ;
  13. using namespace bson ;

  14. int main ( int argc , char ** argv )
  15. {
  16.   vector <int> list1, list2 ; // list2 stored content extract from obj1
  17.   BSONObjBuilder builder ;
  18.   BSONObj obj1 ; // push in obj1
  19.   
  20.   // insert contents into list
  21.   for ( int i = 0 ; i < 30 ; i++ )
  22.       list1.push_back (i) ;
  23.   
  24.    builder.append ("Integer_List", list1) ;
  25.   
  26.    obj1 = builder.obj () ;

  27.    cout << "here is the bson obj content "<< endl ;
  28.    cout << obj1.toString () << endl ;

  29.    // extract from obj1
  30.   
  31.    BSONObjIterator iter (obj1["Integer_List"].embeddedObjectUserCheck()) ;
  32.    
  33.    while ( iter.more() )
  34.       list2.push_back (iter.next().number()) ;

  35.    for ( int i = 0 ; i < list2.size () ; i++ )
  36.       cout << list2[i] << endl ;

  37.   
  38.   cout <<"-----------test bson vector------------------------"<< endl ;
  39.   vector<char> charList1, charList2 ;
  40.   BSONObj cObj1 ;
  41.   BSONObjBuilder cBuilder ;
  42.  
  43.   for ( int i = 0 ; i < 100 ; i++ )
  44.   {
  45.     char c = 0x01 ;
  46.     charList1.push_back(c) ;
  47.   }

  48.   for ( int i = 0 ; i < charList1.size() ; i++ )
  49.      cout << charList1[i] << endl ;
  50.  cBuilder.append ("bit_field",charList1) ;
  51.   
  52.  cObj1 = cBuilder.obj () ;
  53.   
  54.  cout << "here is cObj1 " << cObj1.toString () << endl ;
  55.  
  56.  // extract info from BSONObj stroed it into charList2
  57.  
  58.  BSONObjIterator cIter (cObj1["bit_field"].embeddedObjectUserCheck()) ;
  59.  
  60.  while (cIter.more () )
  61.  {
  62.     charList2.push_back ((char)cIter.next().number()) ;
  63.  }

  64.  cout << "output contents in charList2 " << endl ;

  65.  for ( int i = 0 ; i < charList2.size () ; i++ )
  66.    cout << charList2[i] << endl ;


  67.    return 0 ;
  68. }
end
阅读(3046) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~