#ifndef __MY_BOOST_TEST_H__
#define __MY_BOOST_TEST_H__
//#include "CCString.h"
using namespace std;
#include <iostream>
//关键字 try catch的使用
//try
//{
// throw 1;
// throw "error";
//}
//catch(char *str)
//{
// cout << str << endl;
//}
//catch(int i)
//{
// cout << i << endl;
//}
//1.理解 boost::any
#include <boost/any.hpp>
//boost::any是一种通用的数据类型,可以将各种类型包装后统一放入容器内,最重要的它是类型安全的。有点象COM里面的variant。
//
//使用方法:
//any::type() 返回包装的类型
//any_cast可用于any到其他类型的转化
void test_any()
{
typedef std::vector<boost::any> many;
many a;
a.push_back(2);
a.push_back(string("test"));
for (unsigned int i = 0; i < a.size(); ++i)
{
cout<<a[i].type().name()<<endl;//输出类型
try
{
int result = any_cast<int>(a[i]);
cout<<result<<endl;
}
catch (boost::bad_any_cast &ex)
{
cout<<"cast error"<<ex.what()<<endl;
}
}
}
//2. 理解 boost::array
//boost::array仅仅是对数组一层薄薄的封装,提供跟各种算法配合的iterator,使用方法很简单。
//注意:可以使用{}来初始化array,因为array所有的成员变量都是public的。
#include <boost/array.hpp>
void test_array()
{
array<int, 10> ai = { 1, 2, 3, 4 };
for (size_t i = 0; i < ai.size(); ++i)
{
cout << ai[i] << endl;
}
}
//3. 理解 boost::lexical_cast 词典的_计算
//lexical_cast用于将字符串转换成各种数字类型(int,float,short etc.)。
#include <boost/lexical_cast_hpp>
void test_lexical_cast()
{
int i = boost::lexical_cast<int>("123");//
cout << i << endl;
}
//4.理解 boost::format
//boost::format是用于替代c里面的sprintf,优点是类型安全,不会因为类型和参数不匹配而导致程序崩溃了,
//而且还可以重复使用参数。
//%1%表示第一个参数
#include<boost/format.hpp>
void test_format()
{
cout <<
boost::format("writing %1%, x=%2%:%3%-thtry")
% "toto"
% 40.23
% 50
<< endl;//输出 writing toto, x=40.23:50-thtry
format f("a=%1%,b=%2%,c=%3%,a=%1%");
f % "string" % 2 % 10.0;
cout << f.str() << endl;//输出 a=string,b=2,c=10.0,a=string
}
//5. 理解 boost::tokenizer 分割器
#include <boost/tokenizer.hpp>
void test_tokenizer()
{
string s("this is , a, test!");
boost::tokenizer<> tok(s);
for (boost::tokenizer<>::iterator beg = tok.begin(); beg != tok.end(); ++beg)
{
cout << *beg << "\n";
//输出结果:
//this
//is
//a
//test
}
}
//6.理解 boost::thread
//boost::thread是为了提供跨平台的thread机制。利用boost::function来完成委托。
#include <boost/thread.hpp>
void mythread()
{
cout << "hello,thread" << endl;
}
void hello()
{
cout << "Hello world, I'm a thread!" << endl;
}
void test_thread()
{
boost::function<void()> f(mythread());
boost::thread t(f);
t.join();
cout << "thread is over" << endl;
boost::thread dugaodathread(&hello);
dugaodathread.join();
}
//7.理解 boost::serialization 序列化
//boost::serialization提供object的序列化功能。而且提供好几种序列化的格式,比如text,binary,xml。
//序列化:将对象变成字节流的形式传出去。
//反序列化:从字节流恢复成原来的对象。
//archive 把......存档的意思
//说明
//序列化操作使用 << 或者 & 操作符将数据存入text_oarchive中:
//反序列化操作使用 >> 或者 & 操作符从text_iarchive中读取数据:
#include <boost/archive/text_oarchive.hpp> // 序列化数据,也称为:输出、保存(save)前缀是text说明是对text文本操作
#include <boost/archive/text_iarchive.hpp> // 反序列化数据,也称为:输入、载入(load)
#include <boost/archive/xml_oarchive.hpp>
//将字符串保存到文本归档文件中
void save()
{
std::ofstream file("archive.txt");
boost::archive::text_oarchive oa(file);
std::string s = "hello world dugaoda!";
oa << s
}
//将字符串的内容加载到文本归档文件中
void load()
{
std::ofstream file("archive.txt");
boost::archive::text_iarchive ia(file);
std::string s;
ia >> s;
std::cout << s << std::endl;//之前执行了save(),在执行时候会输出"hello world dugaoda!"
//我们来看一下转储的文本文件:
//22 serialization::archive 9 13 hello world dugaoda!
}
void test_serialization()
{
boost::archive::text_oarchive to(cout, boost::archive::no_header);
int i = 10;
string s = "This is a test";
to & i;//等同于效果 to << i
to & s;
ofstream f("test.xml");
boost::archive::xml_oarchive xo(f);
xo & BOOST_SERIALIZATION_NVP(i) & BOOST_SERIALIZATION_NVP(s);
boost::archive::text_iarchive ti(cin, boost::archive::no_header);
ti & i & s;
cout << "i=" << i << endl;
cout << "s=" << s << endl;
}
//8.理解 boost::function
//boost::function就是所谓的泛函数,能够对普通函数指针,成员函数指针,functor进行委托,达到迟调用的效果
#include<boost/function.hpp>
int foo(int x, int y)
{
cout << "(foo invoking)x= " << x << " y= " << y << endl;
return x + y;
}
void dugaodafoo(int x, int y)
{
cout << "dugaodafoo" << endl;
}
struct test
{
int foo(int x, int y)
{
cout << "(test::foo invoking)x=" << x << "y=" << y << endl;
return x + y;
}
};
void test_function()
{
boost::function<int/* 函数返回的参数 */ (int, int) /* 函数传进去的参数*/> f;
f = foo;
cout << "f(2,3)=" << f(2, 3) << endl;
boost::function<void (int x, int y)> dgdf;
dgdf = &dugaodafoo;
dgdf(1, 2);
test x;//声明结构体对象
boost::function<int (test *, int, int)> f2;//申明对象 传递的参数多一个 test指针
f2 = &test::foo;//f2指向test::foo的地址
cout << "f2(5,3)=" << f2(&x, 5, 3) << endl;
}
//9.理解 boost:shared_ptr
//boost::shared_ptr就是智能指针的实现,不象std::auto_ptr,它是可以stl的容器一起使用的,非常的方便。
#include<boost/shared_ptr.hpp>
class Shared
{
public:
Shared()
{
cout << "ctor() called" << endl;
}
~Shared()
{
cout << "dtor() called" << endl;
}
Shared & operator = (const Shared &other)
{
cout << "operator = called" << endl;
}
};
void test_shared_ptr()
{
typedef boost::shared_ptr<Shared> SharedSP;
typedef vector<SharedSP> VShared;
VShared v;
v.push_back(SharedSP(new Shared()));
v.push_back(SharedSP(new Shared()));
boost::shared_ptr<Shared> a1(new Shared());
printf("a1 reference count: %d\n", a1.use_count());
boost::shared_ptr<Shared> a2 = a1;
printf("a1 reference count: %d\n", a1.use_count());
printf("a2 reference count: %d\n", a2.use_count());
a1.reset();
printf("a2 reference count: %d\n", a2.use_count());
//输出结果:
//a1 reference count: 1
//a1 reference count: 2
//a2 reference count: 2
//a2 reference count: 1
}
//10.boost::asio 译文
//BOOST_ASSERT 断言 在assert上面多了一层封装
其他
//memset函数的原型是:void *memset(void *s,int c,size_t n)
//总的作用:将已开辟内存空间 s 的首 n 个字节的值设为值 c。
memcpy
//void *memcpy(void *dest, const void *src, size_t n);
//从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中
//htonl就是把本机字节顺序转化为网络字节顺序
//htonl()返回一个网络字节顺序的值。
#endif // __CCSCHEDULER_H__
#define __MY_BOOST_TEST_H__
//#include "CCString.h"
using namespace std;
#include <iostream>
//关键字 try catch的使用
//try
//{
// throw 1;
// throw "error";
//}
//catch(char *str)
//{
// cout << str << endl;
//}
//catch(int i)
//{
// cout << i << endl;
//}
//1.理解 boost::any
#include <boost/any.hpp>
//boost::any是一种通用的数据类型,可以将各种类型包装后统一放入容器内,最重要的它是类型安全的。有点象COM里面的variant。
//
//使用方法:
//any::type() 返回包装的类型
//any_cast可用于any到其他类型的转化
void test_any()
{
typedef std::vector<boost::any> many;
many a;
a.push_back(2);
a.push_back(string("test"));
for (unsigned int i = 0; i < a.size(); ++i)
{
cout<<a[i].type().name()<<endl;//输出类型
try
{
int result = any_cast<int>(a[i]);
cout<<result<<endl;
}
catch (boost::bad_any_cast &ex)
{
cout<<"cast error"<<ex.what()<<endl;
}
}
}
//2. 理解 boost::array
//boost::array仅仅是对数组一层薄薄的封装,提供跟各种算法配合的iterator,使用方法很简单。
//注意:可以使用{}来初始化array,因为array所有的成员变量都是public的。
#include <boost/array.hpp>
void test_array()
{
array<int, 10> ai = { 1, 2, 3, 4 };
for (size_t i = 0; i < ai.size(); ++i)
{
cout << ai[i] << endl;
}
}
//3. 理解 boost::lexical_cast 词典的_计算
//lexical_cast用于将字符串转换成各种数字类型(int,float,short etc.)。
#include <boost/lexical_cast_hpp>
void test_lexical_cast()
{
int i = boost::lexical_cast<int>("123");//
cout << i << endl;
}
//4.理解 boost::format
//boost::format是用于替代c里面的sprintf,优点是类型安全,不会因为类型和参数不匹配而导致程序崩溃了,
//而且还可以重复使用参数。
//%1%表示第一个参数
#include<boost/format.hpp>
void test_format()
{
cout <<
boost::format("writing %1%, x=%2%:%3%-thtry")
% "toto"
% 40.23
% 50
<< endl;//输出 writing toto, x=40.23:50-thtry
format f("a=%1%,b=%2%,c=%3%,a=%1%");
f % "string" % 2 % 10.0;
cout << f.str() << endl;//输出 a=string,b=2,c=10.0,a=string
}
//5. 理解 boost::tokenizer 分割器
#include <boost/tokenizer.hpp>
void test_tokenizer()
{
string s("this is , a, test!");
boost::tokenizer<> tok(s);
for (boost::tokenizer<>::iterator beg = tok.begin(); beg != tok.end(); ++beg)
{
cout << *beg << "\n";
//输出结果:
//this
//is
//a
//test
}
}
//6.理解 boost::thread
//boost::thread是为了提供跨平台的thread机制。利用boost::function来完成委托。
#include <boost/thread.hpp>
void mythread()
{
cout << "hello,thread" << endl;
}
void hello()
{
cout << "Hello world, I'm a thread!" << endl;
}
void test_thread()
{
boost::function<void()> f(mythread());
boost::thread t(f);
t.join();
cout << "thread is over" << endl;
boost::thread dugaodathread(&hello);
dugaodathread.join();
}
//7.理解 boost::serialization 序列化
//boost::serialization提供object的序列化功能。而且提供好几种序列化的格式,比如text,binary,xml。
//序列化:将对象变成字节流的形式传出去。
//反序列化:从字节流恢复成原来的对象。
//archive 把......存档的意思
//说明
//序列化操作使用 << 或者 & 操作符将数据存入text_oarchive中:
//反序列化操作使用 >> 或者 & 操作符从text_iarchive中读取数据:
#include <boost/archive/text_oarchive.hpp> // 序列化数据,也称为:输出、保存(save)前缀是text说明是对text文本操作
#include <boost/archive/text_iarchive.hpp> // 反序列化数据,也称为:输入、载入(load)
#include <boost/archive/xml_oarchive.hpp>
//将字符串保存到文本归档文件中
void save()
{
std::ofstream file("archive.txt");
boost::archive::text_oarchive oa(file);
std::string s = "hello world dugaoda!";
oa << s
}
//将字符串的内容加载到文本归档文件中
void load()
{
std::ofstream file("archive.txt");
boost::archive::text_iarchive ia(file);
std::string s;
ia >> s;
std::cout << s << std::endl;//之前执行了save(),在执行时候会输出"hello world dugaoda!"
//我们来看一下转储的文本文件:
//22 serialization::archive 9 13 hello world dugaoda!
}
void test_serialization()
{
boost::archive::text_oarchive to(cout, boost::archive::no_header);
int i = 10;
string s = "This is a test";
to & i;//等同于效果 to << i
to & s;
ofstream f("test.xml");
boost::archive::xml_oarchive xo(f);
xo & BOOST_SERIALIZATION_NVP(i) & BOOST_SERIALIZATION_NVP(s);
boost::archive::text_iarchive ti(cin, boost::archive::no_header);
ti & i & s;
cout << "i=" << i << endl;
cout << "s=" << s << endl;
}
//8.理解 boost::function
//boost::function就是所谓的泛函数,能够对普通函数指针,成员函数指针,functor进行委托,达到迟调用的效果
#include<boost/function.hpp>
int foo(int x, int y)
{
cout << "(foo invoking)x= " << x << " y= " << y << endl;
return x + y;
}
void dugaodafoo(int x, int y)
{
cout << "dugaodafoo" << endl;
}
struct test
{
int foo(int x, int y)
{
cout << "(test::foo invoking)x=" << x << "y=" << y << endl;
return x + y;
}
};
void test_function()
{
boost::function<int/* 函数返回的参数 */ (int, int) /* 函数传进去的参数*/> f;
f = foo;
cout << "f(2,3)=" << f(2, 3) << endl;
boost::function<void (int x, int y)> dgdf;
dgdf = &dugaodafoo;
dgdf(1, 2);
test x;//声明结构体对象
boost::function<int (test *, int, int)> f2;//申明对象 传递的参数多一个 test指针
f2 = &test::foo;//f2指向test::foo的地址
cout << "f2(5,3)=" << f2(&x, 5, 3) << endl;
}
//9.理解 boost:shared_ptr
//boost::shared_ptr就是智能指针的实现,不象std::auto_ptr,它是可以stl的容器一起使用的,非常的方便。
#include<boost/shared_ptr.hpp>
class Shared
{
public:
Shared()
{
cout << "ctor() called" << endl;
}
~Shared()
{
cout << "dtor() called" << endl;
}
Shared & operator = (const Shared &other)
{
cout << "operator = called" << endl;
}
};
void test_shared_ptr()
{
typedef boost::shared_ptr<Shared> SharedSP;
typedef vector<SharedSP> VShared;
VShared v;
v.push_back(SharedSP(new Shared()));
v.push_back(SharedSP(new Shared()));
boost::shared_ptr<Shared> a1(new Shared());
printf("a1 reference count: %d\n", a1.use_count());
boost::shared_ptr<Shared> a2 = a1;
printf("a1 reference count: %d\n", a1.use_count());
printf("a2 reference count: %d\n", a2.use_count());
a1.reset();
printf("a2 reference count: %d\n", a2.use_count());
//输出结果:
//a1 reference count: 1
//a1 reference count: 2
//a2 reference count: 2
//a2 reference count: 1
}
//10.boost::asio 译文
//BOOST_ASSERT 断言 在assert上面多了一层封装
其他
//memset函数的原型是:void *memset(void *s,int c,size_t n)
//总的作用:将已开辟内存空间 s 的首 n 个字节的值设为值 c。
memcpy
//void *memcpy(void *dest, const void *src, size_t n);
//从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中
//htonl就是把本机字节顺序转化为网络字节顺序
//htonl()返回一个网络字节顺序的值。
#endif // __CCSCHEDULER_H__