Can't figure out why this isnt working ( smart pointer casting )



  • Hi I wrote to following in an attempt to create a flexible container , that can store multiple types of objects at the same time.( idea from a post on SFML forum ) But I can't figure out why it isn't running, am I beeing stupid again ? 😃
    IProperty.hpp :

    #ifndef IPROPERTY_HPP
    #define IPROPERTY_HPP
    
    #include <string>
    
    class IProperty
    {
    	public:
    		IProperty(std::string name, const std::string id);
    		virtual ~IProperty();
    
    		const std::string   getId()  { return id_; }
    		      std::string getName(){ return name_; }
    
    			  virtual void updateProperty() = 0;
    
    	protected:
    	private:
    		const std::string   id_;
    		      std::string name_;
    };
    
    #endif
    

    IProperty.cpp :

    #include "IProperty.hpp"
    
    IProperty::IProperty(std::string name, const std::string id)
    	:id_(id), name_(name)
    {}
    
    IProperty::~IProperty()
    {}
    

    TProperty.hpp :

    #ifndef TPROPERTY_HPP
    #define TPROPERTY_HPP
    
    #include <string>
    #include <typeinfo>
    
    #include "IProperty.hpp"
    
    template <class T>
    class TProperty : public IProperty
    {
    	public:
    		TProperty(const std::string id) : IProperty(typeid(T).name(), id){}
    
    		   T getValue       (){ return value_; }
    		   void setValue(T value){ value_ = value; }
    
    		   void updateProperty() override {}
    
    	protected:
    	private:
    		T value_;
    };
    
    #endif
    

    PropertyManager.hpp :

    #ifndef PROPERTY_MANAGER
    #define PROPERTY_MANAGER
    
    #include <map>
    #include <memory>
    #include <string>
    
    #include "IProperty.hpp"
    #include "TProperty.hpp"
    
    class PropertyManager
    {
    	public:
    		PropertyManager();
    		virtual ~PropertyManager();
    
    		template < class T >
    		T getPropertyValue(const std::string id){
    			if (properties_.find(id) != properties_.end()){
    				if (properties_.at(id)->getName() == typeid(T).name()){
    					return std::dynamic_pointer_cast<TProperty<T>>(properties_.at(id))->getValue();
    				}
    			}
    		}
    
    		template < class T >
    		void setPropertyValue(const std::string id, T value){
    			if (properties_.find(id) != properties_.end()){
    				if (properties_.at(id)->getName() == typeid(T).name()){
    					std::dynamic_pointer_cast<TProperty<T>>(properties_.at(id))->setValue(value);
    				}
    			}
    		}
    
    		template < class T > 
    		void addProperty(const std::string id, T value){
    			std::cout << value << std::endl;
    
    			if (properties_.find(id) == properties_.end()){
    				if (properties_.at(id)->getName() == typeid(T).name()){
    					properties_.emplace(id, std::make_shared < TProperty<T>(id));
    					setPropertyValue(id, value);
    				}
    			}
    		}
    
    	protected:
    	private:
    		std::map<std::string, std::shared_ptr<IProperty>> properties_;
    };
    
    #endif
    

    PropertyManager.cpp :

    #include "PropertyManager.hpp"
    
    PropertyManager::PropertyManager()
    {}
    
    PropertyManager::~PropertyManager()
    {}
    

    Im Voraus schon einmal danke 😃


  • Mod

    template < class T >
            void addProperty(const std::string id, T value){
                std::cout << value << std::endl;
    
                if (properties_.find(id) == properties_.end()){
                    if (properties_.at(id)->getName() == typeid(T).name()){
    

    😮 😕
    ➡ zuviel c&p ⚠


Anmelden zum Antworten