DPsim
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 
18 namespace CPS {
20 class AttributeList : public SharedFactory<AttributeList> {
21 private:
23  AttributeBase::Map mAttributeMap;
24 
25 public:
26  using Ptr = std::shared_ptr<AttributeList>;
27 
28  AttributeList(){};
29 
30  const AttributeBase::Map &attributes() const { return mAttributeMap; };
31 
35  template <typename T>
36  typename Attribute<T>::Ptr create(const String &name, T intitialValue = T()) {
37  typename Attribute<T>::Ptr newAttr =
39  mAttributeMap[name] = newAttr;
40  return newAttr;
41  }
42 
46  template <typename T>
47  typename Attribute<T>::Ptr createDynamic(const String &name) {
48  typename Attribute<T>::Ptr newAttr =
50  mAttributeMap[name] = newAttr;
51  return newAttr;
52  }
53 
55  AttributeBase::Ptr attribute(const String &name) const {
56  auto it = mAttributeMap.find(name);
57  if (it == mAttributeMap.end())
59 
60  return it->second;
61  }
62 
64  template <typename T>
65  typename Attribute<T>::Ptr attributeTyped(const String &name) const {
66  auto attr = attribute(name);
67  auto attrPtr = std::dynamic_pointer_cast<Attribute<T>>(attr.getPtr());
68 
69  if (attrPtr == nullptr)
71 
72  return typename Attribute<T>::Ptr(attrPtr);
73  }
74 };
75 } // namespace CPS
Base class of objects having attributes to access member variables.
Definition: AttributeList.h:20
AttributeBase::Ptr attribute(const String &name) const
Return pointer to an attribute.
Definition: AttributeList.h:55
Attribute< T >::Ptr createDynamic(const String &name)
Definition: AttributeList.h:47
Attribute< T >::Ptr attributeTyped(const String &name) const
Return pointer to an attribute.
Definition: AttributeList.h:65
Attribute< T >::Ptr create(const String &name, T intitialValue=T())
Definition: AttributeList.h:36