PLCnext API Documentation  20.0.0.24462
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& operator=(const StructNavigator& arg) = default;
49  ~StructNavigator(void) = default;
50 
51 public: // setter/getter operations
52 
53 public: // operations
54  Enumerator GetEnumerator(const byte* address);
55  Enumerator GetEnumerator(const DataInfo& dataInfo);
56  size_t GetOffset(const String& fieldName);
57 
58 private: // methods
59 
60 private: // fields
61  const TypeIdentifier& typeIdentifier;
62  ITypeNavigator& typeNavigator;
63  std::vector<DataInfo> fieldInfos;
64  std::vector<size_t> offsets;
65  bool isChanging = false;
66 };
67 
69 // inline methods of class StructNavigator
70 
71 inline StructNavigator::Enumerator StructNavigator::GetEnumerator(const byte* address)
72 {
73  return StructNavigator::Enumerator(this->fieldInfos, this->offsets, address);
74 }
75 
76 inline StructNavigator::Enumerator StructNavigator::GetEnumerator(const DataInfo& dataInfo)
77 {
78  return this->GetEnumerator(dataInfo.GetDataTag().GetDataAddress());
79 }
80 
82 // inline methods of class StructNavigator::Enumerator
83 inline size_t StructNavigator::Enumerator::GetCurrentOffset(void)const
84 {
85  if (this->index < offsets.size())
86  {
87  return offsets[this->index];
88  }
89  return 0;
90 }
91 
92 }}}} // end of namespace Arp::Plc::Commons::Meta
DataTag & GetDataTag(void)
Gets the DataTag .
Definition: DataInfo.hpp:145
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