您的位置:首页 > 其它

Boost any库的笔记

2016-03-10 16:42 92 查看
cpp源文件清单
#include <boost/any.hpp>
#include <boost/smart_ptr.hpp>

#include <map>
#include <vector>
#include <string>
#include <iostream>
using namespace std;

/*
Title:boost::any的测试
Author: kagula
Date: 2018-2-9
Prologue:
boost any是万能数据类型,使用它仅包含头文件就可以了,不需要库文件链接。
Test Environment:
[1]Windows7, Visual Studio 2017 Update5
[2]CentOS7, GCC 5.5
[3]CMake
[4]boost 1.59
Conclude
[1]建议boost::any里的对象类型是boost::shared_ptr
否则通过boost::any_cast修改对象可能没有效果。
*/

void test1()
{
cout << "测试boost::any变量容纳不同数据类型的对象" << endl;

boost::any a;

std::vector<boost::shared_ptr<std::string>> vecMy;

a = vecMy;

if (a.type() == typeid(std::vector<boost::shared_ptr<std::string>>))
{
cout << "is std::vector<boost::shared_ptr<std::string>>!" << endl;
}
else
{
assert(NULL);
}

boost::shared_ptr<std::string> pA(new std::string("hello,World!"));

a = pA;

if (a.type() == typeid(boost::shared_ptr<std::string>))
{
cout << "boost::shared_ptr<std::string>!" << endl;
cout << *boost::any_cast<boost::shared_ptr<std::string>>(a) << endl;
}
else
{
assert(NULL);
}
}

void test_pushback()
{
cout << "测试修改boost::any_cast返回的对象!" << endl;

std::vector<boost::shared_ptr<std::string>> idMapBuf;

boost::shared_ptr<std::string> newBuffer(new std::string("Hello,world!"));

idMapBuf.push_back(newBuffer);//只能这样才能把数据压进去。
{
//下面这行代码数据压不进去,哪道any_cast为idMapBuf生成了新的临时对象?
//boost::any_cast<std::vector<boost::shared_ptr<std::string>>>(idMapBuf).push_back(newBuffer);
}

//检查数据有没有压进去
int tempSize = boost::any_cast<std::vector<boost::shared_ptr<std::string>>>(idMapBuf).size();
cout << tempSize << endl;
}

void test_pushback2()
{
cout << "相对于test_pushback,idMapBuf多了一层boost::shared_ptr封装。" << endl;
boost::shared_ptr<std::vector<boost::shared_ptr<std::string>>> idMapBuf(new std::vector<boost::shared_ptr<std::string>>());

boost::shared_ptr<std::string> newBuffer(new std::string("Hello,world!"));

{
//现在数据能压进去了!
boost::any_cast<boost::shared_ptr<std::vector<boost::shared_ptr<std::string>>>>(idMapBuf)->push_back(newBuffer);
}

//检查数据有没有压进去
int tempSize = boost::any_cast<boost::shared_ptr<std::vector<boost::shared_ptr<std::string>>>>(idMapBuf)->size();
cout << tempSize << endl;

boost::shared_ptr<std::vector<boost::shared_ptr<std::string>>> tempIdMapBuf =
boost::any_cast<boost::shared_ptr<std::vector<boost::shared_ptr<std::string>>>>(idMapBuf);

std::string temp = *tempIdMapBuf->at(0);
cout << temp << endl;
}

int main(int argc, char* argv[])
{
map<string, boost::any> mapAny;

//测试数据放进去
string strValue = "std::string";

mapAny["string"] = "string";
mapAny["std:string"] = strValue;
mapAny["std:string2"] = string("std:string2");
mapAny["int"] = 123;
mapAny["long"] = 234l;
mapAny["float"] = .345f;
mapAny["double"] = .456;

//测试数据取出来
//如果cast了不正确的数据类型会throw exception!
map<string, boost::any>::iterator iter = mapAny.begin();
while (iter != mapAny.end())
{
cout << iter->first << ">>>";
if (iter->second.type() == typeid(int))
{
cout << " == typeid(int) " << boost::any_cast<int>(iter->second);
}
else if (iter->second.type() == typeid(string))
{
cout << " == typeid(string) " << boost::any_cast<string>(iter->second);
}
else if (iter->second.type() == typeid(const char*))
{
cout << " == typeid(const char*) " << boost::any_cast<const char *>(iter->second);
}
else
{
cout << "unknown ";
}

cout << endl;
iter++;
}

//测试boost::any_cast抛出异常
try
{
//注意原始数据类型any_cast异常可能是不能capture的。
//null值异常是不能capture的。
cout << boost::any_cast<int>(mapAny["std:string2"]) << endl;
}
catch (boost::bad_any_cast& e)
{
cout << "capture exception is " << e.what() << endl;
}

test1();
test_pushback();
test_pushback2();

cin.get();

return 0;
}

CMakeLists.txt清单
#设置项目名称
project(testBoostAny)

#要求CMake的最低版本为2.8
cmake_minimum_required(VERSION 2.8)

include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)

if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} doesn’t support C++11. Please upgrade or use a different C++ compiler.")
endif()

#For Boost library
add_definitions(-DBOOST_ALL_NO_LIB)

set(Boost_USE_STATIC_LIBS OFF) # using dynamic files
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)

#boost的system子库,如果有多个子库,子库名称之间用空格分隔
find_package(Boost 1.59 COMPONENTS system REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})

#用于将当前目录下的所有源文件的名字保存在变量 DIR_SRCS 中
aux_source_directory(. DIR_SRCS)

#用于指定从一组源文件 source1 source2 … sourceN(在变量DIR_SRCS中定义)
#编译出一个可执行文件且命名为sceneInfoServer
add_executable(testBoostAny ${DIR_SRCS})

#We need some third party libraries to run our program!
target_link_libraries(testBoostAny ${Boost_LIBRARIES})
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: