2013-01-23 17:38:03 +00:00
|
|
|
|
#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;
|
2013-01-23 17:38:03 +00:00
|
|
|
|
|
|
|
|
|
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;
|
2013-01-23 17:38:03 +00:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
StoragePtr() {}
|
|
|
|
|
StoragePtr(const StoragePtr & p) : ptr(p.ptr) {}
|
2014-03-09 18:16:52 +00:00
|
|
|
|
inline StoragePtr(const StorageWeakPtr & p);
|
2013-01-23 17:38:03 +00:00
|
|
|
|
|
2013-11-16 15:57:00 +00:00
|
|
|
|
StoragePtr& operator= (const StoragePtr & p)
|
2013-01-23 17:38:03 +00:00
|
|
|
|
{
|
|
|
|
|
ptr = p.ptr;
|
2013-01-23 17:40:09 +00:00
|
|
|
|
return *this;
|
2013-01-23 17:38:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-05 08:32:20 +00:00
|
|
|
|
IStorage* get() const
|
2013-01-23 17:38:03 +00:00
|
|
|
|
{
|
2013-11-16 15:57:00 +00:00
|
|
|
|
if (ptr == NULL)
|
2013-02-05 08:32:20 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
else
|
|
|
|
|
return ptr->storage.get();
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-16 15:57:00 +00:00
|
|
|
|
bool operator== (const IStorage * p) const
|
2013-02-05 08:32:20 +00:00
|
|
|
|
{
|
|
|
|
|
return get() == p;
|
2013-01-23 17:38:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-16 15:57:00 +00:00
|
|
|
|
IStorage* operator-> () const
|
2013-01-23 17:38:03 +00:00
|
|
|
|
{
|
2013-02-05 08:32:20 +00:00
|
|
|
|
return get();
|
2013-01-23 17:38:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-16 15:57:00 +00:00
|
|
|
|
IStorage& operator* () const
|
2013-01-23 17:38:03 +00:00
|
|
|
|
{
|
2013-02-05 08:32:20 +00:00
|
|
|
|
return *get();
|
2013-01-23 17:38:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-16 15:57:00 +00:00
|
|
|
|
operator IStorage*() const
|
2013-01-23 17:38:03 +00:00
|
|
|
|
{
|
2013-02-05 08:32:20 +00:00
|
|
|
|
return get();
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-16 15:57:00 +00:00
|
|
|
|
operator bool() const
|
2013-02-05 08:32:20 +00:00
|
|
|
|
{
|
2013-11-16 15:57:00 +00:00
|
|
|
|
return bool(ptr);
|
2013-01-23 17:38:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-16 15:57:00 +00:00
|
|
|
|
bool operator! () const
|
2013-01-23 17:38:03 +00:00
|
|
|
|
{
|
2013-11-16 15:57:00 +00:00
|
|
|
|
return !bool(ptr);
|
2013-01-23 17:38:03 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
2014-03-09 18:16:52 +00:00
|
|
|
|
inline StoragePtr::StoragePtr(const StorageWeakPtr & p) : ptr(p.ptr) {}
|
2014-03-09 17:58:27 +00:00
|
|
|
|
|
2013-01-23 17:38:03 +00:00
|
|
|
|
}
|