目录
Level 1 无模式
std::cout << "Level 1 无模式\n";
Level 2 简单模式,数据分离
auto DoPrint = [](const char* data)
{
std::cout << data;
std::cout << "\n";
};
const char* data = GetData("Level 2 简单模式,数据分离\n");
DoPrint(data);
Level 3 简单模式,数据与方法分离
using DoMethodVRetPStr = void(const char*);
const char* data2 = GetData("Level 3 简单模式,数据与方法分离\n");
DoMethodVRetPStr *method1 = DoPrint;
method1(data2);
Level 4 简单协议模式
struct DataProtocol
{
const char* m_para;
DoMethodVRetPStr* m_handler;
};
DataProtocol testData[] = {
{"Level 4 简单协议模式", DoPrint},
{"...", DoOthers},
{nullptr, nullptr}
};
auto TestProtocol = [](DataProtocol& data) ->void
{
data.m_handler(data.m_para);
};
TestProtocol(testData[0]);
Level 5 文本协议
enum class EKeyType:int
{
eKeyType_Data = 0,
eKeyType_Method,
eKeyType_Loop
};
std::map<EKeyType, const char *> KeyConfigs = {
{EKeyType::eKeyType_Data,"data"},
{EKeyType::eKeyType_Loop,"loop"},
{EKeyType::eKeyType_Method, "method"}
};
std::map<const char *, DoMethodVRetPStr* > MethodConfigs ={
{ "DoPrint", DoPrint},
{ "DoOthers", DoOthers}
};
auto prt_Analyse_GetMethod = [&](const char* data)->DoMethodVRetPStr*
{
json requestJson = json::parse(data);
const char* key = KeyConfigs[EKeyType::eKeyType_Method];
std::string methodName = requestJson.value(key, "");
if (methodName == "")
return nullptr;
for (auto it : MethodConfigs)
{
if (methodName.compare(it.first) == 0)
return it.second;
}
return nullptr;
};
auto prt_Analyse_GetData = [&](const char* data)->std::string
{
json requestJson = json::parse(data);
const char* key = KeyConfigs[EKeyType::eKeyType_Data];
return requestJson.value(key, "");
};
auto prt_Run = [&](const char* data)
{
DoMethodVRetPStr* handler = prt_Analyse_GetMethod(data);
std::string handerData = prt_Analyse_GetData(data);
DataProtocol _setData = { handerData.c_str() , handler };
TestProtocol(_setData);
};
const char* protocolRequest = R"(
{
"method": "DoPrint",
"data": "Level 5 TEST PROTOCAL "
}
)";
prt_Run(protocolRequest);
Level 6 文本协议-- 扩充功能,添加逻辑流程控制
auto DoLoop = [](int loopCount, DataProtocol* hander) {
if (!hander)
return;
for (; loopCount > 0; loopCount--)
{
hander->m_handler(hander->m_para);
}
};
using LoopLogicType = void(int , DataProtocol*);
std::map<std::string, LoopLogicType* > logicMethodConfigs = {
{ "loop", DoLoop}
};
auto prt_Analyse_GetLoop = [&](const char* data,int& outData) -> LoopLogicType*
{
json requestJson = json::parse(data);
std::string key = KeyConfigs[EKeyType::eKeyType_Loop];
if (requestJson.contains(key))
{
outData = requestJson.value(key,1);
for (auto it : logicMethodConfigs)
{
if (key.compare(it.first) != std::string::npos)
return it.second;
}
}
return nullptr;
};
auto prt_Run_extra = [&](const char* data)
{
DoMethodVRetPStr* handler = prt_Analyse_GetMethod(data);
std::string handerData = prt_Analyse_GetData(data);
DataProtocol _setData = { handerData.c_str() , handler };
int loopCount = 0;
LoopLogicType *loopMethod = prt_Analyse_GetLoop(data, loopCount);
if(loopMethod)
{
loopMethod(loopCount, &_setData);
}
};
const char* protocolRequest_extra = R"(
{
"method": "DoPrint",
"data": "Level 6 TEST PROTOCAL extention , add Loop control",
"loop":3
}
)";
prt_Run_extra(protocolRequest_extra);
Level 7 自定义脚本语法编译器,此处略过
const char* script = R"(
loopCount = 3
for i in range(0,loopCount){
print "Level7 run script"
}
)";