PLCnext API Documentation  21.6.0.46
StaticString.hxx
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 #include "Arp/Plc/Commons/Gds/StaticStringBase.hpp"
9 namespace Arp { namespace Plc { namespace Commons { namespace Gds
10 {
11 
16 template <uint16 Capacity = 80, class TChar = char8>
18 {
19 public: // typedefs
21 
22 public: // construction/destruction
24  StaticString(const TChar* str = nullptr);
26  StaticString(const StaticString& arg);
29  template <uint16 M>
31  StaticString& operator=(const String& arg);
32  StaticString& operator=(const TChar* arg);
34  ~StaticString(void) = default;
35 
36 public: // setter/getter operations
37  uint16 GetLength(void)const;
38  bool IsEmpty(void)const;
39 
40 public: // operations
41  void CopyTo(StaticString& other)const;
42  String ToString(void)const;
43  void Clear(void);
44 
45 private: // operations
46  void Assign(const TChar* arg);
47 
48 private: // static operations
49  static uint16 GetLengthFrom(const TChar* arg);
50 
51 private: // fields
52  TChar data[Capacity + 1];
53 };
54 
56 // inline methods of class StaticString
57 
58 template <uint16 N, class TChar>
59 inline StaticString<N, TChar>::StaticString(const TChar* str)
60  : StaticStringBase(N)
61 {
62  this->Clear();
63  if(str != nullptr)
64  {
65  this->Assign(str);
66  }
67 }
68 
69 template <uint16 N, class TChar>
71 {
72  this->capacity = arg.capacity;
73  this->length = arg.length;
74  std::char_traits<TChar>::copy(this->data, reinterpret_cast<TChar*>(arg.GetData()), static_cast<size_t>(this->length) + 1);
75 }
76 
77 template <uint16 N, class TChar>
78 inline void StaticString<N, TChar>::CopyTo(StaticString& other)const
79 {
80  other.capacity = this->capacity;
81  other.length = this->length;
82  std::char_traits<TChar>::copy(reinterpret_cast<TChar*>(other.GetData()), this->data, static_cast<size_t>(this->length) + 1);
83 }
84 
85 template <uint16 N, class TChar>
87 {
88  if (!arg.IsEmpty())
89  {
90  this->Assign(reinterpret_cast<TChar*>(arg.GetData()));
91  }
92  return *this;
93 }
94 
95 template <uint16 N, class TChar>
96 template <uint16 M>
98 {
99  this->Assign(reinterpret_cast<TChar*>(arg.GetData()));
100  return *this;
101 }
102 
103 template <uint16 N, class TChar>
105 {
106  if (arg != nullptr)
107  {
108  this->Assign(arg);
109  }
110  return *this;
111 }
112 
113 template <uint16 N, class TChar>
115 {
116  this->length = this->AssignStaticString(this->data, this->capacity, arg);
117  return *this;
118 }
119 
120 template <uint16 N, class TChar>
122 {
123  return this->length;
124 }
125 
126 template <uint16 N, class TChar>
127 inline uint16 StaticString<N, TChar>::GetLengthFrom(const TChar* arg)
128 {
129  return static_cast<uint16>(std::char_traits<TChar>::length(arg));
130 }
131 
132 template <uint16 N, class TChar>
134 {
135  return this->ConvertStaticString(this->data);
136 }
137 
138 template <uint16 N, class TChar>
139 inline bool StaticString<N, TChar>::IsEmpty(void)const
140 {
141  if (this->GetLength() == 0)
142  {
143  return true;
144  }
145  else
146  {
147  return false;
148  }
149 }
150 
151 template <uint16 N, class TChar>
152 void StaticString<N, TChar>::Assign(const TChar* arg)
153 {
154  uint16 currentLength = this->GetLengthFrom(arg);
155  if (currentLength > this->capacity)
156  {
157  this->length = this->capacity;
158  }
159  else
160  {
161  this->length = currentLength;
162  }
163  std::memset(this->data, 0, sizeof(this->data));
164  std::char_traits<TChar>::copy(this->data, arg, static_cast<size_t>(this->length));
165 }
166 
167 template <uint16 N, class TChar>
168 inline void StaticString<N, TChar>::Clear(void)
169 {
170  std::memset(this->data, 0, sizeof(this->data));
171  this->length = 0;
172 }
173 
174 }}}} // end of namespace Arp::Plc::Commons::Gds
175 
static uint16 AssignStaticString(char8 *pData, uint16 capacity, const String &arg)
Assigns an Arp::String to a StaticString(char8) object
~StaticString(void)=default
Destructs this instance and frees all resources.
This class represents the counterpart to the data type &#39;IecString&#39;, &#39;IecWString&#39;. It&#39;s the C++ data t...
Definition: StaticString.hxx:17
static String ConvertStaticString(const char8 *pData)
Converts a StaticString(char8) object into an Arp::String
StaticString & operator=(const StaticString< Capacity, TChar > &arg)
Assignment operator.
StaticString(const TChar *str=nullptr)
Constructs an StaticString instance.
Definition: StaticString.hxx:59
Root namespace for the PLCnext API
std::uint16_t uint16
The Arp unsigned integer type of 2 byte size.
Definition: PrimitiveTypes.hpp:31
This class is the base class used for &#39;IecString&#39; and &#39;StaticString&#39;.
Definition: StaticStringBase.hpp:16