lintel 0.2.0
Loading...
Searching...
No Matches
PropertyService.h
Go to the documentation of this file.
1#ifndef LOGGING_PROPERTYSERVICE_H
2#define LOGGING_PROPERTYSERVICE_H
3
4#include <functional>
5#include <map>
6#include <optional>
7#include <ostream>
8#include <vector>
9
21
22#define DEFINE_PROPERTY(name, type, defaultValue, description, runtime) \
23 std::shared_ptr<lintel::Property<type>> name = \
24 registerProperty<type>(std::string(#name), defaultValue, description, runtime, __FILE__, __LINE__)
25#define LOAD_PROPERTIES() \
26 if (propertyService != nullptr) { propertyService->getOrCreate(m_properties); }
27
28namespace lintel {
31{
32private:
33 std::vector<std::shared_ptr<PropertyRepository>> m_propertyRepositories;
34 std::vector<std::shared_ptr<PropertyRepository>> m_mutablePropertyRepositories;
35 std::vector<std::shared_ptr<PropertyRepository>> m_shadowPropertyRepositories;
36 std::vector<std::shared_ptr<AbstractServiceInterface>> m_abstractServices;
37 // variables
38 std::map<std::string, std::shared_ptr<PropertyBase>>
39 m_properties;// identifier (name_instanceName_processName), Property
40 std::map<PropertyRepositoryType, std::shared_ptr<PropertyRepository>> m_typeRepositoryMap;
41 std::shared_ptr<IEventBus> m_changeBus;
42 std::string m_changeSubscriber;
43
44 // functions
46 static std::string createIdentifier(const std::string &name,
47 const std::string &instanceName,
48 const std::string &className,
49 const std::string &processName);
50
52 static std::map<std::string, std::shared_ptr<PropertyBase>> init(
53 const std::vector<std::shared_ptr<PropertyRepository>> &repoProperties,
54 std::map<PropertyRepositoryType, std::vector<std::shared_ptr<PropertyBase>>> &propertiesMap);
55
57 static std::vector<std::shared_ptr<PropertyRepository>> filterEnabledRepositories(
58 const std::vector<std::shared_ptr<PropertyRepository>> &propertyRepositories);
59
61 static std::vector<std::shared_ptr<PropertyRepository>> filterMutableRepositories(
62 const std::vector<std::shared_ptr<PropertyRepository>> &propertyRepository);
63
65 static std::vector<std::shared_ptr<PropertyRepository>> filterShadowRepositories(
66 const std::vector<std::shared_ptr<PropertyRepository>> &propertyRepository);
67
69 void getOrCreate(const std::vector<std::shared_ptr<PropertyBase>> &propertiesVec,
70 std::map<PropertyRepositoryType, std::vector<std::shared_ptr<PropertyBase>>> &map);
71
74 void publishChange(const std::shared_ptr<PropertyBase> &property);
75
79 bool applyChange(const PropertyChange &change);
80
85 std::optional<std::string> resolveChangeValue(const PropertyChange &change);
86
87public:
89 static constexpr std::string_view PROPERTY_CHANGES_TOPIC = "property_changes";
91 static constexpr std::string_view PROPERTY_CHANGES_TYPE = "property_change";
94 PropertyService(const std::vector<std::shared_ptr<PropertyRepository>> &propertyRepositories,
95 std::vector<std::shared_ptr<AbstractServiceInterface>> abstractServices);
96
97 virtual ~PropertyService() = default;
98
99 void onAwake() override;
100
102 void getOrCreate(const std::vector<std::shared_ptr<PropertyBase>> &propertiesVec)
103 {
104 std::map<PropertyRepositoryType, std::vector<std::shared_ptr<PropertyBase>>> map;
105 return getOrCreate(propertiesVec, map);
106 }
107
120 template<class T>
121 std::shared_ptr<Property<T>> getOrCreate(const std::string &name,
122 const std::string &instanceName,
123 const std::string &className,
124 const std::string &processName,
125 const std::string &description,
126 bool runtimeChange,
127 const T &defaultValue = T());
128
133 std::shared_ptr<PropertyBase> &get(const std::string &name,
134 const std::string &instanceName,
135 const std::string &className,
136 const std::string &processName);
137
139 std::vector<std::shared_ptr<PropertyBase>> allOf();
140
142 std::vector<std::shared_ptr<PropertyBase>> allOf(const std::string &processName);
143
145 std::vector<std::shared_ptr<PropertyBase>> allOf(const std::string &processName, const std::string &className);
146
148 std::vector<std::shared_ptr<PropertyBase>>
149 allOf(const std::string &processName, const std::string &className, const std::string &instanceName);
150
155 template<class T> void changeValueOf(const std::shared_ptr<PropertyBase> &property, const T &value);
156
158 void changeStringValueOf(const std::shared_ptr<PropertyBase> &property, const std::string &value);
159
163 void setPropertyChangeBus(const std::shared_ptr<IEventBus> &bus, const std::string &subscriberName);
164
168
170 std::map<PropertyRepositoryType, std::shared_ptr<PropertyRepository>> buildMap(
171 const std::vector<std::shared_ptr<PropertyRepository>> &vector);
172};
173
174// template functions implementations
175template<class T>
176std::shared_ptr<Property<T>> PropertyService::getOrCreate(const std::string &name,
177 const std::string &instanceName,
178 const std::string &className,
179 const std::string &processName,
180 const std::string &description,
181 bool runtimeChange,
182 const T &defaultValue /* T() default Value */)
183{
184 auto id = createIdentifier(name, instanceName, className, processName);
185 try {
186 auto propertyBase = get(name, instanceName, className, processName);
187 return std::static_pointer_cast<Property<T>>(propertyBase);
188 } catch (PropertyNotFoundException &exception) {
189 auto property = std::make_shared<Property<T>>(
190 name, instanceName, className, processName, defaultValue, description, runtimeChange);
191 m_properties.insert({ property->getIdentifier(), property });
192 if (m_mutablePropertyRepositories.empty()) {
193 LOG_ERROR("no mutable property repositories available");
194 return property;
195 }
196 for (const auto &item : m_mutablePropertyRepositories) { item->save(property); }
197 return property;
198 }
199}
200
201template<class T> void PropertyService::changeValueOf(const std::shared_ptr<PropertyBase> &property, const T &value)
202{
203 std::shared_ptr<PropertyBase> propertyBase =
204 get(property->getName(), property->getInstanceName(), property->getClassName(), property->getProcessName());
205 if (!propertyBase->isRuntimeChange()) { throw PropertyNoRuntimeChangeSupported(propertyBase); }
206 std::static_pointer_cast<Property<T>>(propertyBase)->setValue(value);
207 if (m_mutablePropertyRepositories.empty()) {
208 LOG_ERROR("no mutable property repositories available");
209 } else {
210 std::static_pointer_cast<Property<T>>(propertyBase)
211 ->setDataStorage(m_mutablePropertyRepositories[0]->getDataStorage());
212 for (const auto &item : m_mutablePropertyRepositories) { item->save(propertyBase); }
213 }
214 // publish after the repositories are updated, so that receivers resolving
215 // a truncated value from shared memory read the new value
216 publishChange(propertyBase);
217}
218
219}// namespace lintel
220#endif// LOGGING_PROPERTYSERVICE_H
#define LOG_ERROR(message,...)
Definition LoggerService.h:130
Definition PersistableBean.h:7
Definition PropertyNoRuntimeChangeSupported.h:13
Definition PropertyNotFoundException.h:11
Definition PropertyRepositoryType.h:11
Definition PropertyService.h:31
void getOrCreate(const std::vector< std::shared_ptr< PropertyBase > > &propertiesVec)
Definition PropertyService.h:102
static constexpr std::string_view PROPERTY_CHANGES_TOPIC
Definition PropertyService.h:89
std::map< PropertyRepositoryType, std::shared_ptr< PropertyRepository > > buildMap(const std::vector< std::shared_ptr< PropertyRepository > > &vector)
void setPropertyChangeBus(const std::shared_ptr< IEventBus > &bus, const std::string &subscriberName)
static constexpr std::string_view PROPERTY_CHANGES_TYPE
Definition PropertyService.h:91
std::vector< std::shared_ptr< PropertyBase > > allOf(const std::string &processName)
std::vector< std::shared_ptr< PropertyBase > > allOf(const std::string &processName, const std::string &className)
PropertyService(const std::vector< std::shared_ptr< PropertyRepository > > &propertyRepositories, std::vector< std::shared_ptr< AbstractServiceInterface > > abstractServices)
virtual ~PropertyService()=default
std::shared_ptr< PropertyBase > & get(const std::string &name, const std::string &instanceName, const std::string &className, const std::string &processName)
void onAwake() override
std::vector< std::shared_ptr< PropertyBase > > allOf()
std::size_t applyChangeNotifications()
std::vector< std::shared_ptr< PropertyBase > > allOf(const std::string &processName, const std::string &className, const std::string &instanceName)
void changeValueOf(const std::shared_ptr< PropertyBase > &property, const T &value)
Definition PropertyService.h:201
void changeStringValueOf(const std::shared_ptr< PropertyBase > &property, const std::string &value)
Definition BaseFeature.h:6
constexpr std::string_view type_name()
Definition TypeName.h:9
Definition PropertyChange.h:27