-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdict_processor.h
More file actions
178 lines (155 loc) · 4.28 KB
/
dict_processor.h
File metadata and controls
178 lines (155 loc) · 4.28 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/***************************************************************************
*
* Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
*
**************************************************************************/
/**
* @file dict_processor.h
* @author shichengyi(com@baidu.com)
* @date 2014/03/09 14:06:34
* @brief 处理每一行的数据,返回用户需要的类型
* @version 1.0
*
**/
#ifndef PSSPIDER_CODEMASTER_DICT_PROCESSOR_H
#define PSSPIDER_CODEMASTER_DICT_PROCESSOR_H
#include <sstream>
#include <boost/shared_ptr.hpp>
#include "common.h"
#include "function.h"
namespace cm
{
class DictProcessor
{
public:
DictProcessor();
~DictProcessor();
void process(const std::string& line);
template <typename T>
cm::ErrorCode get_column_value(const size_t column, T* value);
template <typename T>
cm::ErrorCode get_column_value(const size_t column,
boost::shared_ptr<T>* value,
size_t* size);
const std::vector<std::string>& get_columns()
{
return _columns;
}
private:
std::vector<std::string> _columns;
DISALLOW_COPY_AND_ASSIGN(DictProcessor);
};
namespace type
{
template <typename D>
class UserType
{
public:
/**
* @author shichengyi(com@baidu.com)
* @brief operator() 解析用户自定义类型
* @param processor 处理器
* @param column 所在列
* @param t 返回值
*
* @return ErrorCode
*/
cm::ErrorCode operator()(cm::DictProcessor* processor, const size_t column, D* t) const
{
if (processor == NULL || t == NULL)
{
return NULL_POINTER;
}
const std::vector<std::string>& column_vector = processor->get_columns();
if (column >= column_vector.size())
{
return OUT_OF_BOUNDS;
}
const std::string& data = column_vector[column];
typedef typename cm::Function<D>::Type StrtoType;
StrtoType user_function;
cm::ErrorCode ret = cm::UserFunction::get_function<D>(typeid(D).name(), &user_function);
user_function(data, t);
return ret;
}
};
template <typename D>
class BaseType
{
public:
/**
* @author shichengyi(com@baidu.com)
* @brief operator() 解析 build-in 类型
*
* @param processor 处理器
* @param column 所在列
* @param t 返回值
* @return ErrorCode
*/
cm::ErrorCode operator()(cm::DictProcessor* processor, const size_t column, D* t) const
{
if (processor == NULL || t == NULL)
{
return NULL_POINTER;
}
const std::vector<std::string>& column_vector = processor->get_columns();
if (column >= column_vector.size())
{
return OUT_OF_BOUNDS;
}
const std::string& data = column_vector[column];
cm::Function<D>::str_to_type(data, t);
return OK;
}
};
}
/**
* @author shichengyi(com@baidu.com)
* @brief get build-in 类型 或者 用户自定义类型
*
* @tparam T 类型
* @param column 所在列
* @param val 返回值
*
* @return ErrorCode 返回值
*/
template <typename T>
cm::ErrorCode DictProcessor::get_column_value(const size_t column, T* value)
{
typedef typename boost::mpl::if_<
typename boost::is_class<T>::type,
type::UserType<T>,
type::BaseType<T> >::type GetType;
return GetType()(this, column, value);
}
/**
* @author shichengyi(com@baidu.com)
* @brief get build-in 类型数组
*
* @tparam T 数组类型
* @param column 所在列
* @param value 返回值
* @param size 数组长度
*
* @return ErrorCoe 数组指针 数组长度
*/
template <typename T>
cm::ErrorCode DictProcessor::get_column_value(const size_t column,
boost::shared_ptr<T>* value,
size_t* size)
{
if (size == NULL)
{
return NULL_POINTER;
}
const std::vector<std::string>& column_vector = get_columns();
if (column >= column_vector.size())
{
return OUT_OF_BOUNDS;
}
const std::string& data = column_vector[column];
cm::Function<T*>::str_to_type(data, value, size);
return OK;
}
}
#endif //PSSPIDER_CODEMASTER_DICT_PROCESSOR_H