PLCnext API Documentation  20.6.0.30321
StructNavigator.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 #include "Arp/System/Core/IEnumerator.hxx"
9 #include "Arp/Plc/Commons/Meta/DataInfo.hpp"
10 #include "Arp/Plc/Commons/Meta/ITypeNavigator.hpp"
11 
12 //TODO(OR): move to Arp/Plc/Commons/Internal/Meta
13 
14 namespace Arp { namespace Plc { namespace Commons { namespace Meta
15 {
16 
18 {
19 public: // typedefs
20  typedef std::shared_ptr<StructNavigator> Ptr;
21 
22 public: // Nested types
23 
24  class Enumerator : public IEnumerator<const DataInfo&>
25  {
26  public: // construction
27  Enumerator(std::vector<DataInfo>& fieldInfos, std::vector<size_t>& offsets, const byte* startAddress);
28 
29  public: // overridden operations
30  bool MoveNext(void)override;
31  const DataInfo& GetCurrent(void)override;
32  size_t GetCurrentOffset()const;
33 
34  private: // fields
35  std::vector<size_t>& offsets;
36  std::vector<DataInfo>& fieldInfos;
37  const byte* startAddress;
38  size_t index = 0;
39  };
40 
41 public: // construction/destruction
43  StructNavigator(const TypeIdentifier& typeId, ITypeNavigator& typeNavigator, bool isChanging);
45  StructNavigator(const StructNavigator& arg) = default;
47  StructNavigator(StructNavigator&& arg) = default;
49  StructNavigator& operator=(const StructNavigator& arg) = default;
51  StructNavigator& operator=(StructNavigator&& arg) = default;
53  ~StructNavigator(void) = default;
54 
55 public: // setter/getter operations
56 
57 public: // operations
58  Enumerator GetEnumerator(const byte* address);
59  Enumerator GetEnumerator(const DataInfo& dataInfo);
60  size_t GetOffset(const String& fieldName);
61 
62 private: // methods
63 
64 private: // fields
65  const TypeIdentifier& typeIdentifier;
66  ITypeNavigator& typeNavigator;
67  std::vector<DataInfo> fieldInfos;
68  std::vector<size_t> offsets;
69  bool isChanging = false;
70 };
71 
73 // inline methods of class StructNavigator
74 
75 inline StructNavigator::Enumerator StructNavigator::GetEnumerator(const byte* address)
76 {
77  return StructNavigator::Enumerator(this->fieldInfos, this->offsets, address);
78 }
79 
80 inline StructNavigator::Enumerator StructNavigator::GetEnumerator(const DataInfo& dataInfo)
81 {
82  return this->GetEnumerator(dataInfo.GetDataTag().GetDataAddress());
83 }
84 
86 // inline methods of class StructNavigator::Enumerator
87 inline size_t StructNavigator::Enumerator::GetCurrentOffset(void)const
88 {
89  if (this->index < offsets.size())
90  {
91  return offsets[this->index];
92  }
93  return 0;
94 }
95 
96 }}}} // end of namespace Arp::Plc::Commons::Meta
DataTag & GetDataTag(void)
Gets the DataTag .
Definition: DataInfo.hpp:144
Declares the interface of the enumerator pattern, which is leaned on .NET enumerator idiom...
Definition: IEnumerator.hxx:47
Definition: StructNavigator.hpp:17
Includes name and namespace of type object.
Definition: TypeIdentifier.hpp:21
~StructNavigator(void)=default
Destructs this instance and frees all resources.
const DataInfo & GetCurrent(void) override
Gets the element at the current position.
bool MoveNext(void) override
Moves this enumerator to the next position.
const byte * GetDataAddress(void) const
For internal use only.
Definition: DataTag.hpp:194
Root namespace for the PLCnext API
Provides detailed informations for the respective data object.
Definition: DataInfo.hpp:31
IEnumerator & operator=(const IEnumerator &arg)=default
Assigns an IEnumerator instance.
Definition: ITypeNavigator.hpp:16
StructNavigator(const TypeIdentifier &typeId, ITypeNavigator &typeNavigator, bool isChanging)
Constructs an StructNavigator instance.
unsigned char byte
The Arp character type.
Definition: PrimitiveTypes.hpp:23