type_ptr is a type that can be used to hold objects in a container for instance, and can be used to select function in runtime.
Sample
struct Box
{
~Box()
{
std::cout << "~box" << std::endl;
}
};
struct Circle
{
~Circle()
{
std::cout << "~circle" << std::endl;
}
};
void Print(Box& box)
{
std::cout << "box" << std::endl;
}
void Print(Circle& circle)
{
std::cout << "circle" << std::endl;
}
struct F
{
template<class T> static void Call(T& r) { Print(r); }
};
void Test1()
{
std::vector<type_ptr> v;
v.emplace_back(new Box());
v.emplace_back(new Circle());
//Option 1
for (auto & item : v)
{
if (auto p = item.is_ptr<Box>())
{
Print(*p);
}
else if (auto p = item.is_ptr<Circle>())
{
Print(*p);
}
}
//Option 2
for (auto & item : v)
{
switch (is_index<Box, Circle>(item))
{
case 1:
Print(item.ref<Box>());
break;
case 2:
Print(item.ref<Circle>());
break;
default:
break;
}
}
//Option 3
for (auto & item : v)
{
Select<F, Box, Circle>(item);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Test1();
return 0;
}
type_ptr.h ```cpp
class type_ptr
{
template
protected:
void(DeleteF)(void); void * ptr; const type_info* tinfo;
public:
typeptr(typeptr&& other) { ptr = other.ptr; tinfo = other.tinfo; DeleteF = other.DeleteF;
other.ptr = nullptr;
other.tinfo = nullptr;
other.DeleteF = nullptr;
}
template
typeptr() { ptr = nullptr; typeinfo* tinfo = nullptr; }
~type_ptr() { if (DeleteF != nullptr) { DeleteF(ptr); ptr = nullptr; } }
template
template
template
template
template
return isindeximp
template
template
template
return call_imp
template