Minh Nguyen / ArduinoJson
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers String.hpp Source File

String.hpp

00001 // ArduinoJson - arduinojson.org
00002 // Copyright Benoit Blanchon 2014-2021
00003 // MIT License
00004 
00005 #pragma once
00006 
00007 #include <ArduinoJson/Strings/ConstRamStringAdapter.hpp>
00008 #include <ArduinoJson/Strings/IsString.hpp>
00009 #include <ArduinoJson/Strings/StoragePolicy.hpp>
00010 
00011 namespace ARDUINOJSON_NAMESPACE {
00012 
00013 class String {
00014  public:
00015   String() : _data(0), _isStatic(true) {}
00016   String(const char* data, bool isStaticData = true)
00017       : _data(data), _isStatic(isStaticData) {}
00018 
00019   const char* c_str() const {
00020     return _data;
00021   }
00022 
00023   bool isNull() const {
00024     return !_data;
00025   }
00026 
00027   bool isStatic() const {
00028     return _isStatic;
00029   }
00030 
00031   friend bool operator==(String lhs, String rhs) {
00032     if (lhs._data == rhs._data)
00033       return true;
00034     if (!lhs._data)
00035       return false;
00036     if (!rhs._data)
00037       return false;
00038     return strcmp(lhs._data, rhs._data) == 0;
00039   }
00040 
00041   friend bool operator!=(String lhs, String rhs) {
00042     if (lhs._data == rhs._data)
00043       return false;
00044     if (!lhs._data)
00045       return true;
00046     if (!rhs._data)
00047       return true;
00048     return strcmp(lhs._data, rhs._data) != 0;
00049   }
00050 
00051  private:
00052   const char* _data;
00053   bool _isStatic;
00054 };
00055 
00056 class StringAdapter : public RamStringAdapter {
00057  public:
00058   StringAdapter(const String& str)
00059       : RamStringAdapter(str.c_str()), _isStatic(str.isStatic()) {}
00060 
00061   bool isStatic() const {
00062     return _isStatic;
00063   }
00064 
00065   typedef storage_policies::decide_at_runtime storage_policy;
00066 
00067  private:
00068   bool _isStatic;
00069 };
00070 
00071 template <>
00072 struct IsString<String> : true_type {};
00073 
00074 inline StringAdapter adaptString(const String& str) {
00075   return StringAdapter(str);
00076 }
00077 }  // namespace ARDUINOJSON_NAMESPACE