Skip to content

Commit

Permalink
address requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
roby2014 committed Nov 11, 2023
1 parent 3164ea6 commit f4e53bf
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 30 deletions.
24 changes: 10 additions & 14 deletions engine/include/cubos/engine/assets/assets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

#include <cubos/core/memory/guards.hpp>
#include <cubos/core/memory/type_map.hpp>
#include <cubos/core/reflection/type.hpp>
#include <cubos/core/reflection/reflect.hpp>

#include <cubos/engine/assets/bridge.hpp>
Expand Down Expand Up @@ -41,10 +40,6 @@ namespace cubos::engine
template <typename T>
using AssetWrite = core::memory::WriteGuard<T, std::unique_lock<std::shared_mutex>>;

using Type = cubos::core::reflection::Type;

using cubos::core::reflection::reflect;

/// @brief Resource which manages all assets. Responsible for loading and unloading assets,
/// storing them in memory, and providing access to them.
///
Expand Down Expand Up @@ -147,7 +142,7 @@ namespace cubos::engine
// Create a strong handle to the asset, so that the asset starts loading if it isn't already.
auto strong = this->load(handle);
auto lock = this->lockRead(strong);
auto data = static_cast<const T*>(this->access(strong, reflect<T>(), lock, false));
auto data = static_cast<const T*>(this->access(strong, core::reflection::reflect<T>(), lock, false));
return AssetRead<T>(*data, std::move(lock));
}

Expand Down Expand Up @@ -205,7 +200,8 @@ namespace cubos::engine
template <typename T>
inline Asset<T> create(T data)
{
return this->create(reflect<T>(), new T(std::move(data)), [](void* data) { delete static_cast<T*>(data); });
return this->create(core::reflection::reflect<T>(), new T(std::move(data)),
[](void* data) { delete static_cast<T*>(data); });
}

/// @brief Stores the given asset data in memory, associated with the given handle.
Expand All @@ -221,7 +217,7 @@ namespace cubos::engine
template <typename T>
inline AnyAsset store(AnyAsset handle, T data)
{
return this->store(handle, reflect<T>(), new T(std::move(data)),
return this->store(handle, core::reflection::reflect<T>(), new T(std::move(data)),
[](void* data) { delete static_cast<T*>(data); });
}

Expand Down Expand Up @@ -253,9 +249,9 @@ namespace cubos::engine
std::shared_mutex mutex; ///< Mutex for the asset data.
std::condition_variable_any cond; ///< Triggered when the asset is loaded.

void* data{nullptr}; ///< Pointer to the asset data, if loaded. Otherwise, nullptr.
Type* type{nullptr}; ///< The type of the asset data.
void (*destructor)(void*); ///< The destructor for the asset data - initially nullptr.
void* data{nullptr}; ///< Pointer to the asset data, if loaded. Otherwise, nullptr.
core::reflection::Type* type{nullptr}; ///< The type of the asset data.
void (*destructor)(void*); ///< The destructor for the asset data - initially nullptr.
};

/// @brief Stores all data necessary to load an asset.
Expand All @@ -270,15 +266,15 @@ namespace cubos::engine
/// @param data Asset data to store.
/// @param destructor Destructor for the asset data.
/// @return Strong handle to the asset.
AnyAsset create(const Type& type, void* data, void (*destructor)(void*));
AnyAsset create(const core::reflection::Type& type, void* data, void (*destructor)(void*));

/// @brief Untyped version of @ref store().
/// @param handle Handle to associate the asset with.
/// @param type Type of the asset data.
/// @param data Asset data to store.
/// @param destructor Destructor for the asset data.
/// @return Strong handle to the asset.
AnyAsset store(AnyAsset handle, const Type& type, void* data, void (*destructor)(void*));
AnyAsset store(AnyAsset handle, const core::reflection::Type& type, void* data, void (*destructor)(void*));

/// @brief Gets a pointer to the asset data associated with the given handle.
///
Expand All @@ -293,7 +289,7 @@ namespace cubos::engine
/// @param incVersion Whether to increase the asset's version.
/// @return Pointer to the asset's data.
template <typename Lock>
void* access(const AnyAsset& handle, const Type& type, Lock& lock, bool incVersion) const;
void* access(const AnyAsset& handle, const core::reflection::Type& type, Lock& lock, bool incVersion) const;

/// @brief Locks the given asset for reading.
/// @param handle Handle to lock.
Expand Down
11 changes: 4 additions & 7 deletions engine/include/cubos/engine/assets/bridge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

#pragma once

#include <typeindex>
#include <cubos/core/reflection/reflect.hpp>

#include <cubos/engine/assets/asset.hpp>
#include <cubos/core/reflection/type.hpp>

namespace cubos::engine
{
Expand All @@ -29,12 +28,10 @@ namespace cubos::engine
class AssetBridge
{
public:
using Type = cubos::core::reflection::Type;

/// @brief Constructs a bridge.
///
/// @param type Type of assets loaded by the bridge.
explicit AssetBridge(const Type& type)
explicit AssetBridge(const core::reflection::Type& type)
: mType(type)
{
}
Expand All @@ -61,12 +58,12 @@ namespace cubos::engine

/// @brief Gets the type of the assets the bridge loads.
/// @return Type of the asset.
inline const Type& assetType() const
inline const core::reflection::Type& assetType() const
{
return mType;
}

private:
const Type& mType; ///< Type of assets loaded by the bridge
const core::reflection::Type& mType; ///< Type of assets loaded by the bridge
};
} // namespace cubos::engine
2 changes: 1 addition & 1 deletion engine/include/cubos/engine/assets/bridges/binary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace cubos::engine
/// @brief Constructs a bridge.
/// @param littleEndian Whether to use little endian byte order.
BinaryBridge(bool littleEndian = true)
: FileBridge(cubos::core::reflection::reflect<T>())
: FileBridge(core::reflection::reflect<T>())
, mLittleEndian{littleEndian}
{
}
Expand Down
4 changes: 2 additions & 2 deletions engine/include/cubos/engine/assets/bridges/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#pragma once

#include <cubos/core/memory/stream.hpp>
#include <cubos/core/reflection/type.hpp>
#include <cubos/core/reflection/reflect.hpp>

#include <cubos/engine/assets/assets.hpp>

Expand All @@ -25,7 +25,7 @@ namespace cubos::engine
/// @brief Constructs a bridge.
///
/// @param type Type of assets loaded by the bridge.
explicit FileBridge(const cubos::core::reflection::Type& type)
explicit FileBridge(const core::reflection::Type& type)
: AssetBridge(type)
{
}
Expand Down
2 changes: 1 addition & 1 deletion engine/include/cubos/engine/assets/bridges/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace cubos::engine
///
/// @param indentation Indentation level to use when saving the JSON file.
JSONBridge(int indentation = 4)
: FileBridge(cubos::core::reflection::reflect<T>())
: FileBridge(core::reflection::reflect<T>())
, mIndentation{indentation}
{
}
Expand Down
5 changes: 3 additions & 2 deletions engine/include/cubos/engine/scene/bridge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

#pragma once

#include <cubos/core/reflection/reflect.hpp>

#include <cubos/engine/assets/bridge.hpp>
#include <cubos/engine/scene/scene.hpp>
#include <cubos/core/reflection/reflect.hpp>

namespace cubos::engine
{
Expand Down Expand Up @@ -50,7 +51,7 @@ namespace cubos::engine
/// @brief Constructs a bridge.
///
SceneBridge()
: AssetBridge(cubos::core::reflection::reflect<Scene>())
: AssetBridge(core::reflection::reflect<Scene>())
{
}

Expand Down
8 changes: 5 additions & 3 deletions engine/src/cubos/engine/assets/assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include <cubos/engine/assets/assets.hpp>

using cubos::core::reflection::Type;

using namespace cubos::engine;

Assets::Assets()
Expand Down Expand Up @@ -421,7 +423,7 @@ void* Assets::access(const AnyAsset& handle, const Type& type, Lock& lock, bool
}

CUBOS_ASSERT(assetEntry->status == Status::Loaded && assetEntry->data != nullptr, "Could not access asset");
CUBOS_ASSERT(assetEntry->type == type, "Type mismatch, expected {}, got {}", type.name(), assetEntry->type.name());
CUBOS_ASSERT(assetEntry->type == &type, "Type mismatch, expected {}, got {}", type.name(), assetEntry->type->name());

if (incVersion)
{
Expand Down Expand Up @@ -593,7 +595,7 @@ void Assets::loader()
{
auto assetEntry = this->entry(task.handle);
CUBOS_ASSERT(assetEntry != nullptr);
//CUBOS_ASSERT(assetEntry->type == task.bridge->assetType()); // @todo
CUBOS_ASSERT(assetEntry->type == &task.bridge->assetType());
}
}
}
Expand All @@ -608,7 +610,7 @@ std::vector<AnyAsset> Assets::listAll() const
return out;
}

const cubos::core::reflection::Type& Assets::type(const AnyAsset& handle) const
const Type& Assets::type(const AnyAsset& handle) const
{
auto assetEntry = this->entry(handle);
CUBOS_ASSERT(assetEntry != nullptr, "Could not find asset's type");
Expand Down

0 comments on commit f4e53bf

Please sign in to comment.