472,784 Members | 803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,784 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 1598
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.