ClickHouse/dbms/include/DB/Storages/StoragePtr.h
Michael Kolupaev a4fe773cba Merge
2013-02-22 12:18:08 +00:00

92 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
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;
public:
StoragePtr() {}
StoragePtr(const StoragePtr & p) : ptr(p.ptr) {}
StoragePtr& operator = (const StoragePtr & p)
{
ptr = p.ptr;
return *this;
}
IStorage* get() const
{
if (!ptr)
return NULL;
else
return ptr->storage.get();
}
size_t use_count() const
{
return ptr.use_count();
}
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 ptr;
}
bool operator ! () const
{
return !ptr;
}
};
}