Using property wrapper for all property accesses
This commit is contained in:
parent
465975e9ca
commit
206e0052d1
@ -34,7 +34,7 @@ void BasicChannel::setProperties(const std::shared_ptr<ts::PropertyManager>& pro
|
|||||||
}
|
}
|
||||||
this->_properties = props;
|
this->_properties = props;
|
||||||
|
|
||||||
this->properties().registerNotifyHandler([&](Property& prop){
|
this->properties()->registerNotifyHandler([&](Property& prop){
|
||||||
if(prop.type() == property::CHANNEL_FLAG_DEFAULT)
|
if(prop.type() == property::CHANNEL_FLAG_DEFAULT)
|
||||||
this->properties()[property::CHANNEL_FLAG_PASSWORD] = false;
|
this->properties()[property::CHANNEL_FLAG_PASSWORD] = false;
|
||||||
else if(prop.type() == property::CHANNEL_ID)
|
else if(prop.type() == property::CHANNEL_ID)
|
||||||
@ -130,11 +130,8 @@ bool BasicChannel::passwordMatch(std::string password, bool hashed) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int64_t BasicChannel::emptySince() {
|
int64_t BasicChannel::emptySince() {
|
||||||
if (!properties().hasProperty(property::CHANNEL_LAST_LEFT))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
time_point<system_clock> lastLeft = time_point<system_clock>() + milliseconds(
|
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();
|
return (int64_t) duration_cast<seconds>(system_clock::now() - lastLeft).count();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +45,9 @@ namespace ts {
|
|||||||
|
|
||||||
inline std::string name(){ return properties()[property::CHANNEL_NAME]; }
|
inline std::string name(){ return properties()[property::CHANNEL_NAME]; }
|
||||||
inline ChannelId channelOrder(){ return this->previousChannelId(); }
|
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();
|
ChannelType::ChannelType channelType();
|
||||||
void setChannelType(ChannelType::ChannelType);
|
void setChannelType(ChannelType::ChannelType);
|
||||||
|
@ -759,26 +759,6 @@ namespace ts {
|
|||||||
public:
|
public:
|
||||||
explicit Property(std::shared_ptr<PropertyManager> /* handle */, PropertyData* /* ptr */, std::shared_ptr<PropertyBundle> /* bundle */);
|
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.
|
* Get the property manager for this property.
|
||||||
* Note: The handle might be null if the manager hasn't been created with
|
* Note: The handle might be null if the manager hasn't been created with
|
||||||
@ -885,23 +865,6 @@ namespace ts {
|
|||||||
return true;
|
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.
|
* Increment the value, if numeric, by the given value.
|
||||||
* If the value isn't cast able to T `false` will be returned.
|
* 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:
|
private:
|
||||||
/* Will be initialized by the constructor */
|
/* Will be initialized by the constructor */
|
||||||
PropertyData* property_data;
|
PropertyData* property_data;
|
||||||
@ -997,6 +987,37 @@ namespace ts {
|
|||||||
size_t properties_count{0};
|
size_t properties_count{0};
|
||||||
std::vector<std::shared_ptr<PropertyBundle>> properties;
|
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);
|
//DEFINE_TRANSFORMS(ts::property::PropertyType, uint8_t);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user