Minh Nguyen / ArduinoJson
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DeserializationError.hpp Source File

DeserializationError.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/Polyfills/preprocessor.hpp>
00009 #include <ArduinoJson/Polyfills/static_array.hpp>
00010 
00011 #if ARDUINOJSON_ENABLE_STD_STREAM
00012 #include <ostream>
00013 #endif
00014 
00015 namespace ARDUINOJSON_NAMESPACE {
00016 
00017 class DeserializationError {
00018   // safe bool idiom
00019   typedef void (DeserializationError::*bool_type)() const;
00020   void safeBoolHelper() const {}
00021 
00022  public:
00023   enum Code {
00024     Ok,
00025     EmptyInput,
00026     IncompleteInput,
00027     InvalidInput,
00028     NoMemory,
00029     NotSupported,
00030     TooDeep
00031   };
00032 
00033   DeserializationError() {}
00034   DeserializationError(Code c) : _code(c) {}
00035 
00036   // Compare with DeserializationError
00037   friend bool operator==(const DeserializationError& lhs,
00038                          const DeserializationError& rhs) {
00039     return lhs._code == rhs._code;
00040   }
00041   friend bool operator!=(const DeserializationError& lhs,
00042                          const DeserializationError& rhs) {
00043     return lhs._code != rhs._code;
00044   }
00045 
00046   // Compare with Code
00047   friend bool operator==(const DeserializationError& lhs, Code rhs) {
00048     return lhs._code == rhs;
00049   }
00050   friend bool operator==(Code lhs, const DeserializationError& rhs) {
00051     return lhs == rhs._code;
00052   }
00053   friend bool operator!=(const DeserializationError& lhs, Code rhs) {
00054     return lhs._code != rhs;
00055   }
00056   friend bool operator!=(Code lhs, const DeserializationError& rhs) {
00057     return lhs != rhs._code;
00058   }
00059 
00060   // Behaves like a bool
00061   operator bool_type() const {
00062     return _code != Ok ? &DeserializationError::safeBoolHelper : 0;
00063   }
00064   friend bool operator==(bool value, const DeserializationError& err) {
00065     return static_cast<bool>(err) == value;
00066   }
00067   friend bool operator==(const DeserializationError& err, bool value) {
00068     return static_cast<bool>(err) == value;
00069   }
00070   friend bool operator!=(bool value, const DeserializationError& err) {
00071     return static_cast<bool>(err) != value;
00072   }
00073   friend bool operator!=(const DeserializationError& err, bool value) {
00074     return static_cast<bool>(err) != value;
00075   }
00076 
00077   // Returns internal enum, useful for switch statement
00078   Code code() const {
00079     return _code;
00080   }
00081 
00082   const char* c_str() const {
00083     static const char* messages[] = {
00084         "Ok",       "EmptyInput",   "IncompleteInput", "InvalidInput",
00085         "NoMemory", "NotSupported", "TooDeep"};
00086     ARDUINOJSON_ASSERT(static_cast<size_t>(_code) <
00087                        sizeof(messages) / sizeof(messages[0]));
00088     return messages[_code];
00089   }
00090 
00091 #if ARDUINOJSON_ENABLE_PROGMEM
00092   const __FlashStringHelper* f_str() const {
00093     ARDUINOJSON_DEFINE_STATIC_ARRAY(char, s0, "Ok");
00094     ARDUINOJSON_DEFINE_STATIC_ARRAY(char, s1, "EmptyInput");
00095     ARDUINOJSON_DEFINE_STATIC_ARRAY(char, s2, "IncompleteInput");
00096     ARDUINOJSON_DEFINE_STATIC_ARRAY(char, s3, "InvalidInput");
00097     ARDUINOJSON_DEFINE_STATIC_ARRAY(char, s4, "NoMemory");
00098     ARDUINOJSON_DEFINE_STATIC_ARRAY(char, s5, "NotSupported");
00099     ARDUINOJSON_DEFINE_STATIC_ARRAY(char, s6, "TooDeep");
00100     ARDUINOJSON_DEFINE_STATIC_ARRAY(
00101         const char*, messages,
00102         ARDUINOJSON_EXPAND7({s0, s1, s2, s3, s4, s5, s6}));
00103     return ARDUINOJSON_READ_STATIC_ARRAY(const __FlashStringHelper*, messages,
00104                                          _code);
00105   }
00106 #endif
00107 
00108  private:
00109   Code _code;
00110 };
00111 
00112 #if ARDUINOJSON_ENABLE_STD_STREAM
00113 inline std::ostream& operator<<(std::ostream& s,
00114                                 const DeserializationError& e) {
00115   s << e.c_str();
00116   return s;
00117 }
00118 
00119 inline std::ostream& operator<<(std::ostream& s, DeserializationError::Code c) {
00120   s << DeserializationError(c).c_str();
00121   return s;
00122 }
00123 #endif
00124 
00125 }  // namespace ARDUINOJSON_NAMESPACE