PLCnext API Documentation  22.9.0.33
RscString.hxx
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
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 
13 namespace Arp { namespace System { namespace Rsc { namespace Services
14 {
15 
19 template<int N>
20 class RscString
21 {
22 public: // typedefs
23 
24 public: // 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 
46 public: // operators
51  operator String(void) const;
52 
53 public: // operations
58  const char* CStr(void) const;
59 
64  char* CStr(void);
65 
70  String ToString(void) const;
71 
72 private: // fields
73  char buffer[N];
74 };
75 
77 // inline methods of class RscString
78 template<int N>
80 {
81  buffer[0] = '\0';
82 }
83 
84 template<int N>
85 inline 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 
94 template<int N>
95 inline RscString<N>::RscString(const String& arg)
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 
104 template<int N>
105 inline RscString<N>::operator String(void) const
106 {
107  return String(this->buffer);
108 }
109 
110 template<int N>
111 inline const char* RscString<N>::CStr(void) const
112 {
113  return this->buffer;
114 }
115 
116 template<int N>
117 inline char* RscString<N>::CStr(void)
118 {
119  return this->buffer;
120 }
121 
122 template<int N>
123 inline String RscString<N>::ToString(void) const
124 {
125  return String(this->buffer);
126 }
127 
128 template<int N>
129 inline 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
const CharType * CStr() const
Gets the character data of this string.
Definition: BasicString.hxx:1508
size_type Length() const
Returns the number of char elements in this string.
Definition: BasicString.hxx:1039
size_type Size() const
Returns the number of char elements in this string.
Definition: BasicString.hxx:1051
@ 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