template help

C

Christopher

My goal is to make maps where the key is a string name and the value
is a pair of data, one being a value, and one being a bool telling me
whether that value has been initialized to something meaningful.


How do I change these existing maps to take the AttributeValue struct
in the private section with T being a specific data type?



//----------------------------------------------------------------------------
/**
* Material
*
* Dynamic container of those variables relating to an effect.
*
* A Material first has to be created by an effect in order to allocate
the differant
* types of attributes the effect uses. It can then be set and given
back to the
* effect to set the values of the effect's variables.
*/
class Material
{
public:

/**
* Constructor
*/
Material();

/**
* Deconstructor
*/
~Material();


//-----
// Matrix type
//-----

/**
* Sets a matrix attribute
*
* If the attribute does not exist, it creates the attribute
*/
void SetMatrix(const std::string & name, const D3DXMATRIX & value);

/**
* Gets a matrix attribute
**/
const D3DXMATRIX & GetMatrix(const std::string & name) const;


//-----
// Float type
//-----

/**
* Sets a float attribute
*
* If the attribute does not exist, it creates the attribute
*/
void SetFloat(const std::string & name, const float value);

/**
* Gets a float attribute
**/
const float GetFloat(const std::string & name) const;


//-----
// Texture type
//-----

/**
* Sets a texture attribute
*
* If the attribute does not exist, it creates the attribute
*/
void SetTexture(const std::string & variableName, const std::string
& textureName);

/**
* Gets a texture attribute
*/
const std::string GetTexture(const std::string & variableName)
const;





private:

template <class T>
struct AttributeValue
{
bool m_initialized;
T m_value;
};

///////// HELP NEEDED HERE ////////////
/**
* Map of the matrix variables
*
* key - matrix variable name as it appears in the DirectX effect
* value - matrix
*/
typedef std::map<std::string, D3DXMATRIX> Matrices;
Matrices m_matrices;

/**
* Map of the single float variables
*
* key - float variable name as it appears in the DirectX effect
* value - float
*/
typedef std::map<std::string, float> Floats;
Floats m_floats;

/**
* Map of the texture variables
*
* key - texture variable name as it appears in the DirectX effect
* value - texture name as it appears in the texture manager
*/
typedef std::map<std::string, std::string> Textures;
Textures m_textures;


};

#endif
 
T

Thomas J. Gritzan

Christopher said:
My goal is to make maps where the key is a string name and the value
is a pair of data, one being a value, and one being a bool telling me
whether that value has been initialized to something meaningful.

What do you need the bool for?
Just let uninitialized key-value pairs be non-existant.

You can check for existing keys using the find member function. If
find() returns end(), the key is not in the map.
How do I change these existing maps to take the AttributeValue struct
in the private section with T being a specific data type?

Did you try
std::map<std::string, AttributeValue<D3DXMATRIX> > and
std::map<std::string, AttributeValue<float> > as types?

[...]
private:

template <class T>
struct AttributeValue
{
bool m_initialized;
T m_value;
}; [...]
typedef std::map<std::string, D3DXMATRIX> Matrices;
Matrices m_matrices; [...]
typedef std::map<std::string, float> Floats;
Floats m_floats; [...]
typedef std::map<std::string, std::string> Textures;
Textures m_textures;
 
C

Christopher

What do you need the bool for?
Just let uninitialized key-value pairs be non-existant.

You can check for existing keys using the find member function. If
find() returns end(), the key is not in the map.

Thomas- Hide quoted text -

- Show quoted text -

Figured someone would ask that. I didn't want to go into the long
explanation.

Short version:
I need to be able to set keys with uninitialized data. Because those
keys are places holders for the data to be set elsewhere.

Long version:

I have an effect class that parses any number of variables and types
from a DirectX effect object. Those variables need to be set before
the effect is applied. My material class holds those variables and
serves as a seperate container for all the variables that can be set.
So, when an effect is created, it in turn creates a material
containing all the variables that must be set outside my engine down
the road. The material is the only thing I want to expose to the
outside in this system. In summary, I need to create a container of
uninitialized variables, which can have those varibales set to
something later, and be able to tell the difference internally.
Because of the nature of some of these variables, there won't be a way
to tell the difference between a placeholder matrix and a matrix that
was indeed set, so I want to provide that. i.e you can't have a NULL
value or a -1 or anything else like that.
 
T

Thomas J. Gritzan

Christopher said:
Figured someone would ask that. I didn't want to go into the long
explanation.

Short version:
I need to be able to set keys with uninitialized data. Because those
keys are places holders for the data to be set elsewhere.

Try Boost.Optional.
The advantage is: Non-initialized value inside a boost::eek:ptional<T> is
not even default constructed.

And think about a different design :)
 
C

Christopher

Christopher schrieb:








Try Boost.Optional.
The advantage is: Non-initialized value inside a boost::eek:ptional<T> is
not even default constructed.

And think about a different design :)

I've thought of every possible desing I could for 3 months. Wish I
could post some UML to show the details of the problem. I don't see a
way around doing something rather ugly given the interface microsoft
provided, while keeping my requirements.
 
C

Chinson

I've thought of every possible desing I could for 3 months. Wish I
could post some UML to show the details of the problem. I don't see a
way around doing something rather ugly given the interface microsoft
provided, while keeping my requirements.- Hide quoted text -

- Show quoted text -

The following code may be a reference:
///////////////////in Material class declaration ///////////////////
class Base { };

template <class T>
class AttributeValue : public Base
{
bool m_initialized;
T m_value;
};

struct lessStr
{
bool operator ()( const std::string& str1, const std::string&
str2) const
{ return str1.compare(str2) < 0; }
};

typedef std::map<std::string, Base*,lessStr> IMap;
IMap m_variables;

///////////////////calling example///////////////////
Material mater;
Material ::Base* p_float_variable = new
Material::AttributeValue<float>;
mater.m_variables["test1"] = p_float_variable;

Material::Base* p_double_variable = new
Material::AttributeValue<double>;
mater.m_variables["test2"] = p_double_variable;

//Todo...

delete p_float_variable, p_float_variable = 0;
delete p_double_variable, p_double_variable = 0;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,779
Messages
2,569,606
Members
45,239
Latest member
Alex Young

Latest Threads

Top