-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfunction.h
More file actions
155 lines (131 loc) · 3.67 KB
/
function.h
File metadata and controls
155 lines (131 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/***************************************************************************
*
* Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
*
**************************************************************************/
/**
* @file function.h
* @author shichengyi(com@baidu.com)
* @date 2014/03/09 20:30:56
* @brief 定义了各种类型的解析函数以及用于保存用户函数的类
* @version 1.0
*
**/
#ifndef PSSPIDER_CODEMASTER_FUNCTION_H
#define PSSPIDER_CODEMASTER_FUNCTION_H
#include <map>
#include <sstream>
#include <vector>
#include <boost/any.hpp>
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
#include "common.h"
#include "util.h"
namespace cm
{
template <typename T>
class ArrayDeleter
{
public:
void operator() (T* & array) const
{
delete[] array;
array = NULL;
}
};
template <typename T>
class Function
{
public:
typedef boost::function<void(const std::string&, T*)> Type;
static void str_to_type(const std::string& str, T* ret);
private:
DISALLOW_COPY_AND_ASSIGN(Function);
};
/**
* @author shichengyi(com@baidu.com)
* @brief str_to_type string 转成 T类型
*
* @tparam T 类型参数
* @param str
* @param ret 返回T类型值
*/
template <typename T>
void Function<T>::str_to_type(const std::string& str, T* ret)
{
std::stringstream str_stream;
str_stream << str;
str_stream >> *ret;
}
template <>
class Function<char*>
{
public:
typedef boost::function<void(const std::string&, boost::shared_ptr<char>*)> Type;
static void str_to_type(const std::string& str,
boost::shared_ptr<char>* ret,
size_t* size);
private:
DISALLOW_COPY_AND_ASSIGN(Function);
};
template <typename T>
class Function<T*>
{
public:
typedef boost::function<void(const std::string&, boost::shared_ptr<T>*)> Type;
static void str_to_type(const std::string& str, boost::shared_ptr<T>* ret, size_t* size);
private:
DISALLOW_COPY_AND_ASSIGN(Function);
};
/**
* @author shichengyi(com@baidu.com)
* @brief str_to_type 转为 build-in 数组类型
*
* @tparam T
* @param str 待转字符串,使用','分割
* @param ret 数组指针
* @param size 数组长度
*/
template <typename T>
void Function<T*>::str_to_type(const std::string& str, boost::shared_ptr<T>* ret, size_t* size)
{
std::vector<T> type_t_vector;
cm::Util::separate_string<T>(str, ",", &type_t_vector);
*size = type_t_vector.size();
T* pointer = new T[*size];
std::copy(type_t_vector.begin(), type_t_vector.end(), pointer);
ret->reset(pointer, ArrayDeleter<T>());
}
class UserFunction
{
public:
typedef std::map<const std::string, boost::any> FuncListType;
template <typename T>
static void add_function(typename Function<T>::Type func);
template <typename T>
static cm::ErrorCode get_function(const std::string& name,
typename Function<T>::Type* func);
private:
static std::map<const std::string, boost::any> _s_functions;
DISALLOW_COPY_AND_ASSIGN(UserFunction);
};
template <typename T>
void UserFunction::add_function(typename Function<T>::Type func)
{
_s_functions[typeid(T).name()] = func;
}
template <typename T>
typename cm::ErrorCode UserFunction::get_function(const std::string& name,
typename Function<T>::Type* user_func)
{
FuncListType::iterator it = _s_functions.find(name);
if (it == _s_functions.end())
{
return TYPE_NOT_FOUND;
}
typedef typename cm::Function<T>::Type StrtoType;
*user_func = boost::any_cast<StrtoType>(it->second);
return OK;
}
}
#endif //PSSPIDER_CODEMASTER_FUNCTION_H