ADTF
Loading...
Searching...
No Matches
string_intf.h
Go to the documentation of this file.
1
7#pragma once
8#include <adtf_utils.h>
9#include <string>
10#include <string_view>
11#include <optional>
12#include <cstring>
13
15
16namespace adtf
17{
18namespace base
19{
20namespace ant
21{
29{
30public:
32 static constexpr size_t InvalidPos = g_npos;
40 virtual tResult Set(const char* strValue) = 0;
46 virtual const char* Get() const = 0;
54 virtual size_t GetLength() const = 0;
55};
56
64template<typename T>
66{
67 static constexpr bool is_mutable = false;
74 inline static const char* GetConstChar(const T* const pValue)
75 {
77 "Unsupported type T! See documentation which types are supported."
78 "adtf_string<T> only supports std::string and adtf_util::cString types unless you specialize the "
79 "adtf_string_forward<T> for the type T!");
80 return nullptr;
81 }
82
87 inline static size_t GetCurrentLength(const T* const pValue)
88 {
90 "Unsupported type T! See documentation which types are supported."
91 "adtf_string<T> only supports std::string and adtf_util::cString types unless you specialize the "
92 "adtf_string_forward<T> for the type T!");
93
94 return size_t(0);
95 }
96
105 inline static tResult SetValue(T* const pValue, const char* strValue)
106 {
108 "Unsupported type T! See documentation which types are supported."
109 "adtf_string<T> only supports std::string and adtf_util::cString types unless you specialize the "
110 "adtf_string_forward<T> for the type T!");
111 return ERR_NOT_IMPL;
112 }
113};
114
118template<>
119struct adtf_string_forward<std::string>
120{
121 static constexpr bool is_mutable = true;
122
124 static const char* GetConstChar(const std::string* const pValue)
125 {
126 return pValue->c_str();
127 }
128
129 static size_t GetCurrentLength(const std::string* const pValue)
130 {
131 return static_cast<size_t>(pValue->length());
132 }
133
134 static tResult SetValue(std::string* const pValue, const char* strValue /* strValue */)
135 {
136 pValue->assign(strValue);
138 }
139};
140
144template<>
145struct adtf_string_forward<const std::string> : adtf_string_forward<std::string>
146{
147 static constexpr bool is_mutable = false;
148
150 static tResult SetValue(const std::string* const /* pValue */, const char* /* strValue */)
151 {
152 RETURN_ERROR(ERR_ACCESS_DENIED);
153 }
154};
155
159template<>
160struct adtf_string_forward<std::string_view>
161{
162 static constexpr bool is_mutable = false;
163
165 static const char* GetConstChar(const std::string* const pValue)
166 {
167 return pValue->c_str();
168 }
169
170 static size_t GetCurrentLength(const std::string* const pValue)
171 {
172 return static_cast<size_t>(pValue->length());
173 }
174
175 static tResult SetValue(std::string_view* const /* pValue */, const char* /* strValue */)
176 {
177 RETURN_ERROR(ERR_ACCESS_DENIED);
178 }
179};
180
184template<>
185struct adtf_string_forward<const std::string_view> : adtf_string_forward<std::string_view>
186{
188 static tResult SetValue(const std::string_view* const /* pValue */, const char* /* strValue */)
189 {
190 RETURN_ERROR(ERR_ACCESS_DENIED);
191 }
192};
193
197template<>
198struct adtf_string_forward<adtf::util::cString>
199{
200 static constexpr bool is_mutable = true;
201
202 static const char* GetConstChar(const adtf_util::cString* const pValue)
203 {
204 return pValue->GetPtr();
205 }
206 static size_t GetCurrentLength(const adtf_util::cString* const pValue)
207 {
208 return static_cast<size_t>(pValue->GetLength());
209 }
210 static tResult SetValue(adtf_util::cString* const pValue, const char* strValue /* strValue */)
211 {
212 pValue->Set(strValue);
214 }
215};
216
220template<>
221struct adtf_string_forward<const adtf::util::cString> : adtf_string_forward<adtf::util::cString>
222{
223 static constexpr bool is_mutable = false;
224
226 static tResult SetValue(const adtf_util::cString* const /* pValue */, const char* /* strValue */)
227 {
228 RETURN_ERROR(ERR_ACCESS_DENIED);
229 }
230};
231
235template<>
236struct adtf_string_forward<adtf::util::cFilename> : public adtf_string_forward<adtf::util::cString>
237{
238};
239
243template<>
244struct adtf_string_forward<const adtf::util::cFilename> : public adtf_string_forward<const adtf::util::cString>
245{
246};
247
251template<>
252struct adtf_string_forward<const char>
253{
254 static constexpr bool is_mutable = false;
255
256 static const char* GetConstChar(const char* const pValue)
257 {
258 return pValue;
259 }
260 static size_t GetCurrentLength(const char* const pValue)
261 {
262 return static_cast<size_t>(adtf_util::cStringUtil::GetLength(pValue));
263 }
264 static tResult SetValue(const char* const /* pValue */, const char* /* strValue */)
265 {
266 RETURN_ERROR(ERR_ACCESS_DENIED);
267 }
268};
269
279template<typename T>
280class adtf_string : public IString, protected adtf_string_forward<T>
281{
283 typedef adtf_string<T> self_type;
285 typedef T value_type;
287 typedef adtf_string_forward<T> base_type;
288
289private:
291 value_type* m_pAssignValue;
293 adtf_string() = delete;
296 adtf_string(const adtf_string& strValue) = delete;
299 adtf_string(adtf_string&& strValue) = delete;
303 adtf_string& operator=(const adtf_string& strValue) = delete;
307 adtf_string& operator=(adtf_string&& strValue) = delete;
308
309public:
312 explicit adtf_string(value_type* pstrAssignValue)
313 {
314 m_pAssignValue = pstrAssignValue;
315 }
316
318 {
319 m_pAssignValue = nullptr;
320 }
321 tResult Set(const char* strValue) override
322 {
323 if constexpr (std::is_const_v<T>)
324 {
325 RETURN_ERROR(ERR_NOT_SUPPORTED);
326 }
327 else if (strValue != nullptr)
328 {
329 return base_type::SetValue(m_pAssignValue, strValue);
330 }
331 else
332 {
333 RETURN_ERROR(ERR_POINTER);
334 }
335 }
336 const char* Get() const override
337 {
338 const char* strValue = "";
339 if (m_pAssignValue != nullptr)
340 {
341 strValue = base_type::GetConstChar(m_pAssignValue);
342 }
343 return strValue;
344 }
345 size_t GetLength() const override
346 {
347 if (m_pAssignValue != nullptr)
348 {
349 return base_type::GetCurrentLength(m_pAssignValue);
350 }
351 return IString::InvalidPos;
352 }
353};
354} // namespace ant
355
356namespace spider
357{
358
362class cStringLengthProxy : public ant::IString
363{
364public:
365 cStringLengthProxy(const char* strValue): m_strValue(strValue)
366 {
367 }
368
369 tResult Set(const char* /* strValue */) override
370 {
371 RETURN_ERROR(ERR_NOT_SUPPORTED);
372 }
373
374 size_t GetLength() const override
375 {
376 if (!m_nSize)
377 {
378 m_nSize = std::strlen(m_strValue);
379 }
380
381 return *m_nSize;
382 }
383
384 const char* Get() const override
385 {
386 return m_strValue;
387 }
388
389private:
390 const char* m_strValue;
391 mutable std::optional<size_t> m_nSize;
392};
393
397class cStringRedirect : public ant::IString
398{
399public:
400 cStringRedirect(std::function<tResult(const ant::IString&)> fnForward): m_fnForward(std::move(fnForward))
401 {
402 }
403
404 tResult Set(const char* strValue) override
405 {
406 return m_fnForward(cStringLengthProxy(strValue));
407 }
408
409 size_t GetLength() const override
410 {
411 return 0;
412 }
413
414 const char* Get() const override
415 {
416 return nullptr;
417 }
418
419private:
420 std::function<tResult(const ant::IString&)> m_fnForward;
421};
422
423} // namespace spider
424
425using ant::adtf_string_forward;
426using ant::adtf_string;
428using ant::IString;
429using spider::cStringRedirect;
430
431} // namespace base
432} // namespace adtf
433
465#define adtf_string_intf(__string__) \
466 static_cast<std::conditional_t< \
467 ::adtf::base::adtf_string_forward<typename std::remove_reference_t<decltype(__string__)>>::is_mutable, \
468 ::adtf::base::IString&&, const ::adtf::base::IString&&>>( \
469 ::adtf::base::adtf_string<typename std::remove_reference<decltype(__string__)>::type>(&(__string__)))
470
473#define adtf_char_intf(__const_char_ptr__) \
474 static_cast<const ::adtf::base::IString&&>(::adtf::base::adtf_string<const char>(__const_char_ptr__))
Definition for type traits helper Copyright © Audi Electronics Venture GmbH. All rights reserved.
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
Definition result.h:736
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
Definition result.h:29
#define RETURN_ERROR(code)
Return specific error code, which requires the calling function's return type to be tResult.
Definition result.h:42
Definition string_intf.h:29
static constexpr size_t InvalidPos
Invalid Position size.
Definition string_intf.h:32
virtual size_t GetLength() const =0
virtual const char * Get() const =0
virtual tResult Set(const char *strValue)=0
~adtf_string()
DTOR.
Definition string_intf.h:317
const char * Get() const override
Definition string_intf.h:336
adtf_string(value_type *pstrAssignValue)
Definition string_intf.h:312
tResult Set(const char *strValue) override
Definition string_intf.h:321
size_t GetLength() const override
Definition string_intf.h:345
Definition string_intf.h:363
size_t GetLength() const override
Definition string_intf.h:374
tResult Set(const char *) override
Definition string_intf.h:369
const char * Get() const override
Definition string_intf.h:384
size_t GetLength() const override
Definition string_intf.h:409
const char * Get() const override
Definition string_intf.h:414
tResult Set(const char *strValue) override
Definition string_intf.h:404
Namespace for all functionality of the ADTF Base SDK provided since v3.0.
Definition adtf_class_version.h:16
constexpr bool always_false
helper template to get a always false expression.
Definition adtf_base_type_traits.h:23
Namespace for the ADTF Base SDK.
Definition adtf_base_type_traits.h:13
Namespace for all functionality provided by ADTF and its SDKs.
Definition adtf_client_connector.h:14
static tResult SetValue(const adtf_util::cString *const, const char *)
Definition string_intf.h:226
static tResult SetValue(const std::string *const, const char *)
Definition string_intf.h:150
static tResult SetValue(const std::string_view *const, const char *)
Definition string_intf.h:188
static tResult SetValue(std::string *const pValue, const char *strValue)
Definition string_intf.h:134
static const char * GetConstChar(const std::string *const pValue)
Definition string_intf.h:124
static size_t GetCurrentLength(const std::string *const pValue)
Definition string_intf.h:129
static const char * GetConstChar(const std::string *const pValue)
Definition string_intf.h:165
static size_t GetCurrentLength(const std::string *const pValue)
Definition string_intf.h:170
static tResult SetValue(std::string_view *const, const char *)
Definition string_intf.h:175
Definition string_intf.h:66
static size_t GetCurrentLength(const T *const pValue)
Definition string_intf.h:87
static tResult SetValue(T *const pValue, const char *strValue)
Definition string_intf.h:105
static const char * GetConstChar(const T *const pValue)
Definition string_intf.h:74