PLCnext API Documentation  21.0.0.35466
RscString.hxx
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Rsc/Services/Rsc.h"
8 #include "Arp/System/Commons/Exceptions/Exceptions.h"
9 #include <cstring>
10 
11 namespace Arp { namespace System { namespace Rsc { namespace Services
12 {
13 
17 template<int N>
18 class RscString
19 {
20 public: // typedefs
21 
22 public: // construction/destruction
24  RscString(void);
25 
30  RscString(const char* pChars);
31 
33  RscString(const String& arg);
34 
36  RscString(const RscString& arg) = default;
37 
39  RscString& operator=(const RscString& arg) = default;
40 
42  ~RscString(void) = default;
43 
44 public: // operators
49  operator String(void) const;
50 
51 public: // operations
56  const char* CStr(void) const;
57 
62  char* CStr(void);
63 
68  String ToString(void) const;
69 
70 private: // fields
71  char buffer[N];
72 };
73 
75 // inline methods of class RscString
76 template<int N>
78 {
79  buffer[0] = '\0';
80 }
81 
82 template<int N>
83 inline RscString<N>::RscString(const char* pChars)
84 {
85  if(strlen(pChars) >= N)
86  {
87  throw ArgumentException::Create("pChars", pChars, "Input string is too long");
88  }
89  strncpy(this->buffer, pChars, N);
90 }
91 
92 template<int N>
93 inline RscString<N>::RscString(const String& arg)
94 {
95  if(arg.Size() >= N)
96  {
97  throw ArgumentException::Create("arg", arg, "Input string is too long");
98  }
99  strncpy(this->buffer, arg.CStr(), N);
100 }
101 
102 template<int N>
103 inline RscString<N>::operator String(void) const
104 {
105  return String(this->buffer);
106 }
107 
108 template<int N>
109 inline const char* RscString<N>::CStr(void) const
110 {
111  return this->buffer;
112 }
113 
114 template<int N>
115 inline char* RscString<N>::CStr(void)
116 {
117  return this->buffer;
118 }
119 
120 template<int N>
121 inline String RscString<N>::ToString(void) const
122 {
123  return String(this->buffer);
124 }
125 
126 template<int N>
127 inline std::ostream& operator<<(std::ostream& os, const RscString<N>& rhs)
128 {
129  os << rhs.CStr();
130  return os;
131 }
132 
133 }}}} // 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
size_type Size() const
Returns the number of char elements in this string.
Definition: BasicString.hxx:1074
const char * CStr(void) const
Returns pointer to internal buffer.
Definition: RscString.hxx:109
String with undefined format. Deprecated with remoting version 4 used by Rsc.
RscString & operator=(const RscString &arg)=default
Assignment operator.
RscString(void)
Constructs an RscString instance with an emtpy string.
Definition: RscString.hxx:77
const CharType * CStr() const
Gets the character data of this string.
Definition: BasicString.hxx:1507
~RscString(void)=default
Destructs this instance and frees all resources.
String ToString(void) const
Converts to new instance of Arp::String
Definition: RscString.hxx:121
Root namespace for the PLCnext API
System components used by the System, Device, Plc or Io domains.
Contains a static string with string lentgh up to N characters. The string has to be null terminated...
Definition: RscString.hxx:18