728x90
mem_fn 을 이용해서 어떤 공통된 function 을 구동 시키되 넘기는 변수는 tuple 을 이용해 유동성 있게 넘기는
방법을 예제로 만들었습니다 1타 2피! 예제랄까... serialize 때 조금 심플해 보일 순 있겠네요.. 가독성이나 처리
문제등 개선점은 있지만;;
// start
#include "stdafx.h"
#include <regex>
#include <tuple>
#include <functional>
#include <boost/function.hpp>
class CWork
{
private:
typedef void(*const fun)();
std::tr1::reference_wrapper<fun> m_RunFunc;
int m_Val;
public:
void SetWrapper(std::tr1::reference_wrapper<fun> f)
{
m_RunFunc = f;
}
template<class T> void Do(T val)
{
std::cout << std::tr1::tuple_size<T>::value << std::endl;
// 사실상 0 번만 알면 뭐.. 시리얼라이즈 분류는..
std::cout << std::tr1::get<0>(val) << std::endl;
std::cout << std::tr1::get<1>(val) << std::endl;
// output 2, 1, 3.14
m_RunFunc();
}
CWork(int n, std::tr1::reference_wrapper<fun> f) : m_Val(n), m_RunFunc(f) {};
};
void Print()
{
std::cout << "-_-!" << std::endl;
}
void Print2()
{
std::cout << "-_-!!" << std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
CWork temp(9, std::tr1::cref(&Print));
temp.SetWrapper(std::tr1::cref(&Print2));
typedef std::tr1::tuple<int, float> tyTuple;
tyTuple t = std::tr1::make_tuple(1, 3.14f);
boost::mem_fn(&CWork::Do<tyTuple>)(temp, t);
int n;
std::cin >> n;
return 0;
}
// end
728x90
'프로그래밍 > boost' 카테고리의 다른 글
| boost::unordered set, map, mutiset, multimap (0) | 2012.03.26 |
|---|---|
| boost::bind 의 기본 사용법 예제 (0) | 2011.11.16 |
| boost::asio 비동기 서버/클라 step 1 (0) | 2011.11.09 |
| WorkQueue & Worker (0) | 2011.11.08 |
| boost::thread / boost::thread_group (0) | 2011.11.08 |
댓글