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})
#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})
相关文章推荐
- Eclipse中集成Maven
- 这是一个水贴
- sql语句查重
- Android Studio开发JNI工程
- emgu CV全景图像拼接
- net面试 ASP.NET页面传值的各种方法和分析 (copy)
- nginx+php-fpm json_encode 到client pages 截断
- Eclipse修改右键点击新建的内容
- 重载操作符
- leetcode之Gas Station
- Spring 注解学习手札(六) 测试
- 关于css布局中margin,padding在整个布局中使用的一些细节
- win10系统没有关机键怎么办?win10系统没有关机键的解决方法
- Velocity 自定义库加载优化
- android使用zxing扫描界面全是白色以及更改显示文字屏幕居中
- 系统定制成iso文件
- Linux基础之变量设定规则
- socket.io分布式
- Android5.1 VectorDrawable
- ReactiveCocoa 学习心得 -- 2