Hi all,
I'm working on a home project where I'm trying to create a lean reflection layer using RTTI and templating.
At the moment I've got most of it running but some things didn't sit well with me, especiallly since I think it can be improved by smarter people than myself :)
Ok, so here goes: I have/need a single function that can inspect a static type and give usefull information about it. I use this to populate my reflection layer from code without the use of too much macro's and duplication.
So my simplified use case is as follows:
1 2
|
std::vector<std::vector<float*>*> test;
InspectObject( &test );
| |
should (and does) return the string POINTER TO VECTOR OF POINTER TO VECTOR OF POINTER TO FLOAT
I'm using std vectors atm but the same goes for any type of templated container.
I made this work by defining the InspectObject as a set of templated functions:
1 2 3 4 5 6
|
template <typename T> void InspectObject(T& inDummy) { std::wcout << L"GENERIC" << std::endl; }
template <> void InspectObject<int>(int& inDummy) { std::wcout << L"INT" << std::endl; }
template <> void InspectObject<float>(float& inDummy) { std::wcout << L"FLOAT" << std::endl; }
template <> void InspectObject<bool>(bool& inDummy) { std::wcout << L"BOOL" << std::endl; }
template <typename T> void InspectObject(T* inDummy) { std::wcout << L"POINTER TO" << std::endl; InspectObject(*inDummy); }
template <typename T> void InspectObject(std::vector<T>& inDummy) { std::wcout << L"VECTOR OF" << std::endl; InspectObject(*((T*)0)); }
| |
Ugly no? Because of the dummy objects passed in the function; they are never touched and used more as an additional layer of function-matching during template generation.
Ideally I would like the function to be more like:
|
InspectClass< std::vector<std::vector<float*>*> >();
| |
returning the same data. This looks like it should be possible, I just get hung up on the syntax:
1 2 3 4 5
|
template <typename T> void Inspect() { std::wcout << L"GENERIC" << std::endl; }
template <> void Inspect<int>() { std::wcout << L"INT" << std::endl; }
template <> void Inspect<float>() { std::wcout << L"FLOAT" << std::endl; }
template <> void Inspect<bool>() { std::wcout << L"BOOL" << std::endl; }
template <> template <typename C> void Inspect< std::vector>() { std::wcout << L"VECTOR OF" << std::endl; Inspect<C>(); }
| |
For the life of me I can't get that last case to compile properly.
Any thoughts?