PLCnext API Documentation  22.9.0.33
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  this->Assign(reinterpret_cast<TChar*>(arg.GetData()));
89  return *this;
90 }
91 
92 template <uint16 N, class TChar>
93 template <uint16 M>
95 {
96  this->Assign(reinterpret_cast<TChar*>(arg.GetData()));
97  return *this;
98 }
99 
100 template <uint16 N, class TChar>
102 {
103  if (arg != nullptr)
104  {
105  this->Assign(arg);
106  }
107  return *this;
108 }
109 
110 template <uint16 N, class TChar>
111 inline StaticString<N, TChar>& StaticString<N, TChar>::operator=(const String& arg)
112 {
113  this->length = this->AssignStaticString(this->data, this->capacity, arg);
114  return *this;
115 }
116 
117 template <uint16 N, class TChar>
118 inline uint16 StaticString<N, TChar>::GetLength(void)const
119 {
120  return this->length;
121 }
122 
123 template <uint16 N, class TChar>
124 inline uint16 StaticString<N, TChar>::GetLengthFrom(const TChar* arg)
125 {
126  return static_cast<uint16>(std::char_traits<TChar>::length(arg));
127 }
128 
129 template <uint16 N, class TChar>
130 inline String StaticString<N, TChar>::ToString(void)const
131 {
132  return this->ConvertStaticString(this->data);
133 }
134 
135 template <uint16 N, class TChar>
136 inline bool StaticString<N, TChar>::IsEmpty(void)const
137 {
138  if (this->GetLength() == 0)
139  {
140  return true;
141  }
142  else
143  {
144  return false;
145  }
146 }
147 
148 template <uint16 N, class TChar>
149 void StaticString<N, TChar>::Assign(const TChar* arg)
150 {
151  uint16 currentLength = this->GetLengthFrom(arg);
152  if (currentLength > this->capacity)
153  {
154  this->length = this->capacity;
155  }
156  else
157  {
158  this->length = currentLength;
159  }
160  std::memset(this->data, 0, sizeof(this->data));
161  std::char_traits<TChar>::copy(this->data, arg, static_cast<size_t>(this->length));
162 }
163 
164 template <uint16 N, class TChar>
165 inline void StaticString<N, TChar>::Clear(void)
166 {
167  std::memset(this->data, 0, sizeof(this->data));
168  this->length = 0;
169 }
170 
171 }}}} // end of namespace Arp::Plc::Commons::Gds
172 
This class is the base class used for 'IecString' and 'StaticString'.
Definition: StaticStringBase.hpp:17
This class represents the counterpart to the data type 'IecString', 'IecWString'. It's the C++ data t...
Definition: StaticString.hxx:18
StaticString(const TChar *str=nullptr)
Constructs an StaticString instance.
Definition: StaticString.hxx:59
StaticString & operator=(const StaticString< Capacity, TChar > &arg)
Assignment operator.
Definition: StaticString.hxx:86
~StaticString(void)=default
Destructs this instance and frees all resources.
Arp::BasicString< char8 > String
The Arp String class.
Definition: TypeSystem.h:27
std::uint16_t uint16
The Arp unsigned integer type of 2 byte size.
Definition: PrimitiveTypes.hpp:31
Root namespace for the PLCnext API