PLCnext API Documentation 23.6.0.37
StaticString.hxx
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
4//
6#pragma once
8#include "Arp/Plc/Commons/Gds/StaticStringBase.hpp"
9#include <cstring>
10namespace Arp { namespace Plc { namespace Commons { namespace Gds
11{
12
17template <uint16 Capacity = 80, class TChar = char8>
19{
20public: // typedefs
22
23public: // construction/destruction
25 StaticString(const TChar* str = nullptr);
27 StaticString(const StaticString& arg);
30 template <uint16 M>
32 StaticString& operator=(const String& arg);
33 StaticString& operator=(const TChar* arg);
35 ~StaticString(void) = default;
36
37public: // setter/getter operations
38 uint16 GetLength(void)const;
39 bool IsEmpty(void)const;
40
41public: // operations
42 void CopyTo(StaticString& other)const;
43 String ToString(void)const;
44 void Clear(void);
45
46private: // operations
47 void Assign(const TChar* arg);
48
49private: // static operations
50 static uint16 GetLengthFrom(const TChar* arg);
51
52private: // fields
53 TChar data[Capacity + 1];
54};
55
57// inline methods of class StaticString
58
59template <uint16 N, class TChar>
62{
63 this->Clear();
64 if(str != nullptr)
65 {
66 this->Assign(str);
67 }
68}
69
70template <uint16 N, class TChar>
72{
73 this->capacity = arg.capacity;
74 this->length = arg.length;
75 std::char_traits<TChar>::copy(this->data, reinterpret_cast<TChar*>(arg.GetData()), static_cast<size_t>(this->length) + 1);
76}
77
78template <uint16 N, class TChar>
79inline void StaticString<N, TChar>::CopyTo(StaticString& other)const
80{
81 other.capacity = this->capacity;
82 other.length = this->length;
83 std::char_traits<TChar>::copy(reinterpret_cast<TChar*>(other.GetData()), this->data, static_cast<size_t>(this->length) + 1);
84}
85
86template <uint16 N, class TChar>
88{
89 this->Assign(reinterpret_cast<TChar*>(arg.GetData()));
90 return *this;
91}
92
93template <uint16 N, class TChar>
94template <uint16 M>
96{
97 this->Assign(reinterpret_cast<TChar*>(arg.GetData()));
98 return *this;
99}
100
101template <uint16 N, class TChar>
103{
104 if (arg != nullptr)
105 {
106 this->Assign(arg);
107 }
108 return *this;
109}
110
111template <uint16 N, class TChar>
112inline StaticString<N, TChar>& StaticString<N, TChar>::operator=(const String& arg)
113{
114 this->length = this->AssignStaticString(this->data, this->capacity, arg);
115 return *this;
116}
117
118template <uint16 N, class TChar>
119inline uint16 StaticString<N, TChar>::GetLength(void)const
120{
121 return this->length;
122}
123
124template <uint16 N, class TChar>
125inline uint16 StaticString<N, TChar>::GetLengthFrom(const TChar* arg)
126{
127 return static_cast<uint16>(std::char_traits<TChar>::length(arg));
128}
129
130template <uint16 N, class TChar>
131inline String StaticString<N, TChar>::ToString(void)const
132{
133 return this->ConvertStaticString(this->data);
134}
135
136template <uint16 N, class TChar>
137inline bool StaticString<N, TChar>::IsEmpty(void)const
138{
139 if (this->GetLength() == 0)
140 {
141 return true;
142 }
143 else
144 {
145 return false;
146 }
147}
148
149template <uint16 N, class TChar>
150void StaticString<N, TChar>::Assign(const TChar* arg)
151{
152 uint16 currentLength = this->GetLengthFrom(arg);
153 if (currentLength > this->capacity)
154 {
155 this->length = this->capacity;
156 }
157 else
158 {
159 this->length = currentLength;
160 }
161 std::memset(this->data, 0, sizeof(this->data));
162 std::char_traits<TChar>::copy(this->data, arg, static_cast<size_t>(this->length));
163}
164
165template <uint16 N, class TChar>
166inline void StaticString<N, TChar>::Clear(void)
167{
168 std::memset(this->data, 0, sizeof(this->data));
169 this->length = 0;
170}
171
172}}}} // end of namespace Arp::Plc::Commons::Gds
173
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:19
StaticString(const TChar *str=nullptr)
Constructs an StaticString instance.
Definition: StaticString.hxx:60
StaticString & operator=(const StaticString< Capacity, TChar > &arg)
Assignment operator.
Definition: StaticString.hxx:87
~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:32
Root namespace for the PLCnext API