CbmRoot
Loading...
Searching...
No Matches
Property.h
Go to the documentation of this file.
1/* Copyright (C) 2023 FIAS Frankfurt Institute for Advanced Studies, Frankfurt / Main
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Felix Weiglhofer [committer] */
4#pragma once
5
6#include "Definitions.h"
7
8#include <optional>
9#include <string_view>
10#include <tuple>
11
12#include <yaml-cpp/emittermanip.h>
13
14namespace cbm::algo::yaml
15{
16
17 template<typename Class, typename T>
18 class Property {
19
20 private:
21 T Class::*fMember;
22 std::string_view fKey;
23 std::string_view fDescription;
24 std::optional<YAML::EMITTER_MANIP> fFormat;
25 std::optional<YAML::EMITTER_MANIP> fFormatEntries;
26
27 public:
28 using ClassType = Class;
29 using ValueType = T;
30
31 Property() = delete;
32
33 constexpr Property(T Class::*member, std::string_view key, std::string_view description = "",
34 std::optional<YAML::EMITTER_MANIP> fmt = {}, std::optional<YAML::EMITTER_MANIP> fmtEntries = {})
35 : fMember(member)
36 , fKey(key)
37 , fDescription(description)
38 , fFormat(fmt)
39 , fFormatEntries(fmtEntries)
40 {
41 }
42
43 Property(const Property&) = delete;
44 Property& operator=(const Property&) = delete;
45
46 Property(Property&&) = default;
48
49 std::string_view Key() const { return fKey; }
50 std::string_view Description() const { return fDescription; }
51 std::optional<YAML::EMITTER_MANIP> Format() const { return fFormat; }
52 std::optional<YAML::EMITTER_MANIP> FormatEntries() const { return fFormatEntries; }
53
54 T& Get(Class& object) const { return object.*fMember; }
55 const T& Get(const Class& object) const { return object.*fMember; }
56
57 void Set(Class& object, const T& value) const { object.*fMember = value; }
58 };
59
60 template<typename Class, typename T>
61 Property(T Class::*member, std::string_view key, std::string_view description) -> Property<Class, T>;
62
63} // namespace cbm::algo::yaml
64
65#define CBM_YAML_PROPERTIES(...) \
66 public: \
67 static constexpr auto Properties = std::make_tuple(__VA_ARGS__)
68
72#define CBM_YAML_FORMAT(tag) \
73 public: \
74 static constexpr std::optional<YAML::EMITTER_MANIP> FormatAs = tag
75
82#define CBM_YAML_MERGE_PROPERTY() \
83 public: \
84 static constexpr bool MergeProperty = true
Property & operator=(Property &&)=default
std::optional< YAML::EMITTER_MANIP > FormatEntries() const
Definition Property.h:52
Property(Property &&)=default
std::string_view Key() const
Definition Property.h:49
std::string_view Description() const
Definition Property.h:50
const T & Get(const Class &object) const
Definition Property.h:55
std::optional< YAML::EMITTER_MANIP > fFormatEntries
Definition Property.h:25
std::optional< YAML::EMITTER_MANIP > fFormat
Definition Property.h:24
Property & operator=(const Property &)=delete
void Set(Class &object, const T &value) const
Definition Property.h:57
Property(const Property &)=delete
std::string_view fKey
Definition Property.h:22
constexpr Property(T Class::*member, std::string_view key, std::string_view description="", std::optional< YAML::EMITTER_MANIP > fmt={}, std::optional< YAML::EMITTER_MANIP > fmtEntries={})
Definition Property.h:33
T & Get(Class &object) const
Definition Property.h:54
std::optional< YAML::EMITTER_MANIP > Format() const
Definition Property.h:51
std::string_view fDescription
Definition Property.h:23
Property(T Class::*member, std::string_view key, std::string_view description) -> Property< Class, T >