PLCnext API Documentation 23.3.0.32
StaticString.hxx
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
4//
6#pragma once
8#include "Arp/Plc/Commons/Gds/StaticStringBase.hpp"
9namespace Arp { namespace Plc { namespace Commons { namespace Gds
10{
11
16template <uint16 Capacity = 80, class TChar = char8>
18{
19public: // typedefs
21
22public: // 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
36public: // setter/getter operations
37 uint16 GetLength(void)const;
38 bool IsEmpty(void)const;
39
40public: // operations
41 void CopyTo(StaticString& other)const;
42 String ToString(void)const;
43 void Clear(void);
44
45private: // operations
46 void Assign(const TChar* arg);
47
48private: // static operations
49 static uint16 GetLengthFrom(const TChar* arg);
50
51private: // fields
52 TChar data[Capacity + 1];
53};
54
56// inline methods of class StaticString
57
58template <uint16 N, class TChar>
61{
62 this->Clear();
63 if(str != nullptr)
64 {
65 this->Assign(str);
66 }
67}
68
69template <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
77template <uint16 N, class TChar>
78inline 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
85template <uint16 N, class TChar>
87{
88 this->Assign(reinterpret_cast<TChar*>(arg.GetData()));
89 return *this;
90}
91
92template <uint16 N, class TChar>
93template <uint16 M>
95{
96 this->Assign(reinterpret_cast<TChar*>(arg.GetData()));
97 return *this;
98}
99
100template <uint16 N, class TChar>
102{
103 if (arg != nullptr)
104 {
105 this->Assign(arg);
106 }
107 return *this;
108}
109
110template <uint16 N, class TChar>
111inline 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
117template <uint16 N, class TChar>
118inline uint16 StaticString<N, TChar>::GetLength(void)const
119{
120 return this->length;
121}
122
123template <uint16 N, class TChar>
124inline uint16 StaticString<N, TChar>::GetLengthFrom(const TChar* arg)
125{
126 return static_cast<uint16>(std::char_traits<TChar>::length(arg));
127}
128
129template <uint16 N, class TChar>
130inline String StaticString<N, TChar>::ToString(void)const
131{
132 return this->ConvertStaticString(this->data);
133}
134
135template <uint16 N, class TChar>
136inline 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
148template <uint16 N, class TChar>
149void 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
164template <uint16 N, class TChar>
165inline 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