ClickHouse/dbms/include/DB/Storages/StoragePtr.h

108 lines
1.6 KiB
C
Raw Normal View History

#pragma once
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/scoped_ptr.hpp>
#include <DB/Core/Exception.h>
namespace DB
{
class IStorage;
2014-03-09 17:58:27 +00:00
class StorageWeakPtr;
class StoragePtr
{
private:
/// Содержит IStorage. В деструкторе при необходимости вызывает IStorage::dropImpl() перед уничтожением IStorage.
struct Wrapper
{
Wrapper();
Wrapper(IStorage * s);
boost::scoped_ptr<IStorage> storage;
~Wrapper();
};
StoragePtr(boost::weak_ptr<Wrapper> p) : ptr(p) {}
boost::shared_ptr<Wrapper> ptr;
friend class IStorage;
2014-03-09 17:58:27 +00:00
friend class StorageWeakPtr;
public:
StoragePtr() {}
StoragePtr(const StoragePtr & p) : ptr(p.ptr) {}
inline StoragePtr(const StorageWeakPtr & p);
StoragePtr& operator= (const StoragePtr & p)
{
ptr = p.ptr;
return *this;
}
IStorage* get() const
{
if (ptr == NULL)
return NULL;
else
return ptr->storage.get();
}
bool operator== (const IStorage * p) const
{
return get() == p;
}
IStorage* operator-> () const
{
return get();
}
IStorage& operator* () const
{
return *get();
}
operator IStorage*() const
{
return get();
}
operator bool() const
{
return bool(ptr);
}
bool operator! () const
{
return !bool(ptr);
}
};
2014-03-09 17:58:27 +00:00
class StorageWeakPtr
{
public:
2014-03-13 12:48:07 +00:00
StorageWeakPtr() {}
2014-03-09 17:58:27 +00:00
StorageWeakPtr(const StoragePtr & p) : ptr(p.ptr) {}
StoragePtr lock()
{
return StoragePtr(ptr);
}
private:
friend class StoragePtr;
boost::weak_ptr<StoragePtr::Wrapper> ptr;
};
inline StoragePtr::StoragePtr(const StorageWeakPtr & p) : ptr(p.ptr) {}
2014-03-09 17:58:27 +00:00
}