Minh Nguyen / ArduinoJson
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ArduinoStringAdapter.hpp Source File

ArduinoStringAdapter.hpp

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