473,385 Members | 1,834 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,385 software developers and data experts.

Parameter List / Parameter Block / Anything patterns...

Hi everyone

I need to come up with an efficient solution to add/remove/access
parameters to an object. Lets say I create an object "Cart" and at run
time based on the user input, lets say we add to this "Cart" object 3
Oranges 4 Apples and 1 Salad (stupid example I know). Whenever we do
something with this cast object we want to be able to have access to
all the data that it holds (3 oranges, 4 apples, 1 salad).

in pseudo code it would do something like that

Cart *cart = new Cart;
Orange orange[4] = { orange1, orange2, orange3, orange4 };
Cart.SetParameter( "SomeOrange", orange ); // copy the 4 orange in
memory
Orange *iterOrange = Cart.GetParameter( "SomeOrange" );
for ( unsigned i = 0; i < 4; ++i )
Orange *o = iterOrange[i];

To get something like that to work the Cart class would have a member
variable that would look like that.

std::map<const char *, Datadict;

So a unique name is associated with each data, such that we can look
for that parameter name later on when we call GetParameter.

I looked a little bit on the net and found a few documents on C++
patterns that seem to do what I need. They seem to be called Parameter
List, Parameter Block or Anything Patterns. But I have a hard time
making to work or find a good implementation example. It sounds like
some API are using these patterns. I have heard of the 3DMaya API &
the nVidia Gelato API. It is used for these 3D renderers because it
allows to add any data of any type at run time for example to lets say
geometric primitives (and I want to use it for that purpose).

I am not sure what is the best way of doing that so was wondering if
someone knows a good implementation of these patterns or could put me
on the right track to write my own. I thought of simply doing
something like that :

enum ParamType { kFloat, kInt, kString };

class Param
{
public:
ParamType type;
std::vector<floatfdefault;
std::vector<const char *sdefault;
std::vector<intidefault;
Param() {}
};

std::map<const char *, ParamparamList;

but if the param is a float array i carry in the Param object useless
data like sdefault & idefault. Therefore Param is "heavier" than it
should. I am not sure this is very elegant (although simple).

I thought of having some sort of Data class that would hold a void*
variable pointer to an array of data. But of course using std::vector
is better since having a void * data type of situation makes have to
deal with allocating/deallocating memory.

class Data
{
public:
void *data;
Data( ParamType type, void *d, unsigned n )
{
switch( type )
case kFloat: data = new float[n];
memcpy(data,d,sizoef(float)*n); break;
case kInt: data = new int[n]; memcpy(data, d, sizeof(int)*n);
break;
case kString: /* etc */
}
~Data() { /* release mem */ }
};

Could anybody advise me of the best way of doing this please (in terms
of code robustness, simplicity of use, better c++ coding, memory &
speed efficiency, etc).

Thanks a lot -mark

Mar 29 '07 #1
4 1665
On 29 Mar, 12:39, "mast...@yahoo.com" <mast...@yahoo.comwrote:
Hi everyone

I need to come up with an efficient solution to add/remove/access
parameters to an object. Lets say I create an object "Cart" and at run
time based on the user input, lets say we add to this "Cart" object 3
Oranges 4 Apples and 1 Salad (stupid example I know). Whenever we do
something with this cast object we want to be able to have access to
all the data that it holds (3 oranges, 4 apples, 1 salad).

in pseudo code it would do something like that

Cart *cart = new Cart;
Orange orange[4] = { orange1, orange2, orange3, orange4 };
Cart.SetParameter( "SomeOrange", orange ); // copy the 4 orange in
memory
Orange *iterOrange = Cart.GetParameter( "SomeOrange" );
for ( unsigned i = 0; i < 4; ++i )
Orange *o = iterOrange[i];

To get something like that to work the Cart class would have a member
variable that would look like that.

std::map<const char *, Datadict;

So a unique name is associated with each data, such that we can look
for that parameter name later on when we call GetParameter.

I looked a little bit on the net and found a few documents on C++
patterns that seem to do what I need. They seem to be called Parameter
List, Parameter Block or Anything Patterns. But I have a hard time
making to work or find a good implementation example. It sounds like
some API are using these patterns. I have heard of the 3DMaya API &
the nVidia Gelato API. It is used for these 3D renderers because it
allows to add any data of any type at run time for example to lets say
geometric primitives (and I want to use it for that purpose).

I am not sure what is the best way of doing that so was wondering if
someone knows a good implementation of these patterns or could put me
on the right track to write my own. I thought of simply doing
something like that :

enum ParamType { kFloat, kInt, kString };

class Param
{
public:
ParamType type;
std::vector<floatfdefault;
std::vector<const char *sdefault;
std::vector<intidefault;
Param() {}

};

std::map<const char *, ParamparamList;

but if the param is a float array i carry in the Param object useless
data like sdefault & idefault. Therefore Param is "heavier" than it
should. I am not sure this is very elegant (although simple).

I thought of having some sort of Data class that would hold a void*
variable pointer to an array of data. But of course using std::vector
is better since having a void * data type of situation makes have to
deal with allocating/deallocating memory.

class Data
{
public:
void *data;
Data( ParamType type, void *d, unsigned n )
{
switch( type )
case kFloat: data = new float[n];
memcpy(data,d,sizoef(float)*n); break;
case kInt: data = new int[n]; memcpy(data, d, sizeof(int)*n);
break;
case kString: /* etc */
}
~Data() { /* release mem */ }

};

Could anybody advise me of the best way of doing this please (in terms
of code robustness, simplicity of use, better c++ coding, memory &
speed efficiency, etc).
If you have a limited set of items that can be placed in the Cart and
if they all have some operations in common it would probably be
easiest to simply let them inherit from some base-type. Failing that
you need a heterogeneous container, it's covered slightly in the FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-34.4 and you
can find some implementations by googling, Boost.Any is one such
container: http://www.boost.org/doc/html/any.html

--
Erik Wikström

Mar 29 '07 #2
On Mar 29, 11:19 pm, "Erik Wikström" <eri...@student.chalmers.se>
wrote:
On 29 Mar, 12:39, "mast...@yahoo.com" <mast...@yahoo.comwrote:
Hi everyone
I need to come up with an efficient solution to add/remove/access
parameters to an object. Lets say I create an object "Cart" and at run
time based on the user input, lets say we add to this "Cart" object 3
Oranges 4 Apples and 1 Salad (stupid example I know). Whenever we do
something with this cast object we want to be able to have access to
all the data that it holds (3 oranges, 4 apples, 1 salad).
in pseudo code it would do something like that
Cart *cart = new Cart;
Orange orange[4] = { orange1, orange2, orange3, orange4 };
Cart.SetParameter( "SomeOrange", orange ); // copy the 4 orange in
memory
Orange *iterOrange = Cart.GetParameter( "SomeOrange" );
for ( unsigned i = 0; i < 4; ++i )
Orange *o = iterOrange[i];
To get something like that to work the Cart class would have a member
variable that would look like that.
std::map<const char *, Datadict;
So a unique name is associated with each data, such that we can look
for that parameter name later on when we call GetParameter.
I looked a little bit on the net and found a few documents on C++
patterns that seem to do what I need. They seem to be called Parameter
List, Parameter Block or Anything Patterns. But I have a hard time
making to work or find a good implementation example. It sounds like
some API are using these patterns. I have heard of the 3DMaya API &
the nVidia Gelato API. It is used for these 3D renderers because it
allows to add any data of any type at run time for example to lets say
geometric primitives (and I want to use it for that purpose).
I am not sure what is the best way of doing that so was wondering if
someone knows a good implementation of these patterns or could put me
on the right track to write my own. I thought of simply doing
something like that :
enum ParamType { kFloat, kInt, kString };
class Param
{
public:
ParamType type;
std::vector<floatfdefault;
std::vector<const char *sdefault;
std::vector<intidefault;
Param() {}
};
std::map<const char *, ParamparamList;
but if the param is a float array i carry in the Param object useless
data like sdefault & idefault. Therefore Param is "heavier" than it
should. I am not sure this is very elegant (although simple).
I thought of having some sort of Data class that would hold a void*
variable pointer to an array of data. But of course using std::vector
is better since having a void * data type of situation makes have to
deal with allocating/deallocating memory.
class Data
{
public:
void *data;
Data( ParamType type, void *d, unsigned n )
{
switch( type )
case kFloat: data = new float[n];
memcpy(data,d,sizoef(float)*n); break;
case kInt: data = new int[n]; memcpy(data, d, sizeof(int)*n);
break;
case kString: /* etc */
}
~Data() { /* release mem */ }
};
Could anybody advise me of the best way of doing this please (in terms
of code robustness, simplicity of use, better c++ coding, memory &
speed efficiency, etc).

If you have a limited set of items that can be placed in the Cart and
if they all have some operations in common it would probably be
easiest to simply let them inherit from some base-type.
Thanks Erik for your answer. They do have comment operations but are
quite different still I think. For example float, integers, Points,
Vectors, Normals, Matrices, etc are all numbers and so all have
arithmetic opertations (+ - / ... )
I think in the code I develop at the moment I do have 3 basic types,
int, float & string.

Mar 29 '07 #3
Boost.Any is one such
container:http://www.boost.org/doc/html/any.html
I just looked at boost::any and it looks like what I need. Is there
any restrictions as to why it might not be a good idea to use it ?
Otherwise it sounds like the way to go isn't it ?

Mar 29 '07 #4
On Mar 29, 2:19 pm, "mast...@yahoo.com" <mast...@yahoo.comwrote:
Boost.Any is one such
container:http://www.boost.org/doc/html/any.html

I just looked at boost::any and it looks like what I need. Is there
any restrictions as to why it might not be a good idea to use it ?
Otherwise it sounds like the way to go isn't it ?
If you have a known set of types (i.e. just
int,float,string,vector,matrix,etc), you may wish to use
boost::variant instead of boost::any. Boost::variant does not perform
any memory allocation (which boost::any must do behind the scenes) and
thus has less overhead. It also has a nice visitor interface which
lets you actually operate on the data without having to check its
type.

However, if you're writing a library, and you just want users to be
able to add and remove any type of data, then boost::any is the way to
go.

Be warned - both any and variant can produce a disheartening amount of
compiler error output if you make a mistake.
-matt

Mar 29 '07 #5

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

Similar topics

49
by: Mark Hahn | last post by:
As we are addressing the "warts" in Python to be fixed in Prothon, we have come upon the mutable default parameter problem. For those unfamiliar with the problem, it can be seen in this Prothon...
6
by: Eric | last post by:
Pythonistas, I seem at a loss for a List Comprehension syntax that will do what I want. I have a list of string position spans: >>> breaks the first pair representing: someString
3
by: David Ross | last post by:
I sometimes place a sidebar on a Web page, using the following: ..sideright { float: right; background-color: #fff; width: 40%; font-size: 90%; text-align: justify; margin-left: 1em;...
6
by: grawsha2000 | last post by:
Hi, I'm planing to start using (MSADB) as my data-tier database communiction means. Do you recommond such tool? MTIA, Grawsha
5
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
1
by: Kenny | last post by:
I'm looking at GUI standards and designs patterns for an upcoming project. I have read the MS Patterns and Practices for the User Interface Process Application Block for .NET block. Everything...
15
by: main() | last post by:
Hi all, When i compile following piece of code, # include <stdio.h> void fun(int val) { int val; /*problem is here*/ printf("%d\n",val);
12
by: ArunDhaJ | last post by:
Hi Friends, Is it possible to pass a table as a parameter to a funtion. whos function declaration would look some thing like this.... ALTER FUNCTION TempFunction (@TempTable TABLE, @nPId INT) ...
9
by: tadmill | last post by:
Is it possible to pass a generic parameter of the same class to to its constructor, where the "T" type passed in the constructor is different than the "T" type of the instanced class? ie, ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.