473,587 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_paramete rs[ 0 ]->m_type == "f" ) {
float afloat = (float)attr.m_p arameters[ 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_fl oats;
deque<doublem_d oubles;
deque<stringm_s trings;
...
void insertFloat( float arg ) { m_floats.push_b ack( 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<typena me T>
class TParameter {
public:
TParameter( const char *name ) : m_name( name ) {
m_type = typeid( T ).name();
}
~TParameter() {}
void insert( T arg ) {
m_data.push_bac k( (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<floa t*floatArray = new TParameter<floa t>( "floatArray "
);
TAttribute attr;
attr.m_paramete rs.push_back( (void*)(floatAr ray) );

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_paramete rs[ 0
]->m_type << endl;
}

Jul 10 '06 #1
2 1658
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<typena me T>
class TParameter {
public:
TParameter( const char *name ) : m_name( name ) {
m_type = typeid( T ).name();
}
~TParameter() {}
void insert( T arg ) {
m_data.push_bac k( (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<cons t char *, void * m_parameters;
};

int main()
{

TParameter<floa t*floatArray = new TParameter<floa t>( "floatArray "
);
TAttribute attr;
attr.m_paramete rs.push_back( pair<const char*, void*>( "float",
(void*)(floatAr ray) ) );

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_paramete rs[ 0 ].first << endl;

if ( attr.m_paramete rs[ 0 ].first == "float" ) {
TParameter<floa t*tparam = (TParameter<flo at*)(
attr.m_paramete rs[ 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.c om 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
3673
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
7021
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 lookup tables are not likely to change. Question 1: Should I include them in the backend (with rest of data) or the frontend?
7
1835
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 choose to change data providers). Therefore i thought to myself, a type of Adapter Pattern is best here and so i proceeded with that. here's an example of what i did (note this implementation differs from the text book one due to the way data...
59
4962
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 ASP.NET. At first glance, it looks excellent, albeit nothing that couldn't have been done to Classic ASP. I have been through a few tutorials and was impressed with how quickly you can get database info onto a page.
5
348
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 the struct, and populate them with data. 3) cin 1, 2, 3, or 4 from the user 4) If the user selected, say, 2, display the contents of the 2nd data
26
4086
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 kind of fun). As I go through the book, I seek to do all the exercises because they are very useful, and good, but it seems like I am just stumbling through somewhat. In particular, I don't really know how to think about "catching errors", or how...
17
3794
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, others are not likely. I wanted to open a discussion on what people think are the good and bad things about C++. Here are my lists: Good things about C++ --------------------- - multi-paradigm language. - const-specifications
11
1464
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. Then in my base class I for example have a pure virtual function called eg PerformAction. Then in each message class I implement PerformAction. The base class works out what the type of message is and then calls the correct inherited class...
23
3128
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 model: that most OO features are not available for class object design without loss of POD-ness. So, I'm more than leaning toward "bad" because of the limitations and
0
8352
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7981
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8222
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6632
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5723
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5396
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3846
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2367
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1194
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.