Browse Source

dtoolbase: make TypeHandle a constexpr class

rdb 7 years ago
parent
commit
766b38fb7a
2 changed files with 20 additions and 9 deletions
  1. 12 6
      dtool/src/dtoolbase/typeHandle.I
  2. 8 3
      dtool/src/dtoolbase/typeHandle.h

+ 12 - 6
dtool/src/dtoolbase/typeHandle.I

@@ -194,9 +194,9 @@ output(ostream &out) const {
 /**
  * Returns a special zero-valued TypeHandle that is used to indicate no type.
  */
-INLINE TypeHandle TypeHandle::
+CONSTEXPR TypeHandle TypeHandle::
 none() {
-  return _none;
+  return TypeHandle(0);
 }
 
 /**
@@ -213,9 +213,15 @@ operator bool () const {
  *
  * See TypeRegistry::find_type_by_id().
  */
-INLINE TypeHandle TypeHandle::
+CONSTEXPR TypeHandle TypeHandle::
 from_index(int index) {
-  TypeHandle handle;
-  handle._index = index;
-  return handle;
+  return TypeHandle(index);
+}
+
+/**
+ * Private constructor for initializing a TypeHandle from an index, used by
+ * none() and by from_index().
+ */
+CONSTEXPR TypeHandle::
+TypeHandle(int index) : _index(index) {
 }

+ 8 - 3
dtool/src/dtoolbase/typeHandle.h

@@ -80,6 +80,8 @@ class TypedObject;
  */
 class EXPCL_DTOOL TypeHandle FINAL {
 PUBLISHED:
+  TypeHandle() NOEXCEPT DEFAULT_CTOR;
+
   enum MemoryClass {
     MC_singleton,
     MC_array,
@@ -127,7 +129,7 @@ PUBLISHED:
 
   INLINE int get_index() const;
   INLINE void output(ostream &out) const;
-  INLINE static TypeHandle none();
+  CONSTEXPR static TypeHandle none();
   INLINE operator bool () const;
 
   MAKE_PROPERTY(index, get_index);
@@ -140,12 +142,15 @@ public:
   void *reallocate_array(void *ptr, size_t size) RETURNS_ALIGNED(MEMORY_HOOK_ALIGNMENT);
   void deallocate_array(void *ptr);
 
-  INLINE static TypeHandle from_index(int index);
+  CONSTEXPR static TypeHandle from_index(int index);
 
 private:
-  int _index;
+  CONSTEXPR TypeHandle(int index);
+
+  // Only kept temporarily for ABI compatibility.
   static TypeHandle _none;
 
+  int _index;
   friend class TypeRegistry;
 };