diff --git a/filament/backend/include/backend/BufferDescriptor.h b/filament/backend/include/backend/BufferDescriptor.h index 80fe182abb2..0d3490afb7a 100644 --- a/filament/backend/include/backend/BufferDescriptor.h +++ b/filament/backend/include/backend/BufferDescriptor.h @@ -113,7 +113,7 @@ class UTILS_PUBLIC BufferDescriptor { /** * Helper to create a BufferDescriptor that uses a KNOWN method pointer w/ object passed * by pointer as the callback. e.g.: - * auto bd = BufferDescriptor::make(buffer, size, &Foo::method, foo); + * auto bd = BufferDescriptor::make(buffer, size, foo); * * @param buffer Memory address of the CPU buffer to reference * @param size Size of the CPU buffer in bytes @@ -121,12 +121,12 @@ class UTILS_PUBLIC BufferDescriptor { * @return a new BufferDescriptor */ template - static BufferDescriptor make( - void const* buffer, size_t size, T* data, CallbackHandler* handler = nullptr) noexcept { + static BufferDescriptor make(void const* buffer, size_t size, T* data, + CallbackHandler* handler = nullptr) noexcept { return { buffer, size, handler, [](void* b, size_t s, void* u) { - (*static_cast(u)->*method)(b, s); + (static_cast(u)->*method)(b, s); }, data }; } @@ -145,14 +145,14 @@ class UTILS_PUBLIC BufferDescriptor { * @return a new BufferDescriptor */ template - static BufferDescriptor make( - void const* buffer, size_t size, T&& functor, CallbackHandler* handler = nullptr) noexcept { + static BufferDescriptor make(void const* buffer, size_t size, T&& functor, + CallbackHandler* handler = nullptr) noexcept { return { buffer, size, handler, [](void* b, size_t s, void* u) { - T& that = *static_cast(u); - that(b, s); - delete &that; + T* const that = static_cast(u); + that->operator()(b, s); + delete that; }, new T(std::forward(functor)) }; @@ -201,7 +201,7 @@ class UTILS_PUBLIC BufferDescriptor { return mUser; } - //! CPU mempry-buffer virtual address + //! CPU memory-buffer virtual address void* buffer = nullptr; //! CPU memory-buffer size in bytes diff --git a/filament/backend/include/backend/PixelBufferDescriptor.h b/filament/backend/include/backend/PixelBufferDescriptor.h index 1b498032fdc..67564fcce46 100644 --- a/filament/backend/include/backend/PixelBufferDescriptor.h +++ b/filament/backend/include/backend/PixelBufferDescriptor.h @@ -141,7 +141,7 @@ class UTILS_PUBLIC PixelBufferDescriptor : public BufferDescriptor { CallbackHandler* handler = nullptr) noexcept { return { buffer, size, format, type, alignment, left, top, stride, handler, [](void* b, size_t s, void* u) { - (*static_cast(u)->*method)(b, s); }, data }; + (static_cast(u)->*method)(b, s); }, data }; } template @@ -149,7 +149,7 @@ class UTILS_PUBLIC PixelBufferDescriptor : public BufferDescriptor { PixelDataFormat format, PixelDataType type, T* data, CallbackHandler* handler = nullptr) noexcept { return { buffer, size, format, type, handler, [](void* b, size_t s, void* u) { - (*static_cast(u)->*method)(b, s); }, data }; + (static_cast(u)->*method)(b, s); }, data }; } template @@ -157,7 +157,7 @@ class UTILS_PUBLIC PixelBufferDescriptor : public BufferDescriptor { backend::CompressedPixelDataType format, uint32_t imageSize, T* data, CallbackHandler* handler = nullptr) noexcept { return { buffer, size, format, imageSize, handler, [](void* b, size_t s, void* u) { - (*static_cast(u)->*method)(b, s); }, data + (static_cast(u)->*method)(b, s); }, data }; } @@ -168,9 +168,9 @@ class UTILS_PUBLIC PixelBufferDescriptor : public BufferDescriptor { CallbackHandler* handler = nullptr) noexcept { return { buffer, size, format, type, alignment, left, top, stride, handler, [](void* b, size_t s, void* u) { - T& that = *static_cast(u); - that(b, s); - delete &that; + T* const that = static_cast(u); + that->operator()(b, s); + delete that; }, new T(std::forward(functor)) }; } @@ -181,9 +181,9 @@ class UTILS_PUBLIC PixelBufferDescriptor : public BufferDescriptor { CallbackHandler* handler = nullptr) noexcept { return { buffer, size, format, type, handler, [](void* b, size_t s, void* u) { - T& that = *static_cast(u); - that(b, s); - delete &that; + T* const that = static_cast(u); + that->operator()(b, s); + delete that; }, new T(std::forward(functor)) }; } @@ -194,9 +194,9 @@ class UTILS_PUBLIC PixelBufferDescriptor : public BufferDescriptor { CallbackHandler* handler = nullptr) noexcept { return { buffer, size, format, imageSize, handler, [](void* b, size_t s, void* u) { - T& that = *static_cast(u); - that(b, s); - delete &that; + T* const that = static_cast(u); + that->operator()(b, s); + delete that; }, new T(std::forward(functor)) }; } diff --git a/filament/include/filament/View.h b/filament/include/filament/View.h index e15fa8724e6..ffa15367b7f 100644 --- a/filament/include/filament/View.h +++ b/filament/include/filament/View.h @@ -742,10 +742,10 @@ class UTILS_PUBLIC View : public FilamentAPI { * @param handler Handler to dispatch the callback or nullptr for the default handler. */ template - void pick(uint32_t x, uint32_t y, T* instance, backend::CallbackHandler* handler = nullptr) noexcept { + void pick(uint32_t x, uint32_t y, T* instance, + backend::CallbackHandler* handler = nullptr) noexcept { PickingQuery& query = pick(x, y, [](PickingQueryResult const& result, PickingQuery* pq) { - void* user = pq->storage; - (*static_cast(user)->*method)(result); + (static_cast(pq->storage[0])->*method)(result); }, handler); query.storage[0] = instance; } @@ -762,11 +762,11 @@ class UTILS_PUBLIC View : public FilamentAPI { * @param handler Handler to dispatch the callback or nullptr for the default handler. */ template - void pick(uint32_t x, uint32_t y, T instance, backend::CallbackHandler* handler = nullptr) noexcept { + void pick(uint32_t x, uint32_t y, T instance, + backend::CallbackHandler* handler = nullptr) noexcept { static_assert(sizeof(instance) <= sizeof(PickingQuery::storage), "user data too large"); PickingQuery& query = pick(x, y, [](PickingQueryResult const& result, PickingQuery* pq) { - void* user = pq->storage; - T* that = static_cast(user); + T* const that = static_cast(reinterpret_cast(pq->storage)); (that->*method)(result); that->~T(); }, handler); @@ -783,15 +783,15 @@ class UTILS_PUBLIC View : public FilamentAPI { * @param handler Handler to dispatch the callback or nullptr for the default handler. */ template - void pick(uint32_t x, uint32_t y, T functor, backend::CallbackHandler* handler = nullptr) noexcept { + void pick(uint32_t x, uint32_t y, T functor, + backend::CallbackHandler* handler = nullptr) noexcept { static_assert(sizeof(functor) <= sizeof(PickingQuery::storage), "functor too large"); PickingQuery& query = pick(x, y, handler, (PickingQueryResultCallback)[](PickingQueryResult const& result, PickingQuery* pq) { - void* user = pq->storage; - T& that = *static_cast(user); - that(result); - that.~T(); - }); + T* const that = static_cast(reinterpret_cast(pq->storage)); + that->operator()(result); + that->~T(); + }); new(query.storage) T(std::move(functor)); } diff --git a/libs/utils/include/utils/JobSystem.h b/libs/utils/include/utils/JobSystem.h index 1c602740de1..041c6d9c55e 100644 --- a/libs/utils/include/utils/JobSystem.h +++ b/libs/utils/include/utils/JobSystem.h @@ -169,8 +169,9 @@ class JobSystem { // the caller must ensure the object will outlive the Job template Job* createJob(Job* parent, T* data) noexcept { - Job* job = create(parent, [](void* user, JobSystem& js, Job* job) { - (*static_cast(user)->*method)(js, job); + Job* job = create(parent, +[](void* storage, JobSystem& js, Job* job) { + T* const that = static_cast(reinterpret_cast(storage)[0]); + (that->*method)(js, job); }); if (job) { job->storage[0] = data; @@ -182,8 +183,8 @@ class JobSystem { template Job* createJob(Job* parent, T data) noexcept { static_assert(sizeof(data) <= sizeof(Job::storage), "user data too large"); - Job* job = create(parent, [](void* user, JobSystem& js, Job* job) { - T* that = static_cast(user); + Job* job = create(parent, [](void* storage, JobSystem& js, Job* job) { + T* const that = static_cast(storage); (that->*method)(js, job); that->~T(); }); @@ -197,10 +198,10 @@ class JobSystem { template Job* createJob(Job* parent, T functor) noexcept { static_assert(sizeof(functor) <= sizeof(Job::storage), "functor too large"); - Job* job = create(parent, [](void* user, JobSystem& js, Job* job){ - T& that = *static_cast(user); - that(js, job); - that.~T(); + Job* job = create(parent, [](void* storage, JobSystem& js, Job* job){ + T* const that = static_cast(storage); + that->operator()(js, job); + that->~T(); }); if (job) { new(job->storage) T(std::move(functor)); @@ -252,7 +253,7 @@ class JobSystem { void signal() noexcept; /* - * Add job to this thread's execution queue and and keep a reference to it. + * Add job to this thread's execution queue and keep a reference to it. * Current thread must be owned by JobSystem's thread pool. See adopt(). * * This job MUST BE waited on with wait(), or released with release().