无详细内容 无 #ifndef COMMAND_H#ifndef COMMAND_H#include stringstream#include string#include vector#include map#include functionalusing namespace std;namespace command{ mapstring, functionvoid(const vectorstring, functionvoid(const string,
#ifndef COMMAND_H #ifndef COMMAND_H #include <stringstream> #include <string> #include <vector> #include <map> #include <functional> using namespace std; namespace command { map<string, function<void(const vector<string>&, function<void(const string&, size_t, size_t)>)>> items; template <typename T> T TextTo(const string& text) { T re = {}; stringstream ss; ss << text; ss >> re; return re; } void Join(const string& name, auto f) { assert(!name.empty()); items[name] = [=](auto args, auto argerrf) { if (args.size() == 1) { f(); } else { argerrf(args[0], 0, args.size() - 1); } }; } template <typename A> void Join(const string& name, auto f) { assert(!name.empty()); items[name] = [=](auto args, auto argerrf) { if (args.size() == 2) { f(TextTo<A>(args[1])); } else { argerrf(args[0], 1, args.size() - 1); } }; } template <typename A, typename B> void Join(const string& name, auto f) { assert(!name.empty()); items[name] = [=](auto args, auto argerrf) { if (args.size() == 3) { f(TextTo<A>(args[1]), TextTo<B>(args[2])); } else { argerrf(args[0], 2, args.size() - 1); } }; } template <typename A, typename B, typename C> void Join(const string& name, auto f) { assert(!name.empty()); items[name] = [=](auto args, auto argerrf) { if (args.size() == 4) { f(TextTo<A>(args[1]), TextTo<B>(args[2]), TextTo<C>(args[3])); } else { argerrf(args[0], 3, args.size() - 1); } }; } vector<string> Split(const string& str, char symbol) { vector<string> result; string unit; for (char c : str) { if (symbol == c) { result.push_back(unit); unit.clear(); } else { unit += c; } } if (unit.size() > 0) { result.push_back(unit); } return result; } void Run(const string& source, auto missf, auto argerrf) { auto units = Split(source, ' '); if (units.size() > 0) { auto it = items.find(units[0]); if (it == items.end()) { missf(units[0]); } else { it->second(units, argerrf); } } } } #endif