diff --git a/src/BasicChannel.cpp b/src/BasicChannel.cpp index 54c7306..132ea83 100644 --- a/src/BasicChannel.cpp +++ b/src/BasicChannel.cpp @@ -34,7 +34,7 @@ void BasicChannel::setProperties(const std::shared_ptr& pro } this->_properties = props; - this->properties().registerNotifyHandler([&](Property& prop){ + this->properties()->registerNotifyHandler([&](Property& prop){ if(prop.type() == property::CHANNEL_FLAG_DEFAULT) this->properties()[property::CHANNEL_FLAG_PASSWORD] = false; else if(prop.type() == property::CHANNEL_ID) @@ -130,11 +130,8 @@ bool BasicChannel::passwordMatch(std::string password, bool hashed) { } int64_t BasicChannel::emptySince() { - if (!properties().hasProperty(property::CHANNEL_LAST_LEFT)) - return 0; - time_point lastLeft = time_point() + milliseconds( - properties()[property::CHANNEL_LAST_LEFT].as_unchecked()); + properties()[property::CHANNEL_LAST_LEFT].as_or(0)); return (int64_t) duration_cast(system_clock::now() - lastLeft).count(); } diff --git a/src/BasicChannel.h b/src/BasicChannel.h index 11e8635..573f049 100644 --- a/src/BasicChannel.h +++ b/src/BasicChannel.h @@ -45,7 +45,9 @@ namespace ts { inline std::string name(){ return properties()[property::CHANNEL_NAME]; } inline ChannelId channelOrder(){ return this->previousChannelId(); } - inline PropertyManager& properties() const { return *this->_properties; } + + inline PropertyWrapper properties() { return PropertyWrapper{this->_properties}; } + inline const PropertyWrapper properties() const { return PropertyWrapper{this->_properties}; } ChannelType::ChannelType channelType(); void setChannelType(ChannelType::ChannelType); diff --git a/src/Properties.h b/src/Properties.h index d7d2941..91342d1 100644 --- a/src/Properties.h +++ b/src/Properties.h @@ -759,26 +759,6 @@ namespace ts { public: explicit Property(std::shared_ptr /* handle */, PropertyData* /* ptr */, std::shared_ptr /* bundle */); - template - bool operator==(const T& other) { - auto value = this->as(); - return value.has_value() && *value == other; - } - - template - bool operator!=(const T& other){ - return !(*this == other); - } - - /* - //Math operators - inline Property&operator++() { return operator=(this->as_unchecked() + 1); } - inline Property&operator++(int) { return operator=(this->as_unchecked() + 1); } - inline Property&operator+=(uint16_t val) { return operator=(this->as_unchecked() + val); } - inline Property&operator+=(int64_t val) { return operator=(this->as_unchecked() + val); } - inline Property&operator+=(uint64_t val) { return operator=(this->as_unchecked() + val); } - */ - /** * Get the property manager for this property. * Note: The handle might be null if the manager hasn't been created with @@ -885,23 +865,6 @@ namespace ts { return true; } - template - Property& operator=(const T& value) { - this->update_value(value); - return *this; - } - - Property& operator=(const char* value) { - this->update_value(std::string_view{value}); - return *this; - } - - template - Property& operator=(char(value)[N]) { - this->update_value(std::string_view{value, N}); - return *this; - } - /** * Increment the value, if numeric, by the given value. * If the value isn't cast able to T `false` will be returned. @@ -921,6 +884,33 @@ namespace ts { } } + template + Property& operator=(const T& value) { + this->update_value(value); + return *this; + } + + Property& operator=(const char* value) { + this->update_value(std::string_view{value}); + return *this; + } + + template + Property& operator=(char(value)[N]) { + this->update_value(std::string_view{value, N}); + return *this; + } + + template + bool operator==(const T& other) { + auto value = this->as(); + return value.has_value() && *value == other; + } + + template + bool operator!=(const T& other){ + return !(*this == other); + } private: /* Will be initialized by the constructor */ PropertyData* property_data; @@ -997,6 +987,37 @@ namespace ts { size_t properties_count{0}; std::vector> properties; }; + + struct PropertyWrapper { + public: + explicit PropertyWrapper(std::shared_ptr handle) : handle{std::move(handle)} {} + + inline PropertyManager* operator->() { + return &*handle; + } + + inline const PropertyManager* operator->() const { + return &*handle; + } + + template + inline std::result_of_t operator->*(F &&f) { + return std::forward(f)(*handle); + } + + template + inline std::result_of_t operator->*(F &&f) const { + return std::forward(f)(*handle); + } + + template + [[nodiscard]] inline ts::Property operator[](const T& type) { + return (*handle)[type]; + } + + private: + std::shared_ptr handle; + }; }; //DEFINE_TRANSFORMS(ts::property::PropertyType, uint8_t);