Core/ParameterList: Add support for color values and updated documentation

Former-commit-id: 87478d0e90a39ec24779ee9174e9f4e1a1f6d32b
This commit is contained in:
Lynix 2016-04-30 12:44:34 +02:00
parent d558ef6edc
commit 9aab369791
3 changed files with 209 additions and 120 deletions

View File

@ -93,6 +93,7 @@ namespace Nz
enum ParameterType
{
ParameterType_Boolean,
ParameterType_Color,
ParameterType_Float,
ParameterType_Integer,
ParameterType_None,

View File

@ -8,6 +8,7 @@
#define NAZARA_PARAMETERLIST_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Color.hpp>
#include <Nazara/Core/String.hpp>
#include <atomic>
#include <unordered_map>
@ -27,6 +28,7 @@ namespace Nz
void Clear();
bool GetBooleanParameter(const String& name, bool* value) const;
bool GetColorParameter(const String& name, Color* value) const;
bool GetFloatParameter(const String& name, float* value) const;
bool GetIntegerParameter(const String& name, int* value) const;
bool GetParameterType(const String& name, ParameterType* type) const;
@ -39,13 +41,14 @@ namespace Nz
void RemoveParameter(const String& name);
void SetParameter(const String& name);
void SetParameter(const String& name, const Color& value);
void SetParameter(const String& name, const String& value);
void SetParameter(const String& name, const char* value);
void SetParameter(const String& name, void* value);
void SetParameter(const String& name, void* value, Destructor destructor);
void SetParameter(const String& name, bool value);
void SetParameter(const String& name, float value);
void SetParameter(const String& name, int value);
void SetParameter(const String& name, void* value);
void SetParameter(const String& name, void* value, Destructor destructor);
ParameterList& operator=(const ParameterList& list);
ParameterList& operator=(ParameterList&&) = default;
@ -79,6 +82,7 @@ namespace Nz
float floatVal;
int intVal;
void* ptrVal;
Color colorVal;
String stringVal;
UserdataValue* userdataVal;
};

View File

@ -22,7 +22,6 @@ namespace Nz
/*!
* \brief Constructs a ParameterList object by copy
*/
ParameterList::ParameterList(const ParameterList& list)
{
operator=(list);
@ -31,16 +30,14 @@ namespace Nz
/*!
* \brief Destructs the object and clears
*/
ParameterList::~ParameterList()
{
Clear();
}
/*!
* \brief Clears the list of parameters
* \brief Clears all the parameters
*/
void ParameterList::Clear()
{
for (auto it = m_parameters.begin(); it != m_parameters.end(); ++it)
@ -50,17 +47,18 @@ namespace Nz
}
/*!
* \brief Gets a boolean parameter by name
* \return true if success
* \brief Gets a parameter as a boolean
* \return true if the parameter could be represented as a boolean
*
* \param name Name of the variable
* \param value Value to set
* \param name Name of the parameter
* \param value Pointer to a boolean to hold the retrieved value
*
* \remark Produces a NazaraAssert if pointer is invalid
* \remark Produces a silent NazaraError if name is not a variable
* \remark Produces a silent NazaraError if value could not be convertible
* \remark value must be a valid pointer
* \remark In case of failure, the variable pointed by value keep its value
* \remark If the parameter is not a boolean, a conversion will be performed, compatibles types are:
Integer: 0 is interpreted as false, any other value is interpreted as true
String: Conversion obeys the rule as described by String::ToBool
*/
bool ParameterList::GetBooleanParameter(const String& name, bool* value) const
{
NazaraAssert(value, "Invalid pointer");
@ -85,17 +83,17 @@ namespace Nz
return true;
case ParameterType_String:
{
bool converted;
if (it->second.value.stringVal.ToBool(&converted, String::CaseInsensitive))
{
bool converted;
if (it->second.value.stringVal.ToBool(&converted, String::CaseInsensitive))
{
*value = converted;
return true;
}
break;
*value = converted;
return true;
}
break;
}
case ParameterType_Float:
case ParameterType_None:
case ParameterType_Pointer:
@ -108,17 +106,62 @@ namespace Nz
}
/*!
* \brief Gets a float parameter by name
* \return true if success
* \brief Gets a parameter as a color
* \return true if the parameter could be represented as a color
*
* \param name Name of the variable
* \param value Value to set
* \param name Name of the parameter
* \param value Pointer to a color to hold the retrieved value
*
* \remark Produces a NazaraAssert if pointer is invalid
* \remark Produces a silent NazaraError if name is not a variable
* \remark Produces a silent NazaraError if value could not be convertible
* \remark value must be a valid pointer
* \remark In case of failure, the variable pointed by value keep its value
* \remark If the parameter is not a color, the function fails
*/
bool ParameterList::GetColorParameter(const String& name, Color* value) const
{
NazaraAssert(value, "Invalid pointer");
ErrorFlags flags(ErrorFlag_Silent | ErrorFlag_ThrowExceptionDisabled);
auto it = m_parameters.find(name);
if (it == m_parameters.end())
{
NazaraError("Parameter \"" + name + "\" is not present");
return false;
}
switch (it->second.type)
{
case ParameterType_Color:
*value = it->second.value.colorVal;
return true;
case ParameterType_Boolean:
case ParameterType_Integer:
case ParameterType_String:
case ParameterType_Float:
case ParameterType_None:
case ParameterType_Pointer:
case ParameterType_Userdata:
break;
}
NazaraError("Parameter value is not representable as a color");
return false;
}
/*!
* \brief Gets a parameter as a float
* \return true if the parameter could be represented as a float
*
* \param name Name of the parameter
* \param value Pointer to a float to hold the retrieved value
*
* \remark value must be a valid pointer
* \remark In case of failure, the variable pointed by value keep its value
* \remark If the parameter is not a float, a conversion will be performed, compatibles types are:
Integer: The integer value is converted to its float representation
String: Conversion obeys the rule as described by String::ToDouble
*/
bool ParameterList::GetFloatParameter(const String& name, float* value) const
{
NazaraAssert(value, "Invalid pointer");
@ -166,17 +209,19 @@ namespace Nz
}
/*!
* \brief Gets a integer parameter by name
* \return true if success
* \brief Gets a parameter as an integer
* \return true if the parameter could be represented as an integer
*
* \param name Name of the variable
* \param value Value to set
* \param name Name of the parameter
* \param value Pointer to an integer to hold the retrieved value
*
* \remark Produces a NazaraAssert if pointer is invalid
* \remark Produces a silent NazaraError if name is not a variable
* \remark Produces a silent NazaraError if value could not be convertible
* \remark value must be a valid pointer
* \remark In case of failure, the variable pointed by value keep its value
* \remark If the parameter is not a float, a conversion will be performed, compatibles types are:
Boolean: The boolean is represented as 1 if true and 0 if false
Float: The floating-point value is truncated and converted to a integer
String: Conversion obeys the rule as described by String::ToInteger but fails if the value could not be represented as a int
*/
bool ParameterList::GetIntegerParameter(const String& name, int* value) const
{
NazaraAssert(value, "Invalid pointer");
@ -229,15 +274,14 @@ namespace Nz
}
/*!
* \brief Gets a parameter type by name
* \return true if the parameter is present
* \brief Gets a parameter type
* \return true if the parameter is present, its type being written to type
*
* \param name Name of the variable
* \param type Pointer to a variable to hold result
* \param type Pointer to a variable to hold the result
*
* \remark Produces a NazaraAssert if type is invalid
* \remark type must be a valid pointer to a ParameterType variable
*/
bool ParameterList::GetParameterType(const String& name, ParameterType* type) const
{
NazaraAssert(type, "Invalid pointer");
@ -252,17 +296,17 @@ namespace Nz
}
/*!
* \brief Gets a pointer parameter by name
* \return true if success
* \brief Gets a parameter as a pointer
* \return true if the parameter could be represented as a pointer
*
* \param name Name of the variable
* \param value Value to set
* \param name Name of the parameter
* \param value Pointer to a pointer to hold the retrieved value
*
* \remark Produces a NazaraAssert if pointer is invalid
* \remark Produces a silent NazaraError if name is not a variable
* \remark Produces a silent NazaraError if value could not be convertible
* \remark value must be a valid pointer
* \remark In case of failure, the variable pointed by value keep its value
* \remark If the parameter is not a pointer, a conversion will be performed, compatibles types are:
Userdata: The pointer part of the userdata is returned
*/
bool ParameterList::GetPointerParameter(const String& name, void** value) const
{
NazaraAssert(value, "Invalid pointer");
@ -299,17 +343,23 @@ namespace Nz
}
/*!
* \brief Gets a string parameter by name
* \return true if success
* \brief Gets a parameter as a string
* \return true if the parameter could be represented as a string
*
* \param name Name of the variable
* \param value Value to set
* \param name Name of the parameter
* \param value Pointer to a pointer to hold the retrieved value
*
* \remark Produces a NazaraAssert if pointer is invalid
* \remark Produces a silent NazaraError if name is not a variable
* \remark Produces a silent NazaraError if value could not be convertible
* \remark value must be a valid pointer
* \remark In case of failure, the variable pointed by value keep its value
* \remark If the parameter is not a string, a conversion will be performed, all types are compatibles:
Boolean: Conversion obeys the rules of String::Boolean
Color: Conversion obeys the rules of Color::ToString
Float: Conversion obeys the rules of String::Number
Integer: Conversion obeys the rules of String::Number
None: An empty string is returned
Pointer: Conversion obeys the rules of String::Pointer
Userdata: Conversion obeys the rules of String::Pointer
*/
bool ParameterList::GetStringParameter(const String& name, String* value) const
{
NazaraAssert(value, "Invalid pointer");
@ -329,6 +379,10 @@ namespace Nz
*value = String::Boolean(it->second.value.boolVal);
return true;
case ParameterType_Color:
*value = it->second.value.colorVal.ToString();
return true;
case ParameterType_Float:
*value = String::Number(it->second.value.floatVal);
return true;
@ -359,17 +413,18 @@ namespace Nz
}
/*!
* \brief Gets a user parameter by name
* \return true if success
* \brief Gets a parameter as an userdata
* \return true if the parameter could be represented as a userdata
*
* \param name Name of the variable
* \param value Value to set
* \param name Name of the parameter
* \param value Pointer to a pointer to hold the retrieved value
*
* \remark Produces a NazaraAssert if pointer is invalid
* \remark Produces a silent NazaraError if name is not a variable
* \remark Produces a silent NazaraError if value could not be convertible
* \remark value must be a valid pointer
* \remark In case of failure, the variable pointed by value keep its value
* \remark If the parameter is not an userdata, the function fails
*
* \see GetPointerParameter
*/
bool ParameterList::GetUserdataParameter(const String& name, void** value) const
{
NazaraAssert(value, "Invalid pointer");
@ -396,25 +451,24 @@ namespace Nz
}
/*!
* \brief Checks whether the parameter list has a parameter with that name
* \brief Checks whether the parameter list contains a parameter named `name`
* \return true if found
*
* \param name Name of the parameter
*/
bool ParameterList::HasParameter(const String& name) const
{
return m_parameters.find(name) != m_parameters.end();
}
/*!
* \brief Removes the parameter with that name
* \brief Removes the parameter named `name`
*
* Removes the parameter with that name, if not found, nothing is done
* Search for a parameter named `name` and remove it from the parameter list, freeing up its memory
* Nothing is done if the parameter is not present in the parameter list
*
* \param name Name of the parameter
*/
void ParameterList::RemoveParameter(const String& name)
{
auto it = m_parameters.find(name);
@ -426,11 +480,12 @@ namespace Nz
}
/*!
* \brief Sets the parameter with the name to ParameterType_None
* \brief Sets a null parameter named `name`
*
* If a parameter already exists with that name, it is destroyed and replaced by this call
*
* \param name Name of the parameter
*/
void ParameterList::SetParameter(const String& name)
{
Parameter& parameter = CreateValue(name);
@ -438,12 +493,29 @@ namespace Nz
}
/*!
* \brief Sets the parameter with the name to the value
* \brief Sets a color parameter named `name`
*
* If a parameter already exists with that name, it is destroyed and replaced by this call
*
* \param name Name of the parameter
* \param value Value of the parameter
* \param value The color value
*/
void ParameterList::SetParameter(const String& name, const Color& value)
{
Parameter& parameter = CreateValue(name);
parameter.type = ParameterType_Color;
PlacementNew(&parameter.value.colorVal, value);
}
/*!
* \brief Sets a string parameter named `name`
*
* If a parameter already exists with that name, it is destroyed and replaced by this call
*
* \param name Name of the parameter
* \param value The string value
*/
void ParameterList::SetParameter(const String& name, const String& value)
{
Parameter& parameter = CreateValue(name);
@ -453,12 +525,13 @@ namespace Nz
}
/*!
* \brief Sets the parameter with the name to the value
* \brief Sets a string parameter named `name`
*
* If a parameter already exists with that name, it is destroyed and replaced by this call
*
* \param name Name of the parameter
* \param value Value of the parameter
* \param value The string value
*/
void ParameterList::SetParameter(const String& name, const char* value)
{
Parameter& parameter = CreateValue(name);
@ -468,41 +541,13 @@ namespace Nz
}
/*!
* \brief Sets the parameter with the name to the value
* \brief Sets a boolean parameter named `name`
*
* If a parameter already exists with that name, it is destroyed and replaced by this call
*
* \param name Name of the parameter
* \param value Value of the parameter
* \param value The boolean value
*/
void ParameterList::SetParameter(const String& name, void* value)
{
Parameter& parameter = CreateValue(name);
parameter.type = ParameterType_Pointer;
parameter.value.ptrVal = value;
}
/*!
* \brief Sets the parameter with the name to the value
*
* \param name Name of the parameter
* \param value Value of the parameter
* \param destructor Destructor for dynamic variable
*/
void ParameterList::SetParameter(const String& name, void* value, Destructor destructor)
{
Parameter& parameter = CreateValue(name);
parameter.type = ParameterType_Userdata;
parameter.value.userdataVal = new Parameter::UserdataValue(destructor, value);
}
/*!
* \brief Sets the parameter with the name to the value
*
* \param name Name of the parameter
* \param value Value of the parameter
*/
void ParameterList::SetParameter(const String& name, bool value)
{
Parameter& parameter = CreateValue(name);
@ -511,12 +556,13 @@ namespace Nz
}
/*!
* \brief Sets the parameter with the name to the value
* \brief Sets a float parameter named `name`
*
* If a parameter already exists with that name, it is destroyed and replaced by this call
*
* \param name Name of the parameter
* \param value Value of the parameter
* \param value The float value
*/
void ParameterList::SetParameter(const String& name, float value)
{
Parameter& parameter = CreateValue(name);
@ -525,12 +571,13 @@ namespace Nz
}
/*!
* \brief Sets the parameter with the name to the value
* \brief Sets an integer parameter named `name`
*
* If a parameter already exists with that name, it is destroyed and replaced by this call
*
* \param name Name of the parameter
* \param value Value of the parameter
* \param value The integer value
*/
void ParameterList::SetParameter(const String& name, int value)
{
Parameter& parameter = CreateValue(name);
@ -539,12 +586,48 @@ namespace Nz
}
/*!
* \brief Assigns the content of the other parameter list to this
* \brief Sets a pointer parameter named `name`
*
* If a parameter already exists with that name, it is destroyed and replaced by this call
*
* \param name Name of the parameter
* \param value The pointer value
*
* \remark This sets a raw pointer, this class takes no responsibility toward it,
if you wish to destroy the pointed variable along with the parameter list, you should set a userdata
*/
void ParameterList::SetParameter(const String& name, void* value)
{
Parameter& parameter = CreateValue(name);
parameter.type = ParameterType_Pointer;
parameter.value.ptrVal = value;
}
/*!
* \brief Sets a userdata parameter named `name`
*
* If a parameter already exists with that name, it is destroyed and replaced by this call
*
* \param name Name of the parameter
* \param value The pointer value
* \param destructor The destructor function to be called upon parameter suppression
*
* \remark The destructor is called once when all copies of the userdata are destroyed, which means
you can safely copy the parameter list around.
*/
void ParameterList::SetParameter(const String& name, void* value, Destructor destructor)
{
Parameter& parameter = CreateValue(name);
parameter.type = ParameterType_Userdata;
parameter.value.userdataVal = new Parameter::UserdataValue(destructor, value);
}
/*!
* \brief Copies the content of the other parameter list to this
* \return A reference to this
*
* \param list List to assign
*/
ParameterList& ParameterList::operator=(const ParameterList& list)
{
Clear();
@ -627,6 +710,7 @@ namespace Nz
}
case ParameterType_Boolean:
case ParameterType_Color:
case ParameterType_Float:
case ParameterType_Integer:
case ParameterType_None: