diff options
| author | HampusM <hampus@hampusmat.com> | 2022-06-09 17:46:24 +0200 | 
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2022-06-09 17:46:24 +0200 | 
| commit | 59d8202868364b6efeb92dbd694d961723b74cc0 (patch) | |
| tree | 573aea529c556bb2b893f93ac6ffd85cfb02f4c9 | |
| parent | cb169dfac720de614821480a2633bb93b6be5245 (diff) | |
refactor: replace BaseObjectType with static ObjectType method
| -rw-r--r-- | include/yacppdic/container.hpp | 16 | ||||
| -rw-r--r-- | include/yacppdic/detail/container-impl.hpp | 39 | ||||
| -rw-r--r-- | include/yacppdic/object_type.hpp | 36 | ||||
| -rw-r--r-- | src/container.cpp | 8 | ||||
| -rw-r--r-- | src/object_type.cpp | 15 | 
5 files changed, 52 insertions, 62 deletions
diff --git a/include/yacppdic/container.hpp b/include/yacppdic/container.hpp index 6308142..0f3a9fc 100644 --- a/include/yacppdic/container.hpp +++ b/include/yacppdic/container.hpp @@ -21,16 +21,13 @@ template <typename Interface>  class BindingWhen  {  public: -	explicit BindingWhen( -		Container *container, -		BaseObjectType object_type -	) noexcept; +	explicit BindingWhen(Container *container, ObjectType object_type) noexcept;  	void when_tagged(std::string_view tag) noexcept;  private:  	Container *_container; -	BaseObjectType _object_type; +	ObjectType _object_type;  };  template <typename Interface> @@ -78,15 +75,14 @@ public:  	requires IsFactory<AFactory>  	auto get_tagged(const char *tag) const noexcept -> AFactory; -	void -	add(const BaseObjectType &type, const WrapperPtr<IGenericWrapper> &wrapper) noexcept; +	void add(const ObjectType &type, const WrapperPtr<IGenericWrapper> &wrapper) noexcept; -	void remove(const BaseObjectType &type) noexcept; +	void remove(const ObjectType &type) noexcept; -	auto at(const BaseObjectType &type) const noexcept -> WrapperPtr<IGenericWrapper>; +	auto at(const ObjectType &type) const noexcept -> WrapperPtr<IGenericWrapper>;  private: -	std::unordered_map<BaseObjectType, WrapperPtr<IGenericWrapper>, ObjectTypeHasher> +	std::unordered_map<ObjectType, WrapperPtr<IGenericWrapper>, ObjectTypeHasher>  		_bindings;  }; diff --git a/include/yacppdic/detail/container-impl.hpp b/include/yacppdic/detail/container-impl.hpp index 869641c..720a88b 100644 --- a/include/yacppdic/detail/container-impl.hpp +++ b/include/yacppdic/detail/container-impl.hpp @@ -11,10 +11,7 @@ namespace yacppdic  {  template <typename Interface> -BindingWhen<Interface>::BindingWhen( -	Container *container, -	BaseObjectType object_type -) noexcept +BindingWhen<Interface>::BindingWhen(Container *container, ObjectType object_type) noexcept  	: _container(container), _object_type(std::move(object_type))  {  } @@ -26,9 +23,9 @@ void BindingWhen<Interface>::when_tagged(std::string_view tag) noexcept  	_container->remove(_object_type); -	auto object_type = ObjectType<Interface>(tag); +	auto type = ObjectType::create<Interface>(tag); -	_container->add(object_type, wrapped); +	_container->add(type, wrapped);  }  template <typename Interface> @@ -46,11 +43,11 @@ auto BindingBuilder<Interface>::to() noexcept -> BindingWhen<Interface>  	auto wrapper = Container::WrapperPtr<Wrapper>(new Wrapper(*_container)); -	auto object_type = ObjectType<Interface>(); +	auto type = ObjectType::create<Interface>(); -	_container->add(object_type, std::dynamic_pointer_cast<IGenericWrapper>(wrapper)); +	_container->add(type, std::dynamic_pointer_cast<IGenericWrapper>(wrapper)); -	return BindingWhen<Interface>(_container, object_type); +	return BindingWhen<Interface>(_container, type);  }  template <typename Interface> @@ -63,11 +60,11 @@ auto BindingBuilder<Interface>::to_factory(FactoryFunc factory) noexcept  	auto wrapper = Container::WrapperPtr<Wrapper>(new Wrapper(factory)); -	auto object_type = ObjectType<Interface>(); +	auto type = ObjectType::create<Interface>(); -	_container->add(object_type, std::dynamic_pointer_cast<IGenericWrapper>(wrapper)); +	_container->add(type, std::dynamic_pointer_cast<IGenericWrapper>(wrapper)); -	return BindingWhen<Interface>(_container, object_type); +	return BindingWhen<Interface>(_container, type);  }  template <typename Interface> @@ -80,19 +77,19 @@ template <typename Interface>  requires Abstract<Interface>  auto Container::get() const noexcept -> std::unique_ptr<Interface>  { -	ObjectType<Interface> interface_type; +	auto type = ObjectType::create<Interface>(); -	if (_bindings.count(interface_type) == 0) +	if (_bindings.count(type) == 0)  	{  		std::cerr  			<< "Error: Tried to get a item from the container using unbound interface '" -			<< interface_type.name() << "'" << std::endl; +			<< type.name() << "'" << std::endl;  		exit(EXIT_FAILURE);  	} -	auto wrapper = std::dynamic_pointer_cast<IWrapper<std::unique_ptr<Interface>>>( -		_bindings.at(interface_type) -	); +	auto wrapper = +		std::dynamic_pointer_cast<IWrapper<std::unique_ptr<Interface>>>(_bindings.at(type) +		);  	return wrapper->get();  } @@ -101,7 +98,7 @@ template <typename AFactory>  requires IsFactory<AFactory>  auto Container::get() const noexcept -> AFactory  { -	BaseObjectType type = ObjectType<AFactory>(); +	auto type = ObjectType::create<AFactory>();  	if (!_bindings.contains(type))  	{ @@ -120,7 +117,7 @@ template <class Interface>  requires Abstract<Interface>  auto Container::get_tagged(const char *tag) const noexcept -> std::unique_ptr<Interface>  { -	BaseObjectType type = ObjectType<Interface>(tag); +	auto type = ObjectType::create<Interface>(tag);  	if (!_bindings.contains(type))  	{ @@ -141,7 +138,7 @@ template <typename AFactory>  requires IsFactory<AFactory>  auto Container::get_tagged(const char *tag) const noexcept -> AFactory  { -	BaseObjectType type = ObjectType<AFactory>(tag); +	auto type = ObjectType::create<AFactory>(tag);  	if (!_bindings.contains(type))  	{ diff --git a/include/yacppdic/object_type.hpp b/include/yacppdic/object_type.hpp index 1765e65..3a7a639 100644 --- a/include/yacppdic/object_type.hpp +++ b/include/yacppdic/object_type.hpp @@ -6,17 +6,14 @@  namespace yacppdic  { -class BaseObjectType +class ObjectType  {  public: -	explicit BaseObjectType(const std::type_info &type_info) noexcept; +	explicit ObjectType(const std::type_info &type_info) noexcept; -	explicit BaseObjectType( -		const std::type_info &type_info, -		std::string_view tag -	) noexcept; +	explicit ObjectType(const std::type_info &type_info, std::string_view tag) noexcept; -	auto operator==(const BaseObjectType &object_type) const noexcept -> bool; +	auto operator==(const ObjectType &object_type) const noexcept -> bool;  	[[nodiscard]] auto hash() const noexcept -> std::size_t; @@ -24,26 +21,27 @@ public:  	[[nodiscard]] auto tag() const noexcept -> std::string_view; -private: -	const std::type_info &_type_info; -	const std::string_view _tag; -}; - -template <class Object> -class ObjectType : public BaseObjectType -{ -public: -	ObjectType() noexcept : BaseObjectType(typeid(Object)) {} +	template <typename Type> +	static auto create() noexcept -> ObjectType +	{ +		return static_cast<ObjectType>(typeid(Type)); +	} -	explicit ObjectType(const std::string_view &tag) noexcept : BaseObjectType(typeid(Object), tag) +	template <typename Type> +	static auto create(std::string_view tag) noexcept -> ObjectType  	{ +		return ObjectType(typeid(Type), tag);  	} + +private: +	const std::type_info &_type_info; +	const std::string_view _tag;  };  class ObjectTypeHasher  {  public: -	auto operator()(const BaseObjectType &object_type) const noexcept -> std::size_t; +	auto operator()(const ObjectType &object_type) const noexcept -> std::size_t;  };  } // namespace yacppdic diff --git a/src/container.cpp b/src/container.cpp index 00efc27..044d487 100644 --- a/src/container.cpp +++ b/src/container.cpp @@ -7,20 +7,20 @@ namespace yacppdic  {  void Container::add( -	const BaseObjectType &type, +	const ObjectType &type,  	const WrapperPtr<IGenericWrapper> &wrapper  ) noexcept  {  	_bindings.insert({ type, wrapper });  } -void Container::remove(const BaseObjectType &type) noexcept +void Container::remove(const ObjectType &type) noexcept  {  	_bindings.erase(type);  } -Container::WrapperPtr<IGenericWrapper> Container::at(const BaseObjectType &type -) const noexcept +auto Container::at(const ObjectType &type) const noexcept +	-> Container::WrapperPtr<IGenericWrapper>  {  	return _bindings.at(type);  } diff --git a/src/object_type.cpp b/src/object_type.cpp index bd61e09..15b798c 100644 --- a/src/object_type.cpp +++ b/src/object_type.cpp @@ -7,12 +7,11 @@  namespace yacppdic  { -BaseObjectType::BaseObjectType(const std::type_info &type_info) noexcept -	: _type_info(type_info) +ObjectType::ObjectType(const std::type_info &type_info) noexcept : _type_info(type_info)  {  } -BaseObjectType::BaseObjectType( +ObjectType::ObjectType(  	const std::type_info &type_info,  	const std::string_view tag  ) noexcept @@ -20,12 +19,12 @@ BaseObjectType::BaseObjectType(  {  } -auto BaseObjectType::operator==(const BaseObjectType &object_type) const noexcept -> bool +auto ObjectType::operator==(const ObjectType &object_type) const noexcept -> bool  {  	return hash() == object_type.hash();  } -auto BaseObjectType::hash() const noexcept -> std::size_t +auto ObjectType::hash() const noexcept -> std::size_t  {  	const auto type_hash = _type_info.hash_code(); @@ -33,17 +32,17 @@ auto BaseObjectType::hash() const noexcept -> std::size_t  					  : combine_hashes(type_hash, std::hash<std::string_view>()(_tag));  } -auto BaseObjectType::name() const noexcept -> std::string_view +auto ObjectType::name() const noexcept -> std::string_view  {  	return { _type_info.name() };  } -auto BaseObjectType::tag() const noexcept -> std::string_view +auto ObjectType::tag() const noexcept -> std::string_view  {  	return _tag;  } -auto ObjectTypeHasher::operator()(const BaseObjectType &object_type) const noexcept +auto ObjectTypeHasher::operator()(const ObjectType &object_type) const noexcept  	-> std::size_t  {  	return object_type.hash();  | 
