Minh Nguyen / ArduinoJson
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers StdStringAdapter.hpp Source File

StdStringAdapter.hpp

00001 // ArduinoJson - arduinojson.org
00002 // Copyright Benoit Blanchon 2014-2021
00003 // MIT License
00004 
00005 #pragma once
00006 
00007 #include <ArduinoJson/Namespace.hpp>
00008 #include <ArduinoJson/Strings/IsString.hpp>
00009 #include <ArduinoJson/Strings/StoragePolicy.hpp>
00010 
00011 #include <string>
00012 
00013 namespace ARDUINOJSON_NAMESPACE {
00014 
00015 template <typename TString>
00016 class StdStringAdapter {
00017  public:
00018   StdStringAdapter(const TString& str) : _str(&str) {}
00019 
00020   void copyTo(char* p, size_t n) const {
00021     memcpy(p, _str->c_str(), n);
00022   }
00023 
00024   bool isNull() const {
00025     return false;
00026   }
00027 
00028   int compare(const char* other) const {
00029     if (!other)
00030       return 1;
00031     return _str->compare(other);
00032   }
00033 
00034   bool equals(const char* expected) const {
00035     if (!expected)
00036       return false;
00037     return *_str == expected;
00038   }
00039 
00040   size_t size() const {
00041     return _str->size();
00042   }
00043 
00044   const char* begin() const {
00045     return _str->c_str();
00046   }
00047 
00048   typedef storage_policies::store_by_copy storage_policy;
00049 
00050  private:
00051   const TString* _str;
00052 };
00053 
00054 template <typename TCharTraits, typename TAllocator>
00055 struct IsString<std::basic_string<char, TCharTraits, TAllocator> > : true_type {
00056 };
00057 
00058 template <typename TCharTraits, typename TAllocator>
00059 inline StdStringAdapter<std::basic_string<char, TCharTraits, TAllocator> >
00060 adaptString(const std::basic_string<char, TCharTraits, TAllocator>& str) {
00061   return StdStringAdapter<std::basic_string<char, TCharTraits, TAllocator> >(
00062       str);
00063 }
00064 
00065 }  // namespace ARDUINOJSON_NAMESPACE