Skip to content
Snippets Groups Projects

ENH: New functions getEnumNameFromValue() and getEnumValueFromName()

Merged Daniel Hornung requested to merge f-enum-utilities into f-consolidation
All threads resolved!
1 file
+ 1
1
Compare changes
  • Side-by-side
  • Inline
+ 45
39
@@ -38,17 +38,16 @@ using ProtoDataType = caosdb::entity::v1alpha1::DataType;
using ProtoListDataType = caosdb::entity::v1alpha1::ListDataType;
using ProtoReferenceDataType = caosdb::entity::v1alpha1::ReferenceDataType;
using DataTypeCase = caosdb::entity::v1alpha1::DataType::DataTypeCase;
using ListDataTypeCase =
caosdb::entity::v1alpha1::ListDataType::ListDataTypeCase;
using ListDataTypeCase = caosdb::entity::v1alpha1::ListDataType::ListDataTypeCase;
using caosdb::utility::ProtoMessageWrapper;
class Entity;
class Property;
// Atomic data types.
enum AtomicDataType {
enum class AtomicDataType {
// The data type is unset/unknown.
UNSPECIFIED_DATA_TYPE = ProtoAtomicDataType::ATOMIC_DATA_TYPE_UNSPECIFIED,
UNSPECIFIED = ProtoAtomicDataType::ATOMIC_DATA_TYPE_UNSPECIFIED,
// TEXT data type.
TEXT = ProtoAtomicDataType::ATOMIC_DATA_TYPE_TEXT,
// DOUBLE data type.
@@ -61,13 +60,18 @@ enum AtomicDataType {
BOOLEAN = ProtoAtomicDataType::ATOMIC_DATA_TYPE_BOOLEAN,
};
const std::map<AtomicDataType, std::string> atomicdatatype_names = {
{AtomicDataType::UNSPECIFIED, "UNSPECIFIED"}, {AtomicDataType::TEXT, "TEXT"},
{AtomicDataType::DOUBLE, "DOUBLE"}, {AtomicDataType::DATETIME, "DATETIME"},
{AtomicDataType::INTEGER, "INTEGER"}, {AtomicDataType::BOOLEAN, "BOOLEAN"}};
class DataType;
class ListDataType;
class ReferenceDataType : public ProtoMessageWrapper<ProtoDataType> {
public:
[[nodiscard]] inline auto GetName() const noexcept -> const std::string & {
// is list of refrence?
// is list of reference?
if (this->wrapped->data_type_case() == DataTypeCase::kListDataType) {
return this->wrapped->list_data_type().reference_data_type().name();
}
@@ -84,13 +88,11 @@ protected:
static ReferenceDataType instance;
return instance;
}
inline static auto Create(ProtoDataType *wrapped)
-> std::unique_ptr<ReferenceDataType> {
inline static auto Create(ProtoDataType *wrapped) -> std::unique_ptr<ReferenceDataType> {
return std::unique_ptr<ReferenceDataType>(new ReferenceDataType(wrapped));
}
ReferenceDataType() : ProtoMessageWrapper<ProtoDataType>() {}
ReferenceDataType(ProtoDataType *wrapped)
: ProtoMessageWrapper<ProtoDataType>(wrapped) {}
ReferenceDataType(ProtoDataType *wrapped) : ProtoMessageWrapper<ProtoDataType>(wrapped) {}
};
class ListDataType : public ProtoMessageWrapper<ProtoDataType> {
@@ -99,14 +101,13 @@ public:
return this->wrapped->list_data_type().list_data_type_case() ==
ListDataTypeCase::kReferenceDataType;
}
[[nodiscard]] inline auto GetReferenceDataType() const
-> const ReferenceDataType & {
[[nodiscard]] inline auto GetReferenceDataType() const -> const ReferenceDataType & {
if (!IsListOfReference()) {
return ReferenceDataType::GetEmptyInstance();
}
if (reference_data_type == nullptr) {
this->reference_data_type = std::unique_ptr<ReferenceDataType>(
ReferenceDataType::Create(this->wrapped).release());
this->reference_data_type =
std::unique_ptr<ReferenceDataType>(ReferenceDataType::Create(this->wrapped).release());
}
return *this->reference_data_type;
}
@@ -116,8 +117,7 @@ public:
ListDataTypeCase::kAtomicDataType;
}
[[nodiscard]] inline auto GetAtomicDataType() const -> AtomicDataType {
return static_cast<AtomicDataType>(
this->wrapped->list_data_type().atomic_data_type());
return static_cast<AtomicDataType>(this->wrapped->list_data_type().atomic_data_type());
}
friend class DataType;
@@ -127,33 +127,43 @@ protected:
static auto empty_instance = ListDataType();
return empty_instance;
}
inline static auto Create(ProtoDataType *wrapped)
-> std::unique_ptr<ListDataType> {
inline static auto Create(ProtoDataType *wrapped) -> std::unique_ptr<ListDataType> {
return std::unique_ptr<ListDataType>(new ListDataType(wrapped));
}
ListDataType() : ProtoMessageWrapper<ProtoDataType>() {}
ListDataType(ProtoDataType *wrapped)
: ProtoMessageWrapper<ProtoDataType>(wrapped) {}
ListDataType(ProtoDataType *wrapped) : ProtoMessageWrapper<ProtoDataType>(wrapped) {}
mutable std::unique_ptr<ReferenceDataType> reference_data_type;
};
class DataType : public ProtoMessageWrapper<ProtoDataType> {
public:
DataType(ProtoDataType *wrapped)
: ProtoMessageWrapper<ProtoDataType>(wrapped) {}
DataType(ProtoDataType *wrapped) : ProtoMessageWrapper<ProtoDataType>(wrapped) {}
DataType() : ProtoMessageWrapper<ProtoDataType>() {}
DataType(AtomicDataType data_type) : DataType() {
this->wrapped->set_atomic_data_type(
static_cast<ProtoAtomicDataType>(data_type));
/**
* Create an AtomicDataType typed DataType. For references, use the std::string constructor.
*/
DataType(AtomicDataType data_type, bool list_type = false) : DataType() {
if (list_type) {
this->wrapped->mutable_list_data_type()->set_atomic_data_type(
static_cast<ProtoAtomicDataType>(data_type));
} else {
this->wrapped->set_atomic_data_type(static_cast<ProtoAtomicDataType>(data_type));
}
}
DataType(const std::string &data_type) : DataType() {
this->wrapped->mutable_reference_data_type()->set_name(data_type);
/**
* Create a reference typed DataType.
*/
DataType(const std::string &data_type, bool list_type = false) : DataType() {
if (list_type) {
this->wrapped->mutable_list_data_type()->mutable_reference_data_type()->set_name(data_type);
} else {
this->wrapped->mutable_reference_data_type()->set_name(data_type);
}
}
inline static auto ListOf(const AtomicDataType &atomic_data_type)
-> DataType {
inline static auto ListOf(const AtomicDataType &atomic_data_type) -> DataType {
DataType result;
result.wrapped->mutable_list_data_type()->set_atomic_data_type(
static_cast<ProtoAtomicDataType>(atomic_data_type));
@@ -161,9 +171,8 @@ public:
}
inline static auto ListOf(const std::string reference_data_type) -> DataType {
DataType result;
result.wrapped->mutable_list_data_type()
->mutable_reference_data_type()
->set_name(reference_data_type);
result.wrapped->mutable_list_data_type()->mutable_reference_data_type()->set_name(
reference_data_type);
return result;
}
@@ -177,13 +186,12 @@ public:
[[nodiscard]] inline auto IsReference() const noexcept -> bool {
return this->wrapped->data_type_case() == DataTypeCase::kReferenceDataType;
}
[[nodiscard]] inline auto AsReference() const noexcept
-> const ReferenceDataType & {
[[nodiscard]] inline auto AsReference() const noexcept -> const ReferenceDataType & {
if (!IsReference()) {
return ReferenceDataType::GetEmptyInstance();
} else if (reference_data_type == nullptr) {
reference_data_type = std::unique_ptr<ReferenceDataType>(
ReferenceDataType::Create(this->wrapped).release());
reference_data_type =
std::unique_ptr<ReferenceDataType>(ReferenceDataType::Create(this->wrapped).release());
}
return *reference_data_type;
}
@@ -196,16 +204,14 @@ public:
if (!IsList()) {
return ListDataType::GetEmptyInstance();
} else if (list_data_type == nullptr) {
list_data_type = std::unique_ptr<ListDataType>(
ListDataType::Create(this->wrapped).release());
list_data_type = std::unique_ptr<ListDataType>(ListDataType::Create(this->wrapped).release());
}
return *list_data_type;
}
inline auto operator==(const DataType &other) const noexcept -> bool {
// TODO(tf) Is this safe?
return this->wrapped->SerializeAsString() ==
other.wrapped->SerializeAsString();
return this->wrapped->SerializeAsString() == other.wrapped->SerializeAsString();
}
friend class Entity;
Loading