PIP 5.5.3
Platform-Independent Primitives
Открытые члены | Полный список членов класса
Шаблон класса PIParseHelper< Key >

Helper class to automate structs receive. Подробнее...

#include <piparsehelper.h>

Открытые члены

 PIParseHelper ()
 Construct PIParseHelper.
 
void assign (Key key, std::function< void()> func)
 Assign key "key" to lambda-function "func" without arguments.
 
template<typename T >
void assign (Key key, std::function< void(const T &)> func)
 Assign key "key" to lambda-function "func" with 1 argument.
 
template<typename O >
void assign (Key key, O *obj, void(O::*member_func)())
 Assign key "key" to member function of object "obj" without arguments.
 
template<typename T , typename O >
void assign (Key key, O *obj, void(O::*member_func)(const T &))
 Assign key "key" to member function of object "obj" with 1 argument.
 
template<typename L >
void assign (Key key, L func)
 Assign key "key" to functor "func" with 0 or 1 argument.
 
void parse (Key key, PIByteArray ba)
 Deserialize data and invoke assigned to "key" methods.
 

Подробное описание

template<typename Key>
class PIParseHelper< Key >

Helper class to automate structs receive.

Synopsis

This class helps to deserialize and invoke neccessarily methods.

Data packets with header and various data types can be automated by this class. Every key value mapped to object member function, lambda-function or functor.

This class can remove switch-case with deserialization code and replace it with several assign() calls, binded to ready-to-use event handlers. Moreover data type automatic takes from event handler or lambda argument. One should only make "PIByteArray & operator <<()" with used types, deserialization will be performed by PIParseHelper.

Usage

Create instance of PIParseHelper, or subclass.

In assign() methods you can use object member function, lambda-function or functor with 0 or 1 arguments,

Lambda-functions

assign(1, [this](const SomeStruct & s){})
void assign(Key key, std::function< void()> func)
Assign key "key" to lambda-function "func" without arguments.
Definition: piparsehelper.h:80

Examples

First example describes subclass variant. As one can see, it`s a single place to change type of received data - event handler argument.

enum Header {
hInt = 1,
hString,
hVoid
};
class MyObj: public PIObject, public PIParseHelper<uchar> {
PIOBJECT(MyObj);
public:
MyObj(): PIParseHelper<uchar>(this) {
// Keys with 1 argument
assign(hInt, std::function<void(int)>([this](int i){piCout << "lambda type Int" << i;}));
assign(hInt, HANDLER(methodI));
assign(hString, HANDLER(methodS));
// hVoid key, without arguments
assign(hVoid, [](){piCout << "type void";});
assign(hVoid, HANDLER(method));
}
EVENT_HANDLER1(void, methodI, int, i) {piCout << "methodI" << i;}
EVENT_HANDLER1(void, methodS, PIString, s) {piCout << "methodS" << s;}
EVENT_HANDLER0(void, method) {piCout << "method";}
};
int main() {
MyObj o;
data.clear();
data << 11;
o.parse(hInt, data);
// lambda type Int 11
// methodI 11
data.clear();
data << PIString("text");
o.parse(hString, data);
// methodS text
data.clear();
o.parse(hVoid, data);
// type void
// method
}
Класс PIByteArray представляет собой массив байтов.
Definition: pibytearray.h:42
Этот класс является базовым для использования механизма события -> обработчики.
Definition: piobject.h:41
Helper class to automate structs receive.
Definition: piparsehelper.h:73
Класс строки.
Definition: pistring.h:42
#define piCout
Макрос для условного (piDebug) вывода в PICout(StdOut)
Definition: picout.h:35

Second example show separate variant:

enum Header {
hInt = 1,
hString,
hVoid
};
class MyObj: public PIObject {
PIOBJECT(MyObj);
public:
EVENT_HANDLER1(void, methodI, int, i) {piCout << "methodI" << i;}
EVENT_HANDLER1(void, methodS, PIString, s) {piCout << "methodS" << s;}
EVENT_HANDLER0(void, method) {piCout << "method";}
};
int main() {
MyObj obj;
PIParseHelper<int> parser(&obj);
parser.assign(hInt, obj.HANDLER(methodI));
parser.assign(hString, obj.HANDLER(methodS));
parser.assign(hVoid, obj.HANDLER(method));
data.clear();
data << 11;
parser.parse(hInt, data);
// methodI 11
data.clear();
data << PIString("text");
parser.parse(hString, data);
// methodS text
data.clear();
parser.parse(hVoid, data);
// method
}