Basically I am trying to create a templated class that can take either be instantiated in a user friendly way or in a more complex way offering more configurability. I want to offer these two instantiating methods WITHOUT HAVING TO DUPLICATE THE API.
The class would look something along the lines of:
// templates that offer a lot of configurability
template<typename T, typename some_probably_awful_to_type_nested_class>
class has_my_api {
};
// templates that are easy to use
template<typename T, int i>
class has_my_api {
};
The idea is that I can build a generic version of some_probably_awful_to_type_nested_class
with i
but the two classes would call the exact same api.
I know I could do this by having an inner type with the actual API and having the API of each of these two classes just make calls to that inner class but I am looking for a way to do this without duplicating code.
Here is a more concrete example of what I'm looking for:
#define DEFAULT_CONFIG 0
//////////////////////////////////////////////////////////////////////
// Nested classes themselves
template<typename T>
class inner_nested_class {
// some fairly complex api
};
template<typename T,
typename inner_nested_class_t,
int configuration_flags = DEFAULT_CONFIG>
class outer_nested_class {
// some data structure of inner_nested_class
};
//////////////////////////////////////////////////////////////////////
// Helpers for creating nested_class from int seed
template<typename T, int i>
struct nested_class_creator;
template<typename T>
struct nested_class_creator<T, 0> {
typedef inner_nested_class<T> type;
};
template<typename T, int i>
struct nested_class_creator {
typedef outer_nested_class<T, typename nested_class_creator<T, i - 1>::type> type;
};
template<typename T, typename outer_nested_class_t>
class manager_class {
manager_class() = default;
int foo();
int bar();
int baz();
// some fairly involved additional API...
};
/* What i do NOT want to do
template<typename T, typename outer_nested_class_t>
class manager_class_api {
manager_class<T, outer_nested_class_t> inner_m;
int foo() { return inner_m.foo() };
int bar() { return inner_m.bar() };
int baz() { return inner_m.baz() };
};
template<typename T, int i>
class manager_class_api {
manager_class<T, typename nested_class_creator<T, i>::type> inner_m;
int foo() { return inner_m.foo() };
int bar() { return inner_m.bar() };
int baz() { return inner_m.baz() };
};
*/
/*
have tried... where the unified api could be called through _manager_class
template<typename T, int i>
using _manager_class = manager_class <T, typename nested_class_creator<T, i>::type>;
template<typename T, typename outer_nested_class_t>
using _manager_class = manager_class <T, outer_nested_class_t>;
But obviously it does not work...
*/
int
main() {
manager_class <int, outer_nested_class<int, outer_nested_class<int, inner_nested_class<int>, 0x4>, 0x3>> manager_with_user_specified_configs;
// how do I do this?
//manager_class <int, 3> manager_with_simply_api;
}
Is this possible to do? If so how can I do it?
I am happy using any version of C++ >= 11
Note: I am aware that I could use a preprocessor macro for this as a worse case scenario. If possible I would rather find a solution so the user could simply specify an int or type.
Aucun commentaire:
Enregistrer un commentaire