ADTF
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
string_intf.h
Go to the documentation of this file.
1
7#pragma once
8#include <adtf_utils.h>
9#include <string>
10#include <optional>
11#include <cstring>
12
13#include "adtf_base_type_traits.h"
14
15namespace adtf
16{
17namespace base
18{
19namespace ant
20{
27 class IString
28 {
29 public:
31 static constexpr size_t InvalidPos = g_npos;
39 virtual tResult Set(const char* strValue) = 0;
45 virtual const char* Get() const = 0;
53 virtual size_t GetLength() const = 0;
54 };
55
63 template <typename T>
65 {
72 inline static const char* GetConstChar(const T* const pValue)
73 {
75 "Unsupported type T! See documentation which types are supported."
76 "adtf_string<T> only supports std::string and adtf_util::cString types unless you specialize the adtf_string_forward<T> for the type T!");
77 return nullptr;
78 }
79
84 inline static size_t GetCurrentLength(const T* const pValue)
85 {
87 "Unsupported type T! See documentation which types are supported."
88 "adtf_string<T> only supports std::string and adtf_util::cString types unless you specialize the adtf_string_forward<T> for the type T!");
89
90 return size_t(0);
91 }
92
101 inline static tResult SetValue(T* const pValue, const char* strValue)
102 {
104 "Unsupported type T! See documentation which types are supported."
105 "adtf_string<T> only supports std::string and adtf_util::cString types unless you specialize the adtf_string_forward<T> for the type T!");
106 return ERR_NOT_IMPL;
107 }
108 };
109
113 template<>
114 struct adtf_string_forward<std::string>
115 {
117 static const char* GetConstChar(const std::string* const pValue)
118 {
119 return pValue->c_str();
120 }
121
122 static size_t GetCurrentLength(const std::string* const pValue)
123 {
124 return static_cast<size_t>(pValue->length());
125 }
126
127 static tResult SetValue(std::string* const pValue, const char* strValue)
128 {
129 pValue->assign(strValue);
131 }
132 };
133
137 template <>
138 struct adtf_string_forward<adtf::util::cString>
139 {
140 static const char * GetConstChar(const adtf_util::cString* const pValue)
141 {
142 return pValue->GetPtr();
143 }
144 static size_t GetCurrentLength(const adtf_util::cString* const pValue)
145 {
146 return static_cast<size_t>(pValue->GetLength());
147 }
148 static tResult SetValue(adtf_util::cString* const pValue, const char* strValue)
149 {
150 pValue->Set(strValue);
152 }
153 };
154
159 template <>
160 struct adtf_string_forward<adtf::util::cFilename>: public adtf_string_forward<adtf::util::cString>
161 {
162 };
163
167 template <>
168 struct adtf_string_forward < const char >
169 {
170 static const char* GetConstChar(const char* const pValue)
171 {
172 return pValue;
173 }
174 static size_t GetCurrentLength(const char* const pValue)
175 {
176 return static_cast<size_t>(adtf_util::cStringUtil::GetLength(pValue));
177 }
178 static tResult SetValue(const char* const /* pValue */, const char* /* strValue */)
179 {
180 RETURN_ERROR(ERR_ACCESS_DENIED);
181 }
182 };
183
193 template <typename T>
194 class adtf_string : public IString,
195 protected adtf_string_forward<T>
196 {
200 typedef T value_type;
203
204 private:
208 adtf_string() = delete;
211 adtf_string(const adtf_string& strValue) = delete;
214 adtf_string(adtf_string&& strValue) = delete;
218 adtf_string& operator=(const adtf_string& strValue) = delete;
222 adtf_string& operator=(adtf_string&& strValue) = delete;
223 public:
226 explicit adtf_string(value_type* pstrAssignValue)
227 {
228 m_pAssignValue = pstrAssignValue;
229 }
230
232 {
233 m_pAssignValue = nullptr;
234 }
235 tResult Set(const char* strValue) override
236 {
237 if (strValue != nullptr)
238 {
239 return base_type::SetValue(m_pAssignValue, strValue);
240 }
241 else
242 {
243 RETURN_ERROR(ERR_POINTER);
244 }
245 }
246 const char* Get() const override
247 {
248 const char* strValue = "";
249 if (m_pAssignValue != nullptr)
250 {
252 }
253 return strValue;
254 }
255 size_t GetLength() const override
256 {
257 if (m_pAssignValue != nullptr)
258 {
260 }
261 return IString::InvalidPos;
262 }
263 };
264} //namespace ant
265
266namespace spider
267{
268
269class cStringLengthProxy: public ant::IString
270{
271public:
272 cStringLengthProxy(const char* strValue):
273 m_strValue(strValue)
274 {
275 }
276
277 tResult Set(const char* /* strValue */) override
278 {
279 RETURN_ERROR(ERR_NOT_SUPPORTED);
280 }
281
282 size_t GetLength() const override
283 {
284 if (!m_nSize)
285 {
286 m_nSize = std::strlen(m_strValue);
287 }
288
289 return *m_nSize;
290 }
291
292 const char* Get() const override
293 {
294 return m_strValue;
295 }
296
297private:
298 const char* m_strValue;
299 mutable std::optional<size_t> m_nSize;
300};
301
302class cStringRedirect: public ant::IString
303{
304public:
305 cStringRedirect(std::function<tResult(const ant::IString&)> fnForward):
306 m_fnForward(std::move(fnForward))
307 {
308 }
309
310 tResult Set(const char* strValue) override
311 {
312 return m_fnForward(cStringLengthProxy(strValue));
313 }
314
315 size_t GetLength() const override
316 {
317 return 0;
318 }
319
320 const char* Get() const override
321 {
322 return nullptr;
323 }
324
325private:
326 std::function<tResult(const ant::IString&)> m_fnForward;
327};
328
329}
330
331using ant::adtf_string_forward;
332using ant::adtf_string;
334using ant::IString;
335using spider::cStringRedirect;
336
337} //namespace base
338} // namespace adtf
339
371#define adtf_string_intf( __string__) ::adtf::base::adtf_string<typename std::remove_reference<decltype(__string__)>::type>(& (__string__))
372
375#define adtf_char_intf( __const_char_ptr__) ::adtf::base::adtf_string<const char>(__const_char_ptr__)
Copyright © Audi Electronics Venture GmbH.
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
#define RETURN_ERROR(code)
Return specific error code, which requires the calling function's return type to be tResult.
adtf_string()=delete
CTOR.
The IString interface provides methods for getting and setting strings through abstract interfaces.
Definition string_intf.h:28
static constexpr size_t InvalidPos
Invalid Position size.
Definition string_intf.h:31
virtual size_t GetLength() const =0
Gets the current size of the strng.
virtual tResult Set(const char *strValue)=0
Sets the given null-terminated string to the implementation.
virtual const char * Get() const =0
Gets the pointer to the current associated nullterminated-string.
adtf_string & operator=(adtf_string &&strValue)=delete
move operator
tResult Set(const char *strValue) override
Sets the given null-terminated string to the implementation.
adtf_string(adtf_string &&strValue)=delete
move CTOR
adtf_string(value_type *pstrAssignValue)
CTOR with pointer to the value of value_type.
value_type * m_pAssignValue
pointer to the value
adtf_string(const adtf_string &strValue)=delete
copy CTOR
adtf_string< T > self_type
self type
const char * Get() const override
Gets the pointer to the current associated nullterminated-string.
size_t GetLength() const override
Gets the current size of the strng.
adtf_string_forward< T > base_type
base type
adtf_string & operator=(const adtf_string &strValue)=delete
copy operator
const char * Get() const override
Gets the pointer to the current associated nullterminated-string.
tResult Set(const char *) override
Sets the given null-terminated string to the implementation.
size_t GetLength() const override
Gets the current size of the strng.
tResult Set(const char *strValue) override
Sets the given null-terminated string to the implementation.
const char * Get() const override
Gets the pointer to the current associated nullterminated-string.
size_t GetLength() const override
Gets the current size of the strng.
Namespace for all functionality of the ADTF Base SDK provided since v3.0.
constexpr bool always_false
helper template to get a always false expression.
Namespace for the ADTF Base SDK.
Namespace for entire ADTF SDK.
static tResult SetValue(std::string *const pValue, const char *strValue)
Sets the given strValue to the string type implementation of the given pValue.
static size_t GetCurrentLength(const std::string *const pValue)
Retrieves the string size of the given pValue.
static const char * GetConstChar(const std::string *const pValue)
Retrieves a const char pointer (null-terminated).
Implementation concept template for user defined adtf_string type support (see Supported types for ad...
Definition string_intf.h:65
static size_t GetCurrentLength(const T *const pValue)
Retrieves the string size of the given pValue.
Definition string_intf.h:84
static const char * GetConstChar(const T *const pValue)
Retrieves a const char pointer (null-terminated).
Definition string_intf.h:72
static tResult SetValue(T *const pValue, const char *strValue)
Sets the given strValue to the string type implementation of the given pValue.