Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Parameter List / Parameter Block / Anything patterns...

Reply
Thread Tools

Parameter List / Parameter Block / Anything patterns...

 
 
mast2as@yahoo.com
Guest
Posts: n/a
 
      03-29-2007
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 *, Data> dict;

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<float> fdefault;
std::vector<const char *> sdefault;
std::vector<int> idefault;
Param() {}
};

std::map<const char *, Param> paramList;

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

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      03-29-2007
On 29 Mar, 12:39, "(E-Mail Removed)" <(E-Mail Removed)> wrote:
> 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 *, Data> dict;
>
> 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<float> fdefault;
> std::vector<const char *> sdefault;
> std::vector<int> idefault;
> Param() {}
>
> };
>
> std::map<const char *, Param> paramList;
>
> 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

 
Reply With Quote
 
 
 
 
mast2as@yahoo.com
Guest
Posts: n/a
 
      03-29-2007
On Mar 29, 11:19 pm, "Erik Wikström" <(E-Mail Removed)>
wrote:
> On 29 Mar, 12:39, "(E-Mail Removed)" <(E-Mail Removed)> wrote:
>
>
>
> > 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 *, Data> dict;

>
> > 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<float> fdefault;
> > std::vector<const char *> sdefault;
> > std::vector<int> idefault;
> > Param() {}

>
> > };

>
> > std::map<const char *, Param> paramList;

>
> > 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.

 
Reply With Quote
 
mast2as@yahoo.com
Guest
Posts: n/a
 
      03-29-2007
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 ?

 
Reply With Quote
 
Matteo
Guest
Posts: n/a
 
      03-29-2007
On Mar 29, 2:19 pm, "(E-Mail Removed)" <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there anything like Array#Index that takes a block? Lee Griffiths Ruby 8 02-13-2011 01:38 AM
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM
Problem with enterprise application block - data block Showjumper ASP .Net 1 03-19-2005 03:48 PM
Block DIV within a block DIV? Noozer HTML 3 01-06-2005 10:24 PM



Advertisments