#include "boost/algorithm/string.hpp"
#include <functional>
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
// template <typename DstType>
// int (*Handler)(const char*, size_t, DstType*);
/**
* @func : 模板函数指针 作为 另一个函数的入参
*/
template <typename DstType>
using Handler = std::function<int(const char*, size_t, DstType*)>;
class Dict {
public:
Dict() {
dict_["1"] = "1,2,3,4,5,6";
dict_["2"] = "11,22,33,44,55,66";
dict_["3"] = "111,222,333,444,555,666";
}
public:
template <typename T>
int Get(const std::string& key, Handler<T> data_handler, T* result) {
if (result == nullptr) {
return -1;
}
auto iter = dict_.find(key);
if (iter == dict_.end()) {
return -2;
}
return data_handler(iter->second.c_str(), iter->second.length(), result);
}
public:
std::unordered_map<std::string, std::string> dict_;
};
int HandlerTest(const char* value_str, size_t length, std::unordered_set<std::string>* ret) {
std::vector<std::string> tmp;
boost::split(tmp, std::string(value_str, length), boost::algorithm::is_any_of(","));
for (const auto& item : tmp) {
(*ret).insert(item);
}
return 0;
}
int main(int args, char* argv[]) {
Dict dict;
std::unordered_set<std::string> uset;
dict.Get<std::unordered_set<std::string>>("2", HandlerTest, &uset);
for (const auto& item : uset) {
std::cout << item << ", ";
}
std::cout << "\nComplete!\n";
return 0;
}