Skip to content

Commit

Permalink
Improved Asset::Commit - now data can be pushed without a trait, endi…
Browse files Browse the repository at this point in the history
…ng up in a default bucket
  • Loading branch information
Epixu committed Mar 29, 2024
1 parent 1ee3601 commit a08b937
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 37 deletions.
14 changes: 7 additions & 7 deletions include/Langulus/Asset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,24 @@ namespace Langulus::A

virtual bool Generate(TMeta, Offset = 0) = 0;

template<CT::Trait, template<class> class S, CT::Block B>
requires CT::Semantic<S<B>>
void Commit(S<B>&&) const;
template<CT::TraitBased = Langulus::Trait, class B>
requires CT::Block<Desem<B>>
void Commit(B&&) const;

template<CT::Trait>
template<CT::TraitBased = Langulus::Trait>
NOD() const Data* GetData(Offset = 0) const noexcept;
NOD() const Data* GetData(TMeta, Offset = 0) const noexcept;

template<CT::Trait>
template<CT::TraitBased = Langulus::Trait>
NOD() const DataList* GetDataList() const noexcept;
NOD() const DataList* GetDataList(TMeta) const noexcept;

protected:
template<CT::Trait>
template<CT::TraitBased = Langulus::Trait>
NOD() Data* GetData(Offset = 0) noexcept;
NOD() Data* GetData(TMeta, Offset = 0) noexcept;

template<CT::Trait>
template<CT::TraitBased = Langulus::Trait>
NOD() DataList* GetDataList() noexcept;
NOD() DataList* GetDataList(TMeta) noexcept;

Expand Down
69 changes: 44 additions & 25 deletions include/Langulus/Asset.inl
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,23 @@ namespace Langulus::A
/// @tparam T - the trait to search for
/// @param index - the Nth data associated to the trait
/// @return a pointer to the data entry, or nullptr if none exists
template<CT::Trait T> LANGULUS(INLINED)
Asset::Data const* Asset::GetData(Offset index) const noexcept {
return GetData(MetaTraitOf<T>(), index);
template<CT::TraitBased T> LANGULUS(INLINED)
Asset::Data* Asset::GetData(Offset index) noexcept {
TMeta trait;
if constexpr (CT::Trait<T>)
trait = MetaTraitOf<T>();
return GetData(trait, index);
}

template<CT::Trait T> LANGULUS(INLINED)
Asset::Data* Asset::GetData(Offset index) noexcept {
return GetData(MetaTraitOf<T>(), index);
template<CT::TraitBased T> LANGULUS(INLINED)
Asset::Data const* Asset::GetData(Offset index) const noexcept {
return const_cast<Asset*>(this)->GetData<T>(index);
}

/// Get a single data entry from the contents
/// @param trait - the trait to search for
/// @param index - the Nth data associated to the trait
/// @return a pointer to the data entry, or nullptr if none exists
LANGULUS(INLINED)
Asset::Data const* Asset::GetData(TMeta trait, Offset index) const noexcept {
return const_cast<Asset*>(this)->GetData(trait, index);
}

LANGULUS(INLINED)
Asset::Data* Asset::GetData(TMeta trait, Offset index) noexcept {
if (not Generate(trait, index))
Expand All @@ -67,17 +65,25 @@ namespace Langulus::A
: nullptr;
}

LANGULUS(INLINED)
Asset::Data const* Asset::GetData(TMeta trait, Offset index) const noexcept {
return const_cast<Asset*>(this)->GetData(trait, index);
}

/// Get a data list from the contents
/// @tparam T - the trait to search for
/// @return a pointer to the data list, or nullptr if none exists
template<CT::Trait T> LANGULUS(INLINED)
Asset::DataList const* Asset::GetDataList() const noexcept {
return GetDataList(MetaTraitOf<T>());
template<CT::TraitBased T> LANGULUS(INLINED)
Asset::DataList* Asset::GetDataList() noexcept {
TMeta trait;
if constexpr (CT::Trait<T>)
trait = MetaTraitOf<T>();
return GetDataList(trait);
}

template<CT::Trait T> LANGULUS(INLINED)
Asset::DataList* Asset::GetDataList() noexcept {
return GetDataList(MetaTraitOf<T>());
template<CT::TraitBased T> LANGULUS(INLINED)
Asset::DataList const* Asset::GetDataList() const noexcept {
return const_cast<Asset*>(this)->GetDataList<T>();
}

/// Get a data list from the contents
Expand All @@ -98,17 +104,30 @@ namespace Langulus::A
}

/// Commit data to an asset
/// @attention data is always commited, even if such trait already exists
/// @tparam T - data trait, the intent we're commiting with
/// @attention data is always comitted, even if trait already exists
/// @tparam T - trait (a.k.a. the intent we're commiting with)
/// @attention default value leads all data to the no-trait bucket
/// @tparam B - block of data to insert (semantically or not)
/// @attention you can use references to external data, but beware
/// that asset data is intrinsically paired with the asset
/// descriptor, and any silent change in contents might cause
/// disparity and confusion in asset libraries. Still useful when
/// you don't use factories, and just want some local content
/// representation.
/// @param content - the data and semantic to commit
template<CT::Trait T, template<class> class S, CT::Block B>
requires CT::Semantic<S<B>> LANGULUS(INLINED)
void Asset::Commit(S<B>&& content) const {
const auto found = mDataListMap.FindIt(MetaTraitOf<T>());
template<CT::TraitBased T, class B>
requires CT::Block<Desem<B>> LANGULUS(INLINED)
void Asset::Commit(B&& content) const {
using S = SemanticOf<B>;
TMeta trait;
if constexpr (CT::Trait<T>)
trait = MetaTraitOf<T>();

const auto found = mDataListMap.FindIt(trait);
if (found)
*found.mValue << Any {content.Forward()};
*found.mValue << Any {S::Nest(content)};
else
mDataListMap.Insert(MetaTraitOf<T>(), content.Forward());
mDataListMap.Insert(trait, S::Nest(content));
}

} // namespace Langulus::A
5 changes: 2 additions & 3 deletions include/Langulus/Image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ namespace Langulus::A
public:
LANGULUS_BASES(Asset);
Image()
: Resolvable {this}
, ProducedFrom {nullptr, {}} {}
: Resolvable {this} {}

NOD() virtual Ref<Image> GetLOD(const Math::LOD&) const = 0;
NOD() virtual void* GetGPUHandle() const noexcept = 0;
Expand All @@ -80,7 +79,7 @@ namespace Langulus::A

NOD() Iterator<true> begin() noexcept;
NOD() Iterator<false> begin() const noexcept;
NOD() A::IteratorEnd end() const noexcept { return {}; }
NOD() A::IteratorEnd end() const noexcept { return {}; }
};


Expand Down
2 changes: 1 addition & 1 deletion include/Langulus/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace Langulus::A

public:
LANGULUS_BASES(Asset);
Mesh() : Resolvable {this}, ProducedFrom {nullptr, {}} {}
Mesh() : Resolvable {this} {}

template<CT::Topology, CT::Topology...>
NOD() bool CheckTopology() const;
Expand Down
5 changes: 4 additions & 1 deletion include/Langulus/Platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ LANGULUS_DEFINE_TRAIT(NativeWindowHandle,
namespace Langulus::A
{

struct Image; // If undefined, include <Langulus/Image.hpp>


///
/// Abstract platform module
///
Expand Down Expand Up @@ -53,7 +56,7 @@ namespace Langulus::A
NOD() virtual void* GetNativeHandle() const noexcept = 0;
NOD() virtual Scale2 GetSize() const noexcept = 0;
NOD() virtual bool IsMinimized() const noexcept = 0;
NOD() virtual bool Draw(const Any&) const { return false; }
NOD() virtual bool Draw(const Ref<Image>&) const { return false; }
};

///
Expand Down

0 comments on commit a08b937

Please sign in to comment.