I want to make GroupBase as a interface...
#include "string"
#include "unordered_map"
template<class Vty>
class BaseV {
public:
BaseV() {
//logic
}
virtual auto run() -> bool { return false; }
protected:
//members
};
class GroupBase {
public:
GroupBase() {
//logic
}
template<class Vty>
auto run(const std::string &name, const Vty &value) -> void {
createValue<Vty>(name);
((BaseV *)_values.at(name))->run();
}
template<class Vty>
auto createValue(const std::string &name) -> void {
static_assert(false, "You must override createValue method to instead GroupBase::createValue!");
}
protected:
std::unordered_map<std::string, void*> _values{};
};
template<class Vty>
class ThirdPartyClass{};
template<class Vty>
class DerivedV : public BaseV<Vty> {
public:
DerivedV() : BaseV<Vty>() {
_data = new ThirdPartyClass<Vty>();
}
auto run() -> bool override {
//call function in _data;
}
protected:
ThirdPartyClass<Vty>* _data;
};
class GroupDerived : public GroupBase {
public:
GroupDerived () : GroupBase() {
//logic
}
template<class Vty>
auto createValue(const std::string &name) -> void {
_values.emplace(name, new DerivedV<Vty>());
}
};
int main(int argc, char** argv) {
auto group = GroupDerived();
group.run("a", 1);
}
Of course this code cannot work.It will print the message in static_assert
an compile will failed.
If remove this line:
static_assert(false, "You must override createValue method to instead GroupBase::createValue!");
It worked, but an warning will occur.
So how to make it more reasonable? (I have to add a new ThirdPartyClass 1, 2, 3...at any time...)