dynamic creation of template class objects

Apr 12, 2010 at 11:35am
Hello,

I need to read a delimited file and then extract fields and other information that i want to store into a template class object. Every field will go to a template class object. Just because previously i don´t know the number of fields , i don´t know how many objects i have to create and overall, how to refer them in my program because i can´t name this objects dynamically.

I understand that with a "simple" types simply using maps o vector will solve the problem, but here my problem is that i have to use template class, .

Thank you in advance,
Apr 12, 2010 at 2:23pm
Any chance of explaining/posting your template thing? I don't see what the problem is.
Apr 12, 2010 at 3:11pm
of course, my basic template class to store the info is something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 template <typename TDATA,typename TREG>
 class datos{                 
 public:
   TDATA dato;
   TREG tipo;
      /*initialization list*/
 datos(
  TDATA   vdato     = TDATA(),
  TREG tipo_dato = TREG()
  ):
  dato( vdato ),
  tipo( tipo_dato )
  { }
   };


Then in another superior class, I have a vector that contains (among other things) elements of type

vector <datos<TDATA,TREG> >

This, basically is designed to get data itself and its type, being the type a predefined char or int values , depending, but knowing this types of course. Of corse data can be integer, double, string .. whatever.

So, my problem is that i have to be able to read a delimited and previously unknown file and fill the information in vectors like the above and then write them to files. To do that, I have to create instances of this classes , but I don´t know how many at compile time, and the real problem, I don´t know how to asign them a name to control them.

Right now I´m thinking about the posibility of reading all as strings, putting all in a vector of fields(type string) and then build a class object whith the values converted through istream for example. But this solution can throw problems with memory because I have to work with very large amounts of data.

Hope you understand the problem better now. Thank you again.
Last edited on Apr 12, 2010 at 3:13pm
Apr 12, 2010 at 3:23pm
If you want to store type/value pairs, how do you determine the type? Can I assume that you're reading just values from the file?

There is a STL utility class that you can use to replace your datos class, it's called pair.

It's clear you already know how to read a comma delimted line into a container. So am I right in assuming the problem is in filling in the type field?
Apr 12, 2010 at 3:39pm
the type can be passed as argument in a function (separating them by a certain character) or being in the first line of the file being the first line like this:

fieldname1-datatype1#fieldname2-datatype2#.... and so on, so I know the field names, quantity and type at run time, but no at compile time.

Then I read the file with getline and I want to store each field data and its type in a vector of class datos. I have simplified the problem because actually the class have more than two parameters , not only the type and the data itself. Well , to store data of each field in a separate file I need to create a class object for each one and then invoke function of writing an reaind associated to that class. That is my problem how to create object_of_class_n1 ... object_of_class_nk, and work with them when I don´t know at comile time either type for the temple neither how many objects I will need.

Regards
Apr 12, 2010 at 5:16pm
I don´t know at comile time either type for the temple neither how many objects

Typically, template magic happens at compile time. It's possible that a direct templated type may not be appropriate.

The number of objects is easy, that's what collections are for.
Apr 13, 2010 at 6:34pm
I`ll try to read all as string and store them in a vector or some dynamic collection. After that , I´ll use a generic instance of the template class for each element of the vector, and depending on the type covert the string in the correct one.I can create the right chioce of template call by very simple if-then-elseif.. conditions. The problem with that is more memory resources needed , so I have to try since I have to flush and serialize this in a filebefore certain memory ammount has consumed, so the buffer will get smaller i think, but sorry that´s not part of the question.

regards and thank you again.
Topic archived. No new replies allowed.