Using property wrapper for all property accesses

This commit is contained in:
WolverinDEV 2021-03-01 14:37:02 +01:00
parent 465975e9ca
commit 206e0052d1
3 changed files with 63 additions and 43 deletions

View File

@ -34,7 +34,7 @@ void BasicChannel::setProperties(const std::shared_ptr<ts::PropertyManager>& 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<system_clock> lastLeft = time_point<system_clock>() + milliseconds(
properties()[property::CHANNEL_LAST_LEFT].as_unchecked<uint64_t>());
properties()[property::CHANNEL_LAST_LEFT].as_or<uint64_t>(0));
return (int64_t) duration_cast<seconds>(system_clock::now() - lastLeft).count();
}

View File

@ -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);

View File

@ -759,26 +759,6 @@ namespace ts {
public:
explicit Property(std::shared_ptr<PropertyManager> /* handle */, PropertyData* /* ptr */, std::shared_ptr<PropertyBundle> /* bundle */);
template <typename T>
bool operator==(const T& other) {
auto value = this->as<T>();
return value.has_value() && *value == other;
}
template <typename T>
bool operator!=(const T& other){
return !(*this == other);
}
/*
//Math operators
inline Property&operator++() { return operator=(this->as_unchecked<int64_t>() + 1); }
inline Property&operator++(int) { return operator=(this->as_unchecked<int64_t>() + 1); }
inline Property&operator+=(uint16_t val) { return operator=(this->as_unchecked<uint16_t>() + val); }
inline Property&operator+=(int64_t val) { return operator=(this->as_unchecked<int64_t>() + val); }
inline Property&operator+=(uint64_t val) { return operator=(this->as_unchecked<uint64_t>() + 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 <typename T>
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 <int N>
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 <typename T>
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 <int N>
Property& operator=(char(value)[N]) {
this->update_value(std::string_view{value, N});
return *this;
}
template <typename T>
bool operator==(const T& other) {
auto value = this->as<T>();
return value.has_value() && *value == other;
}
template <typename T>
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<std::shared_ptr<PropertyBundle>> properties;
};
struct PropertyWrapper {
public:
explicit PropertyWrapper(std::shared_ptr<PropertyManager> handle) : handle{std::move(handle)} {}
inline PropertyManager* operator->() {
return &*handle;
}
inline const PropertyManager* operator->() const {
return &*handle;
}
template<class F>
inline std::result_of_t<F(PropertyManager &)> operator->*(F &&f) {
return std::forward<F>(f)(*handle);
}
template<class F>
inline std::result_of_t<F(PropertyManager const &)> operator->*(F &&f) const {
return std::forward<F>(f)(*handle);
}
template <typename T>
[[nodiscard]] inline ts::Property operator[](const T& type) {
return (*handle)[type];
}
private:
std::shared_ptr<PropertyManager> handle;
};
};
//DEFINE_TRANSFORMS(ts::property::PropertyType, uint8_t);