[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

dynamcially created data structure

Christopher

10/9/2008 7:04:00 PM

Is it possible to dynamically create a data structure?

Not that it matters, but just for understanding, I am working with
video shaders, these shaders use thier own language (HLSL) which you
compile at runtime and run for rendering. The HLSL can contain many
differant types of variables and expect your application to set them.

The library provides the ability to query for
number of variables
thier names
thier types
and provides a method for setting them that is depedent on the type

My goal is to package them, no matter what they are, into a single
material class, that can be used no matter what the HLSL it
corresponds to looks like.

So, I want the same data structure that is capable of setting, for
example, two floats in one case, a float and a string in another case,
7 floats and an array of UDTs in another case, etc.


My first thought was to create a class that had a one map for every
possible type, but I am not sure that is the best.

I was also thinking of making seperate "Material Property" classes
that could be added and removed from a "Material", but its fuzzy.

Any ideas?

3 Answers

Victor Bazarov

10/9/2008 7:28:00 PM

0

Christopher wrote:
> Is it possible to dynamically create a data structure?

Not really. You can implement your own mechanism for type information
and then use it, but C++ is *statically typed* language, which means
that all types participating in the program are known at the compile
time. The only "exception" to that is the dynamic binding mechanism
that allows you to supply a pointer to the base class and expect the
derived class' virtual function to be called. But you already know
about this one.

> Not that it matters, but just for understanding, I am working with
> video shaders, these shaders use thier own language (HLSL) which you
> compile at runtime and run for rendering. The HLSL can contain many
> differant types of variables and expect your application to set them.
>
> The library provides the ability to query for
> number of variables
> thier names
> thier types
> and provides a method for setting them that is depedent on the type
>
> My goal is to package them, no matter what they are, into a single
> material class, that can be used no matter what the HLSL it
> corresponds to looks like.
>
> So, I want the same data structure that is capable of setting, for
> example, two floats in one case, a float and a string in another case,
> 7 floats and an array of UDTs in another case, etc.
>
>
> My first thought was to create a class that had a one map for every
> possible type, but I am not sure that is the best.
>
> I was also thinking of making seperate "Material Property" classes
> that could be added and removed from a "Material", but its fuzzy.
>
> Any ideas?

I suspect that something like that has already been done. Perhaps
asking in 'comp.graphics.algorithms' or searching on the Web might help.
What you're describing looks essentially like a system for managing
types in a compiler or an interpreter. There are plenty of examples of
those on the Web, I'm certain of it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Ian Collins

10/9/2008 11:00:00 PM

0

Christopher wrote:
> Is it possible to dynamically create a data structure?
>
> Not that it matters, but just for understanding, I am working with
> video shaders, these shaders use thier own language (HLSL) which you
> compile at runtime and run for rendering. The HLSL can contain many
> differant types of variables and expect your application to set them.
>
> The library provides the ability to query for
> number of variables
> thier names
> thier types
> and provides a method for setting them that is depedent on the type
>
> My goal is to package them, no matter what they are, into a single
> material class, that can be used no matter what the HLSL it
> corresponds to looks like.
>
> So, I want the same data structure that is capable of setting, for
> example, two floats in one case, a float and a string in another case,
> 7 floats and an array of UDTs in another case, etc.
>
Assuming you have a fixed set of operations, you could look at wrapping
the base types in a container class with a fixed set of operators and
working with those.

> My first thought was to create a class that had a one map for every
> possible type, but I am not sure that is the best.
>
> I was also thinking of making seperate "Material Property" classes
> that could be added and removed from a "Material", but its fuzzy.
>
That sounds a bit like my suggestion, investigate it further.

--
Ian Collins

pjb

10/10/2008 12:05:00 PM

0

Christopher <cpisz@austin.rr.com> writes:

> Is it possible to dynamically create a data structure?

Yes.


> Not that it matters, but just for understanding, I am working with
> video shaders, these shaders use thier own language (HLSL) which you
> compile at runtime and run for rendering. The HLSL can contain many
> differant types of variables and expect your application to set them.
>
> The library provides the ability to query for
> number of variables
> thier names
> thier types
> and provides a method for setting them that is depedent on the type

Then you already have it here. What other data structure do you want,
beyond what that library provides?


> My goal is to package them, no matter what they are, into a single
> material class, that can be used no matter what the HLSL it
> corresponds to looks like.

Ok so you want to write some wrapper, to be able to use it abstractly,
because that library doesn't already provide the abstract layer.


> So, I want the same data structure that is capable of setting, for
> example, two floats in one case, a float and a string in another case,
> 7 floats and an array of UDTs in another case, etc.

Well, having a single abstract class for the interface doesn't remove
that it will be simplier to treat each of the case in a different
concrete subclass, so the implementation of your wrapper won't be a
single class.

> My first thought was to create a class that had a one map for every
> possible type, but I am not sure that is the best.
>
> I was also thinking of making seperate "Material Property" classes
> that could be added and removed from a "Material", but its fuzzy.
>
> Any ideas?


So here is your single abstract data structure capable of setting:

class Data{
virtual void setHLSLData(hslshandle*)=0;
};


Now you can define subclasses for each of the C++ data types you need
to set.

class IntData{
protected: int value;
public: IntData(int aValue):value(aValue){};
public: virtual void setHLSLData(hslshandle* hslsdata){
if(hslsType(hslsdata)==hslsInteger){
hslsSetInteger(hslsdata,value);
}else{
throw TypeError("Cannot assign an int to a "+hslsTypeName(hslsdata));;
}
}
};

and so on for the other data types. You can also use templates to
define a bunch of then.

If you have structures or vectors, you can easily define a subclass of
Data with a map of fieldName to Data items, or a vector of Data items,
etc.

--
__Pascal Bourguignon__