#include "mlir/Support/StorageUniquer.h"
#include "gmock/gmock.h"
using namespace mlir;
namespace {
template <typename ConcreteT, typename... Args>
struct SimpleStorage : public StorageUniquer::BaseStorage {
using Base = SimpleStorage<ConcreteT, Args...>;
using KeyTy = std::tuple<Args...>;
SimpleStorage(KeyTy key) : key(key) {}
template <typename... ParamsT>
static ConcreteT *get(StorageUniquer &uniquer, ParamsT &&...params) {
return uniquer.get<ConcreteT>(
{}, std::make_tuple(std::forward<ParamsT>(params)...));
}
static ConcreteT *construct(StorageUniquer::StorageAllocator &alloc,
KeyTy key) {
return new (alloc.allocate<ConcreteT>())
ConcreteT(std::forward<KeyTy>(key));
}
bool operator==(const KeyTy &key) const { return this->key == key; }
KeyTy key;
};
}
TEST(StorageUniquerTest, NonTrivialDestructor) {
struct NonTrivialStorage : public SimpleStorage<NonTrivialStorage, bool *> {
using Base::Base;
~NonTrivialStorage() {
bool *wasDestructed = std::get<0>(key);
*wasDestructed = true;
}
};
bool wasDestructed = false;
{
StorageUniquer uniquer;
uniquer.registerParametricStorageType<NonTrivialStorage>();
NonTrivialStorage::get(uniquer, &wasDestructed);
}
EXPECT_TRUE(wasDestructed);
}