1 // Copyright 2021 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef INCLUDE_CPPGC_OBJECT_SIZE_TRAIT_H_ 6 #define INCLUDE_CPPGC_OBJECT_SIZE_TRAIT_H_ 7 8 #include <cstddef> 9 10 #include "cppgc/type-traits.h" 11 #include "v8config.h" // NOLINT(build/include_directory) 12 13 namespace cppgc { 14 15 namespace internal { 16 17 struct V8_EXPORT BaseObjectSizeTrait { 18 protected: 19 static size_t GetObjectSizeForGarbageCollected(const void*); 20 static size_t GetObjectSizeForGarbageCollectedMixin(const void*); 21 }; 22 23 } // namespace internal 24 25 namespace subtle { 26 27 /** 28 * Trait specifying how to get the size of an object that was allocated using 29 * `MakeGarbageCollected()`. Also supports querying the size with an inner 30 * pointer to a mixin. 31 */ 32 template <typename T, bool = IsGarbageCollectedMixinTypeV<T>> 33 struct ObjectSizeTrait; 34 35 template <typename T> 36 struct ObjectSizeTrait<T, false> : cppgc::internal::BaseObjectSizeTrait { 37 static_assert(sizeof(T), "T must be fully defined"); 38 static_assert(IsGarbageCollectedTypeV<T>, 39 "T must be of type GarbageCollected or GarbageCollectedMixin"); 40 41 static size_t GetSize(const T& object) { 42 return GetObjectSizeForGarbageCollected(&object); 43 } 44 }; 45 46 template <typename T> 47 struct ObjectSizeTrait<T, true> : cppgc::internal::BaseObjectSizeTrait { 48 static_assert(sizeof(T), "T must be fully defined"); 49 50 static size_t GetSize(const T& object) { 51 return GetObjectSizeForGarbageCollectedMixin(&object); 52 } 53 }; 54 55 } // namespace subtle 56 } // namespace cppgc 57 58 #endif // INCLUDE_CPPGC_OBJECT_SIZE_TRAIT_H_ 59