| 1 | #pragma once |
|---|---|
| 2 | |
| 3 | #include <Core/Types.h> |
| 4 | #include <Common/typeid_cast.h> |
| 5 | #include <memory> |
| 6 | #include <typeindex> |
| 7 | |
| 8 | |
| 9 | namespace DB |
| 10 | { |
| 11 | /// Access entity is a set of data which have a name and a type. Access entity control something related to the access control. |
| 12 | /// Entities can be stored to a file or another storage, see IAccessStorage. |
| 13 | struct IAccessEntity |
| 14 | { |
| 15 | IAccessEntity() = default; |
| 16 | IAccessEntity(const IAccessEntity &) = default; |
| 17 | virtual ~IAccessEntity() = default; |
| 18 | virtual std::shared_ptr<IAccessEntity> clone() const = 0; |
| 19 | |
| 20 | std::type_index getType() const { return typeid(*this); } |
| 21 | static String getTypeName(std::type_index type); |
| 22 | const String getTypeName() const { return getTypeName(getType()); } |
| 23 | |
| 24 | template <typename EntityType> |
| 25 | bool isTypeOf() const { return isTypeOf(typeid(EntityType)); } |
| 26 | bool isTypeOf(std::type_index type) const { return type == getType(); } |
| 27 | |
| 28 | virtual void setName(const String & name_) { full_name = name_; } |
| 29 | virtual String getName() const { return full_name; } |
| 30 | String getFullName() const { return full_name; } |
| 31 | |
| 32 | friend bool operator ==(const IAccessEntity & lhs, const IAccessEntity & rhs) { return lhs.equal(rhs); } |
| 33 | friend bool operator !=(const IAccessEntity & lhs, const IAccessEntity & rhs) { return !(lhs == rhs); } |
| 34 | |
| 35 | protected: |
| 36 | String full_name; |
| 37 | |
| 38 | virtual bool equal(const IAccessEntity & other) const; |
| 39 | |
| 40 | /// Helper function to define clone() in the derived classes. |
| 41 | template <typename EntityType> |
| 42 | std::shared_ptr<IAccessEntity> cloneImpl() const |
| 43 | { |
| 44 | return std::make_shared<EntityType>(typeid_cast<const EntityType &>(*this)); |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | using AccessEntityPtr = std::shared_ptr<const IAccessEntity>; |
| 49 | } |
| 50 |