PLCnext API Documentation 25.6.0.37
RscString.hxx
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
8#include "Arp/Base/Core/SecureString.hpp"
9#include "Arp/Base/Rsc/Commons/RscStringBase.hpp"
10#include "Arp/Base/Commons/Exceptions/ArgumentException.hpp"
11#include <cstring>
12#include <array>
13
14namespace Arp::Base::Rsc::Commons
15{
16
22template<int N>
24{
25public: // construction
26 RscString(void);
27 RscString(const char* pChars); // no explicit here, implicit conversion is desired
28 RscString(const char8u* pChars); // no explicit here, implicit conversion is desired
29 RscString(const String& arg); // no explicit here, implicit conversion is desired
30
31 // canonical construction/assignment/destruction
32 RscString(const RscString& arg);
33 RscString(RscString&& arg)noexcept;
35 RscString& operator=(RscString&& arg)noexcept;
37
38public: // operators
39 RscString& operator=(const char* pChars);
40 RscString& operator=(const char8u* pChars);
42
43public: // global friend operators (shall be defined implicit inline due to EXPORT on Windows)
44
49 friend std::ostream& operator<<(std::ostream& os, const RscString& str)
50 {
51 os << str.CStr();
52 return os;
53 }
54
59 friend std::istream& operator>>(std::istream& is, RscString& str)
60 {
61 std::string s;
62 is >> s;
63 str = s;
64 return is;
65 }
66
67public: // static getter operations
68 static constexpr size_t GetMaxLength(void);
69
70protected: // operations
71 explicit RscString(RscType stringType);
72
73private: // static fields
74 const static size_t maxLength = N;
75 const static size_t bufferSize = maxLength + 1;
76
77private: // fields
78 std::array<char, bufferSize> buffer{}; // default initialized by zeros
79};
80
82// inline methods of class RscString<N>
83
85template<int N>
87 : RscStringBase(RscType::String)
88{
89 RscStringBase::SetBufferInfo(this->buffer.data(), bufferSize);
90}
91
94template<int N>
95inline RscString<N>::RscString(RscType stringType)
96 : RscStringBase(stringType)
97{
98 RscStringBase::SetBufferInfo(this->buffer.data(), bufferSize);
99}
100
103template<int N>
105 : RscStringBase(arg)
106 , buffer(arg.buffer)
107{
108 RscStringBase::SetBufferInfo(this->buffer.data(), bufferSize, arg.GetLength());
109}
110
113template<int N>
115 : RscStringBase(arg)
116 , buffer(arg.buffer)
117{
118 RscStringBase::SetBufferInfo(this->buffer.data(), bufferSize, arg.GetLength());
119}
120
124template<int N>
126{
127 this->buffer = arg.buffer;
128 RscStringBase::SetStringType(this->GetStringType());
129 RscStringBase::SetBufferInfo(this->buffer.data(), bufferSize, arg.GetLength());
130 return *this;
131}
132
136template<int N>
138{
139 this->buffer = arg.buffer;
140 RscStringBase::SetStringType(this->GetStringType());
141 RscStringBase::SetBufferInfo(this->buffer.data(), bufferSize, arg.GetLength());
142 return *this;
143}
144
146template<int N>
147inline RscString<N>::~RscString(void) = default;
148
153template<int N>
154inline RscString<N>::RscString(const char* pChars)
155 : RscStringBase(RscType::String)
156{
157 RscStringBase::SetBufferInfo(this->buffer.data(), bufferSize, pChars);
158}
159
164template<int N>
165inline RscString<N>::RscString(const char8u* pChars)
166 : RscString(reinterpret_cast<const char*>(pChars))
167{
168}
169
172template<int N>
174 : RscStringBase(RscType::String)
175{
176 RscStringBase::SetBufferInfo(this->buffer.data(), bufferSize, arg.CStr());
177}
178
181template<int N>
182inline constexpr size_t RscString<N>::GetMaxLength()
183{
184 return maxLength;
185}
186
190template<int N>
191inline RscString<N>& RscString<N>::operator=(const char* pChars)
192{
193 this->Assign(pChars);
194 return *this;
195}
196
200template<int N>
202{
203 this->Assign(reinterpret_cast<const char*>(pChars));
204 return *this;
205}
206
210template<int N>
212{
213 this->Assign(str.CStr(), str.Length());
214 return *this;
215}
216
217} // end of namespace Arp::Base::Rsc::Commons
218
220// template specialization of RscString<N> formatter
221template<int N>
222struct fmt::formatter<Arp::Base::Rsc::Commons::RscString<N>> : public fmt::ostream_formatter {};
This class represents the Arp String. The implementation is based on std::string.
Definition: String.hpp:39
size_type Length(void) const
Returns the number of char elements in this string.
Definition: String.ipp:954
const CharType * CStr(void) const
Gets the character data of this string.
Definition: String.ipp:1395
This class is a base class of template class RscString.
Definition: RscStringBase.hpp:27
void SetStringType(RscType value)
Sets the string type of this instance.
Definition: RscStringBase.cpp:171
size_t GetLength(void) const
Gets the length of this string.
Definition: RscStringBase.cpp:49
const char * CStr(void) const
Returns a const char pointer to the internal string buffer.
Definition: RscStringBase.cpp:63
void SetBufferInfo(char *pStringBuffer, size_t bufferSize, size_t strLength=0)
Constructs an empty RscStringBase instance.
Definition: RscStringBase.cpp:93
Contains a static string with string lentgh up to N characters. The string shall be null terminated.
Definition: RscString.hxx:24
RscString(const char *pChars)
Constructs an RscString instance and copies the null terminated c-string in internal buffer.
Definition: RscString.hxx:154
RscString(const RscString &arg)
Default copy constructor.
Definition: RscString.hxx:104
RscString(const char8u *pChars)
Constructs an RscString instance and copies the null terminated c-string in internal buffer.
Definition: RscString.hxx:165
RscString & operator=(const RscString &arg)
Default copy-assignment operator.
Definition: RscString.hxx:125
RscString(void)
Constructs an empty RscString.
Definition: RscString.hxx:86
RscString & operator=(const char8u *pChars)
Assigns the UTF8 char buffer to this instance.
Definition: RscString.hxx:201
friend std::ostream & operator<<(std::ostream &os, const RscString &str)
The ostream operator is used for logging and string formatting.
Definition: RscString.hxx:49
RscString & operator=(const String &str)
Assigns the String to this instance.
Definition: RscString.hxx:211
static constexpr size_t GetMaxLength(void)
Get the maximal string length of this instance excluding string terminator.
Definition: RscString.hxx:182
RscString & operator=(const char *pChars)
Assigns the char buffer to this instance.
Definition: RscString.hxx:191
RscString(RscType stringType)
Constructor for type < see cref="RscSecureString"/>.
Definition: RscString.hxx:95
RscString & operator=(RscString &&arg) noexcept
Default move-assignment operator.
Definition: RscString.hxx:137
~RscString(void)
Default destructor.
friend std::istream & operator>>(std::istream &is, RscString &str)
The istream operator is used for string parsing.
Definition: RscString.hxx:59
RscString(RscString &&arg) noexcept
Default move constructor.
Definition: RscString.hxx:114
RscString(const String &arg)
Constructor for type String.
Definition: RscString.hxx:173
char8_t char8u
The Arp UTF8 character type of 1 byte size.
Definition: PrimitiveTypes.hpp:47
Root namespace for the PLCnext API