Skip to content

Commit

Permalink
Enum -> EnumSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
azrogers committed Feb 10, 2025
1 parent 06fc707 commit 2bdf7ea
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace CesiumGltf {
/**
* @brief An object defining the values of an enum.
*/
struct CESIUMGLTF_API Enum final : public CesiumUtility::ExtensibleObject {
struct CESIUMGLTF_API EnumSpec : public CesiumUtility::ExtensibleObject {
/**
* @brief The original name of this type.
*/
Expand Down Expand Up @@ -81,7 +81,7 @@ struct CESIUMGLTF_API Enum final : public CesiumUtility::ExtensibleObject {
*/
int64_t getSizeBytes() const {
int64_t accum = 0;
accum += int64_t(sizeof(Enum));
accum += int64_t(sizeof(EnumSpec));
accum += CesiumUtility::ExtensibleObject::getSizeBytes() -
int64_t(sizeof(CesiumUtility::ExtensibleObject));
if (this->name) {
Expand All @@ -96,5 +96,12 @@ struct CESIUMGLTF_API Enum final : public CesiumUtility::ExtensibleObject {
}
return accum;
}

protected:
/**
* @brief This class is not meant to be instantiated directly. Use {@link Enum} instead.
*/
EnumSpec() = default;
friend struct Enum;
};
} // namespace CesiumGltf
26 changes: 26 additions & 0 deletions CesiumGltf/include/CesiumGltf/Enum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <CesiumGltf/EnumSpec.h>
#include <CesiumGltf/Library.h>
#include <CesiumGltf/PropertyEnumValue.h>

#include <optional>
#include <string_view>

namespace CesiumGltf {
/** @copydoc EnumSpec */
struct CESIUMGLTF_API Enum final : public EnumSpec {
Enum() = default;

/**
* @brief Obtains the name of the given \ref PropertyEnumValue from this enum
* definition.
*
* @param enumValue The \ref PropertyEnumValue to resolve into a name.
* @returns The name in the enum definition corresponding to this value, or an
* empty string if the value cannot be found in the definition.
*/
std::optional<std::string_view>
getName(const CesiumGltf::PropertyEnumValue& enumValue) const;
};
} // namespace CesiumGltf
19 changes: 2 additions & 17 deletions CesiumGltf/include/CesiumGltf/PropertyEnumValue.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#pragma once

#include <CesiumGltf/Enum.h>
#include <CesiumGltf/EnumValue.h>
#include <CesiumUtility/SpanHelper.h>

#include <string_view>
#include <cstdint>

namespace CesiumGltf {

Expand All @@ -19,7 +15,7 @@ class PropertyEnumValue {
/**
* @brief Constructs an empty enum value.
*/
PropertyEnumValue() : _value{} {}
PropertyEnumValue() = default;

/**
* @brief Constructs a \ref PropertyEnumValue from an integer value.
Expand All @@ -28,17 +24,6 @@ class PropertyEnumValue {
*/
PropertyEnumValue(const int64_t value) noexcept : _value{value} {}

/**
* @brief Obtains the name of this enum value from the corresponding \ref
* CesiumGltf::Enum.
*
* @param parentEnum The enum definition that this enum value corresponds to.
* @returns The name in the enum definition corresponding to this value, or an
* empty string if the value cannot be found in the definition.
*/
std::optional<std::string_view>
name(const CesiumGltf::Enum& parentEnum) const;

/**
* @brief Obtains the integer value from this \ref PropertyEnumValue.
*/
Expand Down
2 changes: 2 additions & 0 deletions CesiumGltf/include/CesiumGltf/PropertyView.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "CesiumUtility/JsonValue.h"

#include <CesiumGltf/ClassProperty.h>
#include <CesiumGltf/Enum.h>
#include <CesiumGltf/EnumValue.h>
#include <CesiumGltf/PropertyAttributeProperty.h>
#include <CesiumGltf/PropertyEnumValue.h>
#include <CesiumGltf/PropertyTableProperty.h>
Expand Down
21 changes: 21 additions & 0 deletions CesiumGltf/src/Enum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <CesiumGltf/Enum.h>

#include <algorithm>
#include <optional>
#include <string_view>

std::optional<std::string_view>
CesiumGltf::Enum::getName(const PropertyEnumValue& enumValue) const {
const auto found = std::find_if(
this->values.begin(),
this->values.end(),
[value = enumValue.value()](const CesiumGltf::EnumValue& enumValue) {
return enumValue.value == value;
});

if (found == this->values.end()) {
return std::nullopt;
}

return found->name;
}
22 changes: 0 additions & 22 deletions CesiumGltf/src/PropertyEnumValue.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
#include <CesiumGltf/Enum.h>
#include <CesiumGltf/EnumValue.h>
#include <CesiumGltf/PropertyEnumValue.h>

#include <algorithm>
#include <optional>
#include <string_view>

std::optional<std::string_view>
CesiumGltf::PropertyEnumValue::name(const CesiumGltf::Enum& parentEnum) const {
const auto found = std::find_if(
parentEnum.values.begin(),
parentEnum.values.end(),
[value = this->_value](const CesiumGltf::EnumValue& enumValue) {
return enumValue.value == value;
});

if (found == parentEnum.values.end()) {
return std::nullopt;
}

return found->name;
}

bool CesiumGltf::operator==(
const PropertyEnumValue lhs,
const PropertyEnumValue rhs) {
Expand Down
4 changes: 2 additions & 2 deletions CesiumGltf/test/TestPropertyTableView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,7 @@ TEST_CASE("Test enum PropertyTableProperty") {
enumProperty.get(static_cast<int64_t>(i)).value().value() ==
expected[i]);
REQUIRE(
enumProperty.getRaw(static_cast<int64_t>(i)).name(enumDef) ==
enumDef.getName(enumProperty.getRaw(static_cast<int64_t>(i))) ==
expectedNames[i]);
}
}
Expand Down Expand Up @@ -1388,7 +1388,7 @@ TEST_CASE("Test fixed-length enum array") {
for (int64_t j = 0; j < array.size(); ++j) {
REQUIRE(array[j].value() == values[static_cast<size_t>(i * 3 + j)]);
REQUIRE(
array[j].name(enumDef) == names[static_cast<size_t>(i * 3 + j)]);
enumDef.getName(array[j]) == names[static_cast<size_t>(i * 3 + j)]);
REQUIRE((*maybeArray)[j].value() == array[j].value());
}
}
Expand Down
3 changes: 2 additions & 1 deletion tools/generate-classes/glTF.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@
"overrideName": "Class"
},
"Enum in EXT_structural_metadata": {
"overrideName": "Enum"
"overrideName": "Enum",
"toBeInherited": true
},
"Enum Value in EXT_structural_metadata": {
"overrideName": "EnumValue"
Expand Down
2 changes: 1 addition & 1 deletion tools/generate-classes/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2bdf7ea

Please sign in to comment.