1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- #ifndef DENSEMAPEXTRAS_H
- #define DENSEMAPEXTRAS_H
- #include "llvm-DenseMap.h"
- #include "llvm-DenseSet.h"
- namespace objc {
- template <typename Type>
- class ExplicitInit {
- alignas(Type) uint8_t _storage[sizeof(Type)];
- public:
- template <typename... Ts>
- void init(Ts &&... Args) {
- new (_storage) Type(std::forward<Ts>(Args)...);
- }
- Type &get() {
- return *reinterpret_cast<Type *>(_storage);
- }
- };
- template <typename Type>
- class LazyInit {
- alignas(Type) uint8_t _storage[sizeof(Type)];
- bool _didInit;
- public:
- template <typename... Ts>
- Type *get(bool allowCreate, Ts &&... Args) {
- if (!_didInit) {
- if (!allowCreate) {
- return nullptr;
- }
- new (_storage) Type(std::forward<Ts>(Args)...);
- _didInit = true;
- }
- return reinterpret_cast<Type *>(_storage);
- }
- };
- template <typename Key, typename Value>
- class ExplicitInitDenseMap : public ExplicitInit<DenseMap<Key, Value>> { };
- template <typename Key, typename Value>
- class LazyInitDenseMap : public LazyInit<DenseMap<Key, Value>> { };
- template <typename Value>
- class ExplicitInitDenseSet : public ExplicitInit<DenseSet<Value>> { };
- template <typename Value>
- class LazyInitDenseSet : public LazyInit<DenseSet<Value>> { };
- }
- #endif
|