Skip to content

Commit

Permalink
Use a Mixin class
Browse files Browse the repository at this point in the history
  • Loading branch information
bejado committed Aug 21, 2024
1 parent ad44c12 commit d1dd212
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
14 changes: 14 additions & 0 deletions filament/include/filament/FilamentAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

#include <utils/compiler.h>
#include <utils/PrivateImplementation.h>
#include <utils/CString.h>

#include <optional>
#include <stddef.h>

namespace filament {
Expand Down Expand Up @@ -54,6 +56,18 @@ class UTILS_PUBLIC FilamentAPI {
template<typename T>
using BuilderBase = utils::PrivateImplementation<T>;

template <typename Builder>
class UTILS_PUBLIC NameMixin {
public:
Builder& name(const char* name, size_t len) noexcept;

std::optional<utils::CString> getName() const noexcept { return mName; }

private:
std::optional<utils::CString> mName;

};

} // namespace filament

#endif // TNT_FILAMENT_FILAMENTAPI_H
6 changes: 4 additions & 2 deletions filament/include/filament/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class UTILS_PUBLIC Texture : public FilamentAPI {


//! Use Builder to construct a Texture object instance
class Builder : public BuilderBase<BuilderDetails> {
class Builder : public BuilderBase<BuilderDetails>, public NameMixin<Builder> {
friend struct BuilderDetails;
public:
Builder() noexcept;
Expand Down Expand Up @@ -215,7 +215,7 @@ class UTILS_PUBLIC Texture : public FilamentAPI {
* @param len Length of name, should be less than or equal to 128
* @return This Builder, for chaining calls.
*/
Builder& name(const char* UTILS_NONNULL name, size_t len) noexcept;
// Builder& name(const char* UTILS_NONNULL name, size_t len) noexcept;

/**
* Creates the Texture object and returns a pointer to it.
Expand Down Expand Up @@ -569,6 +569,8 @@ class UTILS_PUBLIC Texture : public FilamentAPI {
~Texture() = default;
};

template class NameMixin<Texture::Builder>;

} // namespace filament

#endif // TNT_FILAMENT_TEXTURE_H
12 changes: 12 additions & 0 deletions filament/src/FilamentAPI-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,16 @@

#include <utils/PrivateImplementation-impl.h>

#include <algorithm>

template <typename Builder>
Builder& filament::NameMixin<Builder>::name(const char* _Nonnull name, size_t len) noexcept {
if (!name) {
return static_cast<Builder&>(*this);
}
size_t const length = std::min(len, size_t { 128u });
mName = utils::CString(name, length);
return static_cast<Builder&>(*this);
}

#endif // TNT_FILAMENT_FILAMENTAPI_IMPL_H
11 changes: 1 addition & 10 deletions filament/src/details/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,6 @@ Texture::Builder& Texture::Builder::swizzle(Swizzle r, Swizzle g, Swizzle b, Swi
return *this;
}

Texture::Builder& Texture::Builder::name(const char* name, size_t len) noexcept {
if (!name) {
return *this;
}
size_t const length = std::min(len, size_t { 128u });
mImpl->mName = utils::CString(name, length);
return *this;
}

Texture* Texture::Builder::build(Engine& engine) {
FILAMENT_CHECK_PRECONDITION(Texture::isTextureFormatSupported(engine, mImpl->mFormat))
<< "Texture format " << uint16_t(mImpl->mFormat) << " not supported on this platform";
Expand Down Expand Up @@ -253,7 +244,7 @@ FTexture::FTexture(FEngine& engine, const Builder& builder) {
mHandle = driver.importTexture(builder->mImportedId,
mTarget, mLevelCount, mFormat, mSampleCount, mWidth, mHeight, mDepth, mUsage);
}
if (auto name = builder->mName) {
if (auto name = builder.getName()) {
driver.setDebugTag(mHandle.getId(), std::move(*name));
}
}
Expand Down

0 comments on commit d1dd212

Please sign in to comment.