List of template classes

Q

Quinlan Morake

Good day,

I have the following code and am uncertain as to how it should best done / thought of. I'm creating a "data access layer", and am aware that there arec++ libraries for this, but am just doing it for the practice. I would like to have a dbField class, that holds the various column info, that is the name, type and data. A dbClass would then hold a list of dbFields, mapping to the columns available in the table. The dbField will thus have differentdata depending on that of the database column, i.e. integer / varchar / bit etc.

My implementation thinking is as follows

enum eDbFieldType
{
Varchar, Integer, Boolean
};

template <class T>
class dbField
{
private:
// Name of column in the database
QString fieldName;
// Column datatype
eDbFieldType dtColumn;
// Data the field contains
T data;

public:
void setData(T);
T getData();
};

class DbClass
{
private:
map<QString, dbField<> > mDbFields;
....

After loading the data from the database into the dbField, I may have to doother things to it depending on its type, for example to base64 decode/encode, etc when getData() is called, and then return the data.

I figure I could use a void* for data, is that what is generally done in this type of situation? I've read that templates are preferable to void* but is that the case here and can they be used? Or should I be thinking differently and do something different? I'm ideally trying to avoid casting.
 
Q

Quinlan Morake

Take a look at boost.variant.



/Leigh

Thanks Leigh, will have a look. Is my thinking about it okay, as in, in this type of scenario, is that what is usually done?
 
L

Larry Evans

On 09/10/13 16:35, Leigh Johnston wrote:
[snip]
Boost.variant is similar to a discriminated (tagged) union which is
basically what you want; no need for your class to be a template when
using it, just have the variant be of all the possible field types you
want to support.

boost::variant is not *exactly* like a tagged union in that:

boost::variant<T1,T1> v1_1;

is not possible because, in the following

T1 a1;
v1_1 = a1;

boost::variant doesn't know if the a1 should be put into the 1st T1 or
the second. IOW, after the assignment to v1_1, what should be the
value of v1_1.which()? This may not be bad in the OP case; however,
it did cause some surprising results in some cases. IIRC, the case
involved something like the variant:

boost::variant<T1,T2> v1_2;

where T2 could be converted to T1, hance variant<T1,T2> behaved
unexpectedly. I think the problem was something like:

v1_2 = T2();

but afterwards:

v1_2.which()==1;

indicating a T1 had been stored instead of a T2.

There are also other problems with boost::variant as shown by this
thread:

http://article.gmane.org/gmane.comp.lib.boost.devel/238011

HTH.

-Larry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top