Hi, i've written a class template (varList) that takes one type as a template argument.
The class basically allocates a vector of that class.
I then have another class that has a vector of (void) pointers to a number of these classes (varListContainer), which have different types.
The container class has a number of methods that must have a type specified so that the void pointers can be recast to the appropriate subclass type, but i want to write a method that deletes all of the subclasses regardless of which type the class took as a parameter.
At the moment I naively recast the void pointers to Myclass<int> pointers and delete that pointer and it seems to work (look at the function varListContainer::removeall()).
Just wondering if anyone can suggest a better way of doing this or if they can tell me if this naive way is guaranteed to work in all circumstances (i imagine this would be equivalent to saying that the default deconstructor always has the same offset for template class types, regardless of the template parameters).
Here is the source code for inspection:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
class vl
{
public:
void * ptr;
std::string vartype;
vl(void * p_ptr, std::string p_vartype)
{
ptr = p_ptr;
vartype = p_vartype;
}
};
template <class variabletype>
class varList
{
private:
std::vector<variabletype> args;
std::vector<std::string> names;
std::string vartype;
public:
void clear()
{
args.clear();
names.clear();
}
variabletype Get(std::string p_name)
{
int n = 0;
while (n < args.size())
{
if (names[n] == p_name)
{
return args[n];
}
++n;
}
}
void Add(variabletype p_arg, std::string p_name)
{
args.push_back(p_arg);
names.push_back(p_name);
}
varList()
{
vartype = typeid(variabletype).name();
}
};
class varListContainer
{
private:
std::vector<vl> varlists;
public:
template<class variablereturntype>
variablereturntype get(std::string p_name)
{
int n = 0;
varList<variablereturntype> * vl_ptr;
while (n < varlists.size())
{
if (varlists[n].vartype == typeid(variablereturntype).name())
{
vl_ptr = (varList<variablereturntype>*) varlists[n].ptr;
return vl_ptr->Get(p_name);
}
++n;
}
}
template<class variableaddtype>
void addType()
{
int n = 0;
while (n < varlists.size())
{
if (typeid(variableaddtype).name() == varlists[n].vartype) return; //Type already exists in the container
++n;
}
varList<variableaddtype> * vl_ptr;
vl_ptr = new varList<variableaddtype>();
varlists.push_back(vl((void*)vl_ptr,typeid(variableaddtype).name()));
}
template<class variableinsertype>
void add(std::string p_name, variableinsertype p_arg)
{
int n = 0;
varList<variableinsertype> * vl_ptr;
while (n < varlists.size())
{
if (varlists[n].vartype == typeid(variableinsertype).name())
{
vl_ptr = (varList<variableinsertype>*) varlists[n].ptr;
vl_ptr->Add(p_arg, p_name);
}
++n;
}
}
template<class variablecleartype>
void clear()
{
int n = 0;
varList<variablecleartype> * vl_ptr;
while (n < varlists.size())
{
if (varlists[n].vartype == typeid(variablecleartype).name())
{
vl_ptr = (varList<variablecleartype>*) varlists[n].ptr;
vl_ptr->clear();
return;
}
++n;
}
}
template<class variableremovetype>
void remove()
{
int n = 0;
varList<variableremovetype> * vl_ptr;
while (n < varlists.size())
{
if (varlists[n].vartype == typeid(variableremovetype).name())
{
vl_ptr = (varList<variableremovetype>*) varlists[n].ptr;
delete vl_ptr;
varlists.erase(varlists.begin() + n);
return;
}
++n;
}
}
void removeall()
{
int n = 0;
varList<int> * vl_ptr;
while (n < varlists.size())
{
vl_ptr = (varList<int>*)varlists[n].ptr;
delete vl_ptr;
varlists.clear();
++n;
}
}
};
|