473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

is there a good way to save data of different types ?

Hi there, for a long time I've been trying to think of way of saving
different data of different types using one single class (well 2 in
reality, a class for the data, and 1 class for a list of data). The
problem that I try to solve is the following. Imagine a class
"Attribute" that needs to save multiple "Parameters" (the number of
parameters for 1 attribute will vary each time we run the application).
Each parameter can be of a different type (float, integer, double...)
and we can have 1 value per parameter or an array of values.

So I tried to create a template class (parameter) that would hold the
data, the name of the parameter & its type (using typeid). Then i
created a list (deque) in the Attribute class which is a list of
pointers to Parameter objects (which are cast to void*). I was hoping i
could save the type of the created parameters using something like
typeid for example to cast a parameter object when I read them back
from the list. Something like:

if ( attr.m_parameters[ 0 ]->m_type == "f" ) {
float afloat = (float)attr.m_parameters[ 0 ]->get( 0 );
}

It doesn't seem to work so well. The last line of code fails to compile
(`void*' is not a pointer-to-object type). I imagine that this is a
need that a lot of developper must have had before and that maybe
there's a good/better way of doing that. What I am trying to avoid is
something like that

class Parameter {
deque<floatm_floats;
deque<doublem_doubles;
deque<stringm_strings;
...
void insertFloat( float arg ) { m_floats.push_back( arg ); }
void insertInterger( int arg ) m_integers.push_back( arg ); }
...
};

I am happy to go with that if experienced developpers tell me it is
still the best way to go otherwise would be happy to learn of a better
way ...

Many thanks for your help. -mark
>>>>CODE <<<<<<<<<<<<<
template<typename T>
class TParameter {
public:
TParameter( const char *name ) : m_name( name ) {
m_type = typeid( T ).name();
}
~TParameter() {}
void insert( T arg ) {
m_data.push_back( (T)arg );
}

T get( int i ) {
if ( i <= ( m_data.size() - 1 ) && i >= 0 )
return (T)(m_data[ i ]);
}
public:
deque<Tm_data;
const char* m_name;
const char* m_type;
};

class TAttribute {
public:
TAttribute() {}
~TAttribute() {}
public:
deque<void *m_parameters;
};

int main()
{

TParameter<float*floatArray = new TParameter<float>( "floatArray"
);
TAttribute attr;
attr.m_parameters.push_back( (void*)(floatArray) );

floatArray->insert( (float)-1.0 );
floatArray->insert( (double)-2.0 );
floatArray->insert( (float)-3.0 );

cout << floatArray->get( 0 ) << endl;
cout << floatArray->get( 1 ) << endl;
cout << floatArray->get( 2 ) << endl;

// compile fails here !
//cout << "Type of this attribute: " << attr.m_parameters[ 0
]->m_type << endl;
}

Jul 10 '06 #1
2 1643
Hi again for those who had the kindness of reading the post here's an
update. I've managed to get the code to work by doing the following
thing (see code below). There's still room for improvement (the const
char to save the type of the parameter could be an integer constant
from an enum type declaration (which would allow me to do a swich of
the type).

Let me know if that seems a good way to go or if I what i am doing is
messy or enifficient. thank you -

template<typename T>
class TParameter {
public:
TParameter( const char *name ) : m_name( name ) {
m_type = typeid( T ).name();
}
~TParameter() {}
void insert( T arg ) {
m_data.push_back( (T)arg );
}
T operator[]( int i ) {
if ( i < ( m_data.size() - 1 ) && i >= 0 )
return m_data[ i ];
}
T get( int i ) {
if ( i <= ( m_data.size() - 1 ) && i >= 0 )
return (T)(m_data[ i ]);
}
public:
deque<Tm_data;
const char* m_name;
const char* m_type;
};

class TAttribute {
public:
TAttribute() {}
~TAttribute() {}
public:
deque<pair<const char *, void * m_parameters;
};

int main()
{

TParameter<float*floatArray = new TParameter<float>( "floatArray"
);
TAttribute attr;
attr.m_parameters.push_back( pair<const char*, void*>( "float",
(void*)(floatArray) ) );

floatArray->insert( (float)-1.0 );
floatArray->insert( (double)-2.0 );
floatArray->insert( (float)-3.0 );

cout << floatArray->get( 0 ) << endl;
cout << floatArray->get( 1 ) << endl;
cout << floatArray->get( 2 ) << endl;

// get the type of the parameter
cout << "Parameter type: " << attr.m_parameters[ 0 ].first << endl;

if ( attr.m_parameters[ 0 ].first == "float" ) {
TParameter<float*tparam = (TParameter<float*)(
attr.m_parameters[ 0 ].second );
cout << "Value at index 0: " << tparam->get( 0 ) << endl;
cout << "Value at index 1: " << tparam->get( 1 ) << endl;
cout << "Value at index 2: " << tparam->get( 2 ) << endl;
}
}

Jul 10 '06 #2
ma*****@yahoo.com schrieb:
Hi there, for a long time I've been trying to think of way of saving
different data of different types using one single class (well 2 in
reality, a class for the data, and 1 class for a list of data). The
problem that I try to solve is the following. Imagine a class
"Attribute" that needs to save multiple "Parameters" (the number of
parameters for 1 attribute will vary each time we run the application).
Each parameter can be of a different type (float, integer, double...)
and we can have 1 value per parameter or an array of values.
Look at:
http://www.boost.org/doc/html/variant.html

And:
http://www.boost.org/doc/html/any.html

--
Thomas
Jul 10 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Shailesh Humbad | last post by:
I wrote a short page as a quick reference to c++ integer data types. Any feedback welcome: http://www.somacon.com/blog/page11.php
9
by: Koen | last post by:
Hi all, My application uses a lot of lookup tables. I've splitted the frontend (forms, reports, etc) from the backend (data). The database has around 10 different users. The values in the...
7
by: farseer | last post by:
Here is the scenario: I have an interface which defines get methods for data that will make up a row in a table. However, the source of this data may, over time, switch/change (The company may...
59
by: Alan Silver | last post by:
Hello, This is NOT a troll, it's a genuine question. Please read right through to see why. I have been using Vusual Basic and Classic ASP for some years, and have now started looking at...
5
by: sherifffruitfly | last post by:
Hi, I'm just learning cpp, and the exercise I'm working on is basically as follows: 1) Create a struct type with 4 members (char, char, char, int). 2) Create an array of, say 3 instances of...
26
by: vlsidesign | last post by:
I am a newbie and going through "The C programming language" by Kernighan & Richie on my own time (I'm not a programmer but I want to learn because it can save me time in my normal job, and it is...
17
by: Kevin Hall | last post by:
C++ is one of my favorite languages to work in. This is because it has so many differrent strengths. But there is also a number of blemishes in the language -- some could potentially be fixed,...
11
by: Angus | last post by:
I am developing a server which receives a range of different messages. There are about 12 different message types so I thought that a good idea would be to devise a class for each message type....
23
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.