ADTF
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 
15 namespace adtf
16 {
17 namespace base
18 {
19 namespace 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  {
74  static_assert(penguin::detail::always_false<T>,
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  }
84  inline static size_t GetCurrentLength(const T* const pValue)
85  {
86  static_assert(penguin::detail::always_false<T>,
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  }
101  inline static tResult SetValue(T* const pValue, const char* strValue)
102  {
103  static_assert(penguin::detail::always_false<T>,
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  }
122  static size_t GetCurrentLength(const std::string* const pValue)
123  {
124  return static_cast<size_t>(pValue->length());
125  }
127  static tResult SetValue(std::string* const pValue, const char* strValue)
128  {
129  pValue->assign(strValue);
131  }
132  };
133 
137  template <>
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  }
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 
266 namespace spider
267 {
268 
270 {
271 public:
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 
297 private:
298  const char* m_strValue;
299  mutable std::optional<size_t> m_nSize;
300 };
301 
303 {
304 public:
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 
325 private:
326  std::function<tResult(const ant::IString&)> m_fnForward;
327 };
328 
329 }
330 
331 using ant::adtf_string_forward;
332 using ant::adtf_string;
334 using ant::IString;
335 using 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.
The IString interface provides methods for getting and setting strings through abstract interfaces.
Definition: string_intf.h:28
virtual const char * Get() const =0
Gets the pointer to the current associated nullterminated-string.
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.
Wrapping template for a rvalue reference of an IString interface for the type T (see Supported types ...
Definition: string_intf.h:196
tResult Set(const char *strValue) override
Sets the given null-terminated string to the implementation.
Definition: string_intf.h:235
adtf_string(adtf_string &&strValue)=delete
move CTOR
adtf_string(value_type *pstrAssignValue)
CTOR with pointer to the value of value_type.
Definition: string_intf.h:226
value_type * m_pAssignValue
pointer to the value
Definition: string_intf.h:206
adtf_string(const adtf_string &strValue)=delete
copy CTOR
adtf_string< T > self_type
self type
Definition: string_intf.h:198
size_t GetLength() const override
Gets the current size of the strng.
Definition: string_intf.h:255
adtf_string & operator=(const adtf_string &strValue)=delete
copy operator
adtf_string_forward< T > base_type
base type
Definition: string_intf.h:202
const char * Get() const override
Gets the pointer to the current associated nullterminated-string.
Definition: string_intf.h:246
adtf_string & operator=(adtf_string &&strValue)=delete
move operator
tResult Set(const char *) override
Sets the given null-terminated string to the implementation.
Definition: string_intf.h:277
size_t GetLength() const override
Gets the current size of the strng.
Definition: string_intf.h:282
const char * Get() const override
Gets the pointer to the current associated nullterminated-string.
Definition: string_intf.h:292
tResult Set(const char *strValue) override
Sets the given null-terminated string to the implementation.
Definition: string_intf.h:310
size_t GetLength() const override
Gets the current size of the strng.
Definition: string_intf.h:315
const char * Get() const override
Gets the pointer to the current associated nullterminated-string.
Definition: string_intf.h:320
string_base< cStackString > cString
cString implementation for a stack string which works on stack if string is lower than A_UTILS_DEFAUL...
Definition: string.h:2784
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.
Definition: string_intf.h:127
static size_t GetCurrentLength(const std::string *const pValue)
Retrieves the string size of the given pValue.
Definition: string_intf.h:122
static const char * GetConstChar(const std::string *const pValue)
Retrieves a const char pointer (null-terminated).
Definition: string_intf.h:117
Implementation concept template for user defined adtf_string type support (see Supported types for ad...
Definition: string_intf.h:65
static const char * GetConstChar(const T *const pValue)
Retrieves a const char pointer (null-terminated).
Definition: string_intf.h:72
static size_t GetCurrentLength(const T *const pValue)
Retrieves the string size of the given pValue.
Definition: string_intf.h:84
static tResult SetValue(T *const pValue, const char *strValue)
Sets the given strValue to the string type implementation of the given pValue.
Definition: string_intf.h:101