DPsim
Loading...
Searching...
No Matches
AttributeList.h
1/* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
2 * EONERC, RWTH Aachen University
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7 *********************************************************************************/
8
9#pragma once
10
11#include <iostream>
12#include <memory>
13#include <vector>
14
15#include <dpsim-models/Attribute.h>
16#include <dpsim-models/Config.h>
17
18namespace CPS {
20class AttributeList : public SharedFactory<AttributeList> {
21private:
23 AttributeBase::Map mAttributeMap;
24
25public:
26 using Ptr = std::shared_ptr<AttributeList>;
27
28 AttributeList(){};
29
30 const AttributeBase::Map &attributes() const { return mAttributeMap; };
31
32 // Creates a new static Attribute and enters a pointer to it into this Attribute Map using the provided name.
33 template <typename T>
34 typename Attribute<T>::Ptr create(const String &name, T intitialValue = T()) {
35 typename Attribute<T>::Ptr newAttr =
36 AttributePointer<Attribute<T>>(AttributeStatic<T>::make(intitialValue));
37 mAttributeMap[name] = newAttr;
38 return newAttr;
39 }
40
41 // Creates a new dynamic Attribute and enters a pointer to it into this Attribute Map using the provided name.
42 template <typename T>
43 typename Attribute<T>::Ptr createDynamic(const String &name) {
44 typename Attribute<T>::Ptr newAttr =
45 AttributePointer<Attribute<T>>(AttributeDynamic<T>::make());
46 mAttributeMap[name] = newAttr;
47 return newAttr;
48 }
49
51 AttributeBase::Ptr attribute(const String &name) const {
52 auto it = mAttributeMap.find(name);
53 if (it == mAttributeMap.end())
55
56 return it->second;
57 }
58
60 template <typename T>
61 typename Attribute<T>::Ptr attributeTyped(const String &name) const {
62 auto attr = attribute(name);
63 auto attrPtr = std::dynamic_pointer_cast<Attribute<T>>(attr.getPtr());
64
65 if (attrPtr == nullptr)
67
68 return typename Attribute<T>::Ptr(attrPtr);
69 }
70};
71} // namespace CPS
AttributeBase::Ptr attribute(const String &name) const
Return pointer to an attribute.
Attribute< T >::Ptr attributeTyped(const String &name) const
Return pointer to an attribute.