michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* A class for lazily constructing an object without sticking it on the heap. */ michael@0: michael@0: #ifndef mozilla_Maybe_h michael@0: #define mozilla_Maybe_h michael@0: michael@0: #include "mozilla/Alignment.h" michael@0: #include "mozilla/Assertions.h" michael@0: michael@0: // For placement new michael@0: #include michael@0: michael@0: namespace mozilla { michael@0: michael@0: /* michael@0: * Small utility for lazily constructing objects without using dynamic storage. michael@0: * When a Maybe is constructed, it is |empty()|, i.e., no value of T has michael@0: * been constructed and no T destructor will be called when the Maybe is michael@0: * destroyed. Upon calling |construct|, a T object will be constructed with the michael@0: * given arguments and that object will be destroyed when the owning Maybe michael@0: * is destroyed. michael@0: * michael@0: * N.B. GCC seems to miss some optimizations with Maybe and may generate extra michael@0: * branches/loads/stores. Use with caution on hot paths. michael@0: */ michael@0: template michael@0: class Maybe michael@0: { michael@0: AlignedStorage2 storage; michael@0: bool constructed; michael@0: michael@0: T& asT() { return *storage.addr(); } michael@0: michael@0: public: michael@0: Maybe() { constructed = false; } michael@0: ~Maybe() { if (constructed) asT().~T(); } michael@0: michael@0: bool empty() const { return !constructed; } michael@0: michael@0: void construct() { michael@0: MOZ_ASSERT(!constructed); michael@0: ::new (storage.addr()) T(); michael@0: constructed = true; michael@0: } michael@0: michael@0: template michael@0: void construct(const T1& t1) { michael@0: MOZ_ASSERT(!constructed); michael@0: ::new (storage.addr()) T(t1); michael@0: constructed = true; michael@0: } michael@0: michael@0: template michael@0: void construct(const T1& t1, const T2& t2) { michael@0: MOZ_ASSERT(!constructed); michael@0: ::new (storage.addr()) T(t1, t2); michael@0: constructed = true; michael@0: } michael@0: michael@0: template michael@0: void construct(const T1& t1, const T2& t2, const T3& t3) { michael@0: MOZ_ASSERT(!constructed); michael@0: ::new (storage.addr()) T(t1, t2, t3); michael@0: constructed = true; michael@0: } michael@0: michael@0: template michael@0: void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4) { michael@0: MOZ_ASSERT(!constructed); michael@0: ::new (storage.addr()) T(t1, t2, t3, t4); michael@0: constructed = true; michael@0: } michael@0: michael@0: template michael@0: void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5) { michael@0: MOZ_ASSERT(!constructed); michael@0: ::new (storage.addr()) T(t1, t2, t3, t4, t5); michael@0: constructed = true; michael@0: } michael@0: michael@0: template michael@0: void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, michael@0: const T6& t6) { michael@0: MOZ_ASSERT(!constructed); michael@0: ::new (storage.addr()) T(t1, t2, t3, t4, t5, t6); michael@0: constructed = true; michael@0: } michael@0: michael@0: template michael@0: void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, michael@0: const T6& t6, const T7& t7) { michael@0: MOZ_ASSERT(!constructed); michael@0: ::new (storage.addr()) T(t1, t2, t3, t4, t5, t6, t7); michael@0: constructed = true; michael@0: } michael@0: michael@0: template michael@0: void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, michael@0: const T6& t6, const T7& t7, const T8& t8) { michael@0: MOZ_ASSERT(!constructed); michael@0: ::new (storage.addr()) T(t1, t2, t3, t4, t5, t6, t7, t8); michael@0: constructed = true; michael@0: } michael@0: michael@0: template michael@0: void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, michael@0: const T6& t6, const T7& t7, const T8& t8, const T9& t9) { michael@0: MOZ_ASSERT(!constructed); michael@0: ::new (storage.addr()) T(t1, t2, t3, t4, t5, t6, t7, t8, t9); michael@0: constructed = true; michael@0: } michael@0: michael@0: template michael@0: void construct(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, michael@0: const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10) { michael@0: MOZ_ASSERT(!constructed); michael@0: ::new (storage.addr()) T(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10); michael@0: constructed = true; michael@0: } michael@0: michael@0: T* addr() { michael@0: MOZ_ASSERT(constructed); michael@0: return &asT(); michael@0: } michael@0: michael@0: T& ref() { michael@0: MOZ_ASSERT(constructed); michael@0: return asT(); michael@0: } michael@0: michael@0: const T& ref() const { michael@0: MOZ_ASSERT(constructed); michael@0: return const_cast(this)->asT(); michael@0: } michael@0: michael@0: void destroy() { michael@0: ref().~T(); michael@0: constructed = false; michael@0: } michael@0: michael@0: void destroyIfConstructed() { michael@0: if (!empty()) michael@0: destroy(); michael@0: } michael@0: michael@0: private: michael@0: Maybe(const Maybe& other) MOZ_DELETE; michael@0: const Maybe& operator=(const Maybe& other) MOZ_DELETE; michael@0: }; michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif /* mozilla_Maybe_h */