Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- enum class TypeID {
- String,
- Float,
- Integer
- };
- template <TypeID>
- struct TypeIdentifier {
- };
- template <>
- struct TypeIdentifier<TypeID::String> {
- typedef std::string type;
- };
- template <>
- struct TypeIdentifier<TypeID::Float> {
- typedef float type;
- };
- template <>
- struct TypeIdentifier<TypeID::Integer> {
- typedef int32_t type;
- };
- struct Data {
- template <typename T>
- std::vector<T> read();
- };
- template <>
- std::vector<std::string> Data::read()
- {
- return std::vector<std::string>{ "Zero", "One", "Two" };
- }
- template <>
- std::vector<float> Data::read()
- {
- return std::vector<float>{ 0.1, 0.2, 0.3 };
- }
- template <>
- std::vector<int32_t> Data::read()
- {
- return std::vector<int32_t>{ 10, 20, 30 };
- }
- template <TypeID ID>
- std::vector<typename TypeIdentifier<ID>::type> process(Data &d)
- {
- return d.read<typename TypeIdentifier<ID>::type>();
- }
- template <typename T>
- void print(const T &t)
- {
- for (auto && i : t)
- std::cout << i << " ";
- std::cout << "\n";
- }
- template <typename T>
- void printReverse(const T &t)
- {
- for (auto it = t.rbegin(); it != t.rend(); it++)
- std::cout << *it << " ";
- std::cout << "\n";
- }
- template <template <TypeID> class P>
- void dispatcher(const TypeID id, Data &d)
- {
- switch (id) {
- case TypeID::String:
- P<TypeID::String>::call(d); break;
- case TypeID::Float:
- P<TypeID::Float>::call(d); break;
- case TypeID::Integer:
- P<TypeID::Integer>::call(d); break;
- }
- }
- template <TypeID>
- struct Proc {};
- template <>
- struct Proc<TypeID::String> {
- static void call(Data &d)
- {
- auto v = process<TypeID::String>(d);
- print(v);
- }
- };
- template <>
- struct Proc<TypeID::Float> {
- static void call(Data &d)
- {
- auto v = process<TypeID::Float>(d);
- print(v);
- }
- };
- template <>
- struct Proc<TypeID::Integer> {
- static void call(Data &d)
- {
- auto v = process<TypeID::Integer>(d);
- print(v);
- }
- };
- template <TypeID>
- struct ProcTwo {};
- template <>
- struct ProcTwo<TypeID::String> {
- static void call(Data &d)
- {
- auto v = process<TypeID::String>(d);
- printReverse(v);
- }
- };
- template <>
- struct ProcTwo<TypeID::Float> {
- static void call(Data &d)
- {
- auto v = process<TypeID::Float>(d);
- printReverse(v);
- }
- };
- template <>
- struct ProcTwo<TypeID::Integer> {
- static void call(Data &d)
- {
- auto v = process<TypeID::Integer>(d);
- printReverse(v);
- }
- };
- int main()
- {
- Data d;
- dispatcher<Proc>(TypeID::String, d);
- dispatcher<Proc>(TypeID::Float, d);
- dispatcher<Proc>(TypeID::Integer, d);
- dispatcher<ProcTwo>(TypeID::String, d);
- dispatcher<ProcTwo>(TypeID::Float, d);
- dispatcher<ProcTwo>(TypeID::Integer, d);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement