Skip to content

Commit

Permalink
Improve attribute class
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed May 9, 2024
1 parent c2c8e28 commit 7d7b613
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 159 deletions.
130 changes: 44 additions & 86 deletions source/core/inc/tactile/core/meta/attribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "tactile/base/container/variant.hpp"
#include "tactile/base/id.hpp"
#include "tactile/base/prelude.hpp"
#include "tactile/core/debug/assert.hpp"
#include "tactile/core/debug/exception.hpp"
#include "tactile/core/meta/attribute_type.hpp"
#include "tactile/core/meta/color.hpp"
Expand All @@ -34,8 +33,11 @@ concept AttributeValueType = std::same_as<T, bool> || //
std::convertible_to<T, Path> || //
std::convertible_to<T, ObjectRef>;

/** Represents a generic value of one of several possible types. */
class Attribute final {
/**
* Represents a generic value of one of several possible types.
*/
class Attribute final
{
// These are indices into the value type variant
inline static constexpr usize kStringTypeIndex = 0;
inline static constexpr usize kIntTypeIndex = 1;
Expand Down Expand Up @@ -80,23 +82,25 @@ class Attribute final {
path_type,
objref_type>;

/** Creates an empty string attribute. */
/**
* Creates an empty string attribute.
*/
Attribute() = default;

/**
* Creates an attribute with the default value of the specified type.
*
* \param type The underlying value type.
*/
explicit Attribute(const AttributeType type) { reset(type); }
explicit Attribute(AttributeType type);

/**
* Creates an attribute with a specific value.
*
* \param value The initial value.
*/
template <AttributeValueType T>
Attribute(T value)
explicit Attribute(T value)
{
mValue.emplace<T>(std::move(value));
}
Expand All @@ -108,11 +112,7 @@ class Attribute final {
*
* \param str A C-style string.
*/
Attribute(const char* str)
{
TACTILE_ASSERT(str);
mValue.emplace<string_type>(str);
}
explicit Attribute(const char* str);

/**
* Resets the attribute the default value for the specified type.
Expand All @@ -139,199 +139,156 @@ class Attribute final {
*
* \param str The new string value.
*/
void set(const char* str)
{
TACTILE_ASSERT(str);
mValue.emplace<string_type>(str);
}
void set(const char* str);

/**
* Returns the underlying string value.
*
* \return
* The underlying value.
* The underlying value.
*
* \throws Exception if the attribute is of a different type.
*/
[[nodiscard]]
auto as_string() const -> const string_type&
{
return _get<string_type>();
}
auto as_string() const -> const string_type&;

/**
* Returns the underlying int value.
*
* \return
* The underlying value.
* The underlying value.
*
* \throws Exception if the attribute is of a different type.
*/
[[nodiscard]]
auto as_int() const -> int_type
{
return _get<int_type>();
}
auto as_int() const -> int_type;

/**
* Returns the underlying 2D int vector value.
*
* \return
* The underlying value.
* The underlying value.
*
* \throws Exception if the attribute is of a different type.
*/
[[nodiscard]]
auto as_int2() const -> const int2_type&
{
return _get<int2_type>();
}
auto as_int2() const -> const int2_type&;

/**
* Returns the underlying 3D int vector value.
*
* \return
* The underlying value.
* The underlying value.
*
* \throws Exception if the attribute is of a different type.
*/
[[nodiscard]]
auto as_int3() const -> const int3_type&
{
return _get<int3_type>();
}
auto as_int3() const -> const int3_type&;

/**
* Returns the underlying 4D int vector value.
*
* \return
* The underlying value.
* The underlying value.
*
* \throws Exception if the attribute is of a different type.
*/
[[nodiscard]]
auto as_int4() const -> const int4_type&
{
return _get<int4_type>();
}
auto as_int4() const -> const int4_type&;

/**
* Returns the underlying float value.
*
* \return
* The underlying value.
* The underlying value.
*
* \throws Exception if the attribute is of a different type.
*/
[[nodiscard]]
auto as_float() const -> float_type
{
return _get<float_type>();
}
auto as_float() const -> float_type;

/**
* Returns the underlying 2D float vector value.
*
* \return
* The underlying value.
* The underlying value.
*
* \throws Exception if the attribute is of a different type.
*/
[[nodiscard]]
auto as_float2() const -> const float2_type&
{
return _get<float2_type>();
}
auto as_float2() const -> const float2_type&;

/**
* Returns the underlying 3D float vector value.
*
* \return
* The underlying value.
* The underlying value.
*
* \throws Exception if the attribute is of a different type.
*/
[[nodiscard]]
auto as_float3() const -> const float3_type&
{
return _get<float3_type>();
}
auto as_float3() const -> const float3_type&;

/**
* Returns the underlying 4D float vector value.
*
* \return
* The underlying value.
* The underlying value.
*
* \throws Exception if the attribute is of a different type.
*/
[[nodiscard]]
auto as_float4() const -> const float4_type&
{
return _get<float4_type>();
}
auto as_float4() const -> const float4_type&;

/**
* Returns the underlying boolean value.
*
* \return
* The underlying value.
* The underlying value.
*
* \throws Exception if the attribute is of a different type.
*/
[[nodiscard]]
auto as_bool() const -> bool
{
return _get<bool>();
}
auto as_bool() const -> bool;

/**
* Returns the underlying color value.
*
* \return
* The underlying value.
* The underlying value.
*
* \throws Exception if the attribute is of a different type.
*/
[[nodiscard]]
auto as_color() const -> const color_type&
{
return _get<color_type>();
}
auto as_color() const -> const color_type&;

/**
* Returns the underlying file path value.
*
* \return
* The underlying value.
* The underlying value.
*
* \throws Exception if the attribute is of a different type.
*/
[[nodiscard]]
auto as_path() const -> const path_type&
{
return _get<path_type>();
}
auto as_path() const -> const path_type&;

/**
* Returns the underlying object reference value.
*
* \return
* The underlying value.
* The underlying value.
*
* \throws Exception if the attribute is of a different type.
*/
[[nodiscard]]
auto as_object() const -> objref_type
{
return _get<objref_type>();
}
auto as_object() const -> objref_type;

/**
* Indicates whether the attribute is a vector attribute.
*
* \return
* True if the attribute has a vector value; false otherwise.
* True if the attribute has a vector value; false otherwise.
*/
[[nodiscard]]
auto is_vector() const -> bool;
Expand All @@ -340,7 +297,7 @@ class Attribute final {
* Indicates whether the attribute has the default value for its type.
*
* \return
* True if the value is the default one; false otherwise.
* True if the value is the default one; false otherwise.
*/
[[nodiscard]]
auto has_default_value() const -> bool;
Expand All @@ -349,7 +306,7 @@ class Attribute final {
* Returns the type of the current value.
*
* \return
* The current type.
* The current type.
*/
[[nodiscard]]
auto get_type() const -> AttributeType;
Expand Down Expand Up @@ -377,8 +334,9 @@ class Attribute final {
* \param attribute The attribute to output.
*
* \return
* The provided stream.
* The provided stream.
*/
auto operator<<(std::ostream& stream, const Attribute& attribute) -> std::ostream&;
auto operator<<(std::ostream& stream,
const Attribute& attribute) -> std::ostream&;

} // namespace tactile
Loading

0 comments on commit 7d7b613

Please sign in to comment.