PLCnext API Documentation 23.6.0.37
RscString.hxx
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
4//
6#pragma once
8#include "Arp/System/Core/SecureString.h"
9#include "Arp/System/Rsc/Services/Rsc.h"
10#include "Arp/System/Commons/Exceptions/Exceptions.h"
11#include <cstring>
12
13namespace Arp { namespace System { namespace Rsc { namespace Services
14{
15
19template<int N>
21{
22public: // typedefs
23
24public: // construction/destruction
26 RscString(void);
27
32 RscString(const char* pChars);
33
35 RscString(const String& arg);
36
38 RscString(const RscString& arg) = default;
39
41 RscString& operator=(const RscString& arg) = default;
42
44 ~RscString(void) = default;
45
46public: // operators
51 operator String(void) const;
52
53public: // operations
58 const char* CStr(void) const;
59
64 char* CStr(void);
65
70 String ToString(void) const;
71
72private: // fields
73 char buffer[N];
74};
75
77// inline methods of class RscString
78template<int N>
80{
81 buffer[0] = '\0';
82}
83
84template<int N>
85inline RscString<N>::RscString(const char* pChars)
86{
87 if(strlen(pChars) >= N)
88 {
89 throw ArgumentException::Create("pChars", pChars, "Input string is too long");
90 }
91 SecureStrncpy(this->buffer, N, pChars, SecureStrnlen(pChars, N));
92}
93
94template<int N>
96{
97 if(arg.Size() >= N)
98 {
99 throw ArgumentException::Create("arg", arg, "Input string is too long");
100 }
101 SecureStrncpy(this->buffer, N, arg.CStr(), arg.Length());
102}
103
104template<int N>
106{
107 return String(this->buffer);
108}
109
110template<int N>
111inline const char* RscString<N>::CStr(void) const
112{
113 return this->buffer;
114}
115
116template<int N>
117inline char* RscString<N>::CStr(void)
118{
119 return this->buffer;
120}
121
122template<int N>
124{
125 return String(this->buffer);
126}
127
128template<int N>
129inline std::ostream& operator<<(std::ostream& os, const RscString<N>& rhs)
130{
131 os << rhs.CStr();
132 return os;
133}
134
135}}}} // end of namespace Arp::System::Rsc::Services
static ArgumentException Create(const char *paramName, const T &paramValue)
Creates an ArgumentException instance using a default message text.
Definition: ArgumentException.hpp:112
Contains a static string with string lentgh up to N characters. The string has to be null terminated.
Definition: RscString.hxx:21
RscString(const char *pChars)
Constructs an RscString instance and copies the null terminated c-string in internal buffer.
Definition: RscString.hxx:85
RscString & operator=(const RscString &arg)=default
Assignment operator.
RscString(void)
Constructs an RscString instance with an emtpy string.
Definition: RscString.hxx:79
~RscString(void)=default
Destructs this instance and frees all resources.
RscString(const String &arg)
Copy constructor.
Definition: RscString.hxx:95
char * CStr(void)
Returns pointer to internal buffer.
Definition: RscString.hxx:117
RscString(const RscString &arg)=default
Copy constructor.
const char * CStr(void) const
Returns pointer to internal buffer.
Definition: RscString.hxx:111
String ToString(void) const
Converts to new instance of Arp::String
Definition: RscString.hxx:123
size_type Length() const
Returns the number of char elements in this string.
Definition: BasicString.hxx:1049
const CharType * CStr() const
Gets the character data of this string.
Definition: BasicString.hxx:1518
size_type Size() const
Returns the number of char elements in this string.
Definition: BasicString.hxx:1061
@ System
System components used by the System, Device, Plc or Io domains.
Root namespace for the PLCnext API
size_t SecureStrnlen(const char *str, size_t maxSize)
Warpper for strnlen_s
void SecureStrncpy(char *dest, size_t destMaxSize, const char *src, size_t count)
Wrapper for strncpy_s