PLCnext API Documentation  20.6.0.30321
DualDomain.hxx
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 #include <algorithm>
9 
10 // TODO(OR): move this to Arp/Plc/Commons/Internal/Domain
11 
12 namespace Arp { namespace Plc { namespace Commons { namespace Domain
13 {
14 
15 template<class T>
17 {
18 public: // typedefs
19 
20 public: // construction/destruction
22  template <class ...Args>
23  DualDomain(Args& ... args);
25  DualDomain(const DualDomain& arg) = default;
27  DualDomain& operator=(const DualDomain& arg) = default;
29  ~DualDomain(void) = default;
30 
31 public: // setter/getter operations
32  T& Get(bool backgroundDomain = false);
33  const T& Get(bool backgroundDomain = false)const;
34 
35 public: // operations
36  void SwapDomains(bool simulate = false);
37 
38 private: // fields
39  T domain1;
40  T domain2;
41  T* pCurrentDomain = nullptr;
42  T* pBackgroundDomain = nullptr;
43 };
44 
46 // inline methods of class DualDomain
47 
48 template<class T>
49 template <class ...Args>
50 inline DualDomain<T>::DualDomain(Args& ... args)
51  : domain1(*this, args...)
52  , domain2(*this, args...)
53 {
54  this->pCurrentDomain = &this->domain1;
55  this->pBackgroundDomain = &this->domain2;
56 }
57 
58 template<class T>
59 inline T& DualDomain<T>::Get(bool backgroundDomain)
60 {
61  return backgroundDomain ? *this->pBackgroundDomain : *this->pCurrentDomain;
62 }
63 
64 template<class T>
65 inline const T& DualDomain<T>::Get(bool backgroundDomain)const
66 {
67  return backgroundDomain ? *this->pBackgroundDomain : *this->pCurrentDomain;
68 }
69 
70 template<class T>
71 inline void DualDomain<T>::SwapDomains(bool simulate)
72 {
73  if (simulate)
74  {
75  // we just swap the backgrounddomain by itself, which causes no changes but will measure the time correctly
76  std::swap(this->pBackgroundDomain, this->pBackgroundDomain);
77  }
78  else
79  {
80  std::swap(this->pCurrentDomain, this->pBackgroundDomain);
81  }
82 }
83 
84 }}}} // end of namespace Arp::Plc::Commons::Domain
~DualDomain(void)=default
Destructs this instance and frees all resources.
Definition: DualDomain.hxx:16
Root namespace for the PLCnext API
DualDomain(Args &... args)
Constructs an DualDomain instance.
Definition: DualDomain.hxx:50
DualDomain & operator=(const DualDomain &arg)=default
Assignment operator.