Created
November 17, 2016 10:27
-
-
Save MizukiSonoko/f67505b9df4aa030ed3f1561234d5715 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <type_traits> | |
#include <tuple> | |
#include <unordered_map> | |
#include <vector> | |
#include <algorithm> | |
#include <memory> | |
class Object{ | |
public: | |
virtual std::string get() = 0; | |
}; | |
class Serializable{}; | |
class O : public Object, public Serializable{ | |
public: | |
std::string get(){ | |
return "Object O"; | |
} | |
}; | |
class P : public Object, public Serializable{ | |
public: | |
std::string get(){ | |
return "Object P"; | |
} | |
}; | |
class Q : public Object, public Serializable{ | |
public: | |
std::string get(){ | |
return "Object Q"; | |
} | |
}; | |
class R{ | |
public: | |
std::string get(){ | |
return "Object R"; | |
} | |
}; | |
class Wrapper{ | |
public: | |
virtual void print() = 0; | |
virtual std::string get() = 0; | |
}; | |
template< | |
typename T, | |
typename std::enable_if< | |
std::is_base_of<Serializable,T>::value || | |
std::is_base_of<Object,T>::value | |
>::type* = nullptr | |
> | |
class Kure : public Wrapper{ | |
T obj; | |
public: | |
Kure(T o) : obj(o){} | |
void print(){ | |
std::cout <<"Kure Wrapper "<< obj.get() << std::endl; | |
} | |
std::string get(){ | |
return "Wrapper "+ obj.get(); | |
} | |
}; | |
template< | |
typename T, | |
typename std::enable_if< | |
std::is_base_of<Serializable,T>::value || | |
std::is_base_of<Object,T>::value | |
>::type* = nullptr | |
> | |
class Saran : public Wrapper{ | |
T obj; | |
public: | |
Saran(T o) : obj(o){} | |
void print(){ | |
std::cout <<"Saran Wrapper "<< obj.get() << std::endl; | |
} | |
std::string get(){ | |
return "Wrapper "+ obj.get(); | |
} | |
}; | |
template< | |
typename W, | |
typename U, | |
std::enable_if_t< | |
std::is_base_of<Wrapper, W>::value || | |
std::is_base_of<Serializable, U>::value || | |
std::is_base_of<Object, U>::value, | |
std::nullptr_t | |
> = nullptr | |
> | |
class Plate{ | |
W wrapper; | |
public: | |
Plate(W w) : wrapper(w){} | |
virtual void print() = 0; | |
virtual std::string get() = 0; | |
}; | |
template< | |
typename P, typename W, typename O, | |
std::enable_if_t< | |
std::is_base_of<Object, O>::value, | |
std::nullptr_t | |
> = nullptr | |
> | |
class Table{ | |
P plate; | |
public: | |
Table(P p) : plate(p) {} | |
virtual void print() = 0; | |
virtual std::string get() = 0; | |
}; | |
int main(){ | |
std::vector<std::unique_ptr<Wrapper>> V; | |
V.push_back(std::make_unique<Saran<Q>>(Q())); | |
V.push_back(std::make_unique<Kure<O>>(O())); | |
for(auto& v : V){ | |
v->print(); | |
} | |
//auto s = make_plate<Saran,Q>(); | |
//make_table<Plate,Saran,P>().print(); | |
//Plate<Wrapper<R>,R>(Wrapper(R())).print(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment