Some time ago I wrote a little property class for a then abandoned project. I just stumbled over it again and I would like to know if the design is using good, efficient C++.
#pragma once
#ifndef NO_THREAD_SAFETY
#include <atomic>
#endif
template<typename T1>
class Property {
public:
#ifndef NO_THREAD_SAFETY
typedef std::atomic<long> PropertyID;
typedef std::atomic<T1> PropertyValue;
#else
typedef long PropertyID;
typedef T1 PropertyValue;
#endif
Property(T1 value): value(value), id(id_count++) {
}
Property(): value(), id(id_count++) {
}
Property(const Property &rhs): id(id_count++) {
value = static_cast<T1>(rhs.value);
}
Property(Property &&rhs): value(std::move(rhs.value)), id(id_count++) {
}
const Property &operator=(const Property &rhs) {
value = std::move(static_cast<T1>(rhs.value));
return *this;
}
const Property &operator=(const T1 &rhs) {
value = rhs;
return *this;
}
const Property &operator=(T1 &&rhs) {
value = std::move(rhs);
return *this;
}
bool operator==(const Property &rhs) const {
return value == rhs.value;
}
bool operator==(const T1 &rhs) const {
return value == rhs;
}
bool operator< (const Property &rhs) const {
return value < rhs.value;
}
bool operator< (const T1 &rhs) const {
return value < rhs;
}
void set(const T1 &value) {
this->value = value;
}
T1 get() const {
return value;
}
operator T1() const {
return value;
}
PropertyID getID() const {
return id;
}
private:
PropertyValue value;
PropertyID id;
static PropertyID id_count;
};
template<typename T1>
typename Property<T1>::PropertyID Property<T1>::id_count (0);