Chromium Code Reviews| Index: src/IceDefs.h |
| diff --git a/src/IceDefs.h b/src/IceDefs.h |
| index 0a1fdd11326693dcd41e5b5be2aabddcaa6008b6..9e6e3831f6ec8b71e937b9842f86dae47cd3358f 100644 |
| --- a/src/IceDefs.h |
| +++ b/src/IceDefs.h |
| @@ -93,6 +93,46 @@ inline bool operator!=(const CfgLocalAllocator<T> &, |
| return false; |
| } |
| +// makeUnique should be used when memory is expected to be allocated from the |
| +// heap (as opposed to allocated from some Allocator.) It is intended to be used |
| +// instead of new. |
| +// |
| +// The expected usage is as follows |
| +// |
| +// class MyClass { |
| +// public: |
| +// static std::unique_ptr<MyClass> create(<ctor_args>) { |
| +// return makeUnique<MyClass>(<ctor_args>); |
| +// } |
| +// |
| +// private: |
| +// ENABLE_MAKE_UNIQUE; |
| +// |
| +// MyClass(<ctor_args>) ... |
| +// } |
| +// |
| +// ENABLE_MAKE_UNIQUE is a trick that is necessary if MyClass' ctor is private. |
| +// Private ctors are highly encouraged when you're writing a class that you'd |
| +// like to have allocated with makeUnique as it would prevent users from |
| +// declaring stack allocated variables. |
| +namespace Internal { |
| +struct MakeUniqueEnabler { |
| + template <class T, class... Args> |
| + static std::unique_ptr<T> create(Args &&... TheArgs) { |
| + std::unique_ptr<T> Unique(new T(std::forward<Args>(TheArgs)...)); |
| + return Unique; |
| + } |
| +}; |
| +} // namespace Internal |
|
Jim Stichnoth
2015/06/19 23:09:25
"end of namespace Internal" for code base consiste
John
2015/06/20 00:10:33
Done.
|
| + |
| +template <class T, class... Args> |
| +static std::unique_ptr<T> makeUnique(Args &&... TheArgs) { |
| + return ::Ice::Internal::MakeUniqueEnabler::create<T>( |
| + std::forward<Args>(TheArgs)...); |
| +} |
| + |
| +#define ENABLE_MAKE_UNIQUE friend struct ::Ice::Internal::MakeUniqueEnabler |
| + |
| typedef std::string IceString; |
| typedef llvm::ilist<Inst> InstList; |
| // Ideally PhiList would be llvm::ilist<InstPhi>, and similar for |