DeepCover Embedded Security in IoT: Public-key Secured Data Paths

Dependencies:   MaximInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers error.h Source File

error.h

Go to the documentation of this file.
00001 // Tencent is pleased to support the open source community by making RapidJSON available.
00002 // 
00003 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
00004 //
00005 // Licensed under the MIT License (the "License"); you may not use this file except
00006 // in compliance with the License. You may obtain a copy of the License at
00007 //
00008 // http://opensource.org/licenses/MIT
00009 //
00010 // Unless required by applicable law or agreed to in writing, software distributed 
00011 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
00012 // CONDITIONS OF ANY KIND, either express or implied. See the License for the 
00013 // specific language governing permissions and limitations under the License.
00014 
00015 #ifndef RAPIDJSON_ERROR_ERROR_H_
00016 #define RAPIDJSON_ERROR_ERROR_H_
00017 
00018 #include "../rapidjson.h"
00019 
00020 #ifdef __clang__
00021 RAPIDJSON_DIAG_PUSH
00022 RAPIDJSON_DIAG_OFF(padded)
00023 #endif
00024 
00025 /*! \file error.h */
00026 
00027 /*! \defgroup RAPIDJSON_ERRORS RapidJSON error handling */
00028 
00029 ///////////////////////////////////////////////////////////////////////////////
00030 // RAPIDJSON_ERROR_CHARTYPE
00031 
00032 //! Character type of error messages.
00033 /*! \ingroup RAPIDJSON_ERRORS
00034     The default character type is \c char.
00035     On Windows, user can define this macro as \c TCHAR for supporting both
00036     unicode/non-unicode settings.
00037 */
00038 #ifndef RAPIDJSON_ERROR_CHARTYPE
00039 #define RAPIDJSON_ERROR_CHARTYPE char
00040 #endif
00041 
00042 ///////////////////////////////////////////////////////////////////////////////
00043 // RAPIDJSON_ERROR_STRING
00044 
00045 //! Macro for converting string literial to \ref RAPIDJSON_ERROR_CHARTYPE[].
00046 /*! \ingroup RAPIDJSON_ERRORS
00047     By default this conversion macro does nothing.
00048     On Windows, user can define this macro as \c _T(x) for supporting both
00049     unicode/non-unicode settings.
00050 */
00051 #ifndef RAPIDJSON_ERROR_STRING
00052 #define RAPIDJSON_ERROR_STRING(x) x
00053 #endif
00054 
00055 RAPIDJSON_NAMESPACE_BEGIN
00056 
00057 ///////////////////////////////////////////////////////////////////////////////
00058 // ParseErrorCode
00059 
00060 //! Error code of parsing.
00061 /*! \ingroup RAPIDJSON_ERRORS
00062     \see GenericReader::Parse, GenericReader::GetParseErrorCode
00063 */
00064 enum ParseErrorCode {
00065     kParseErrorNone = 0,                        //!< No error.
00066 
00067     kParseErrorDocumentEmpty,                   //!< The document is empty.
00068     kParseErrorDocumentRootNotSingular,         //!< The document root must not follow by other values.
00069 
00070     kParseErrorValueInvalid,                    //!< Invalid value.
00071 
00072     kParseErrorObjectMissName,                  //!< Missing a name for object member.
00073     kParseErrorObjectMissColon,                 //!< Missing a colon after a name of object member.
00074     kParseErrorObjectMissCommaOrCurlyBracket,   //!< Missing a comma or '}' after an object member.
00075 
00076     kParseErrorArrayMissCommaOrSquareBracket,   //!< Missing a comma or ']' after an array element.
00077 
00078     kParseErrorStringUnicodeEscapeInvalidHex,   //!< Incorrect hex digit after \\u escape in string.
00079     kParseErrorStringUnicodeSurrogateInvalid,   //!< The surrogate pair in string is invalid.
00080     kParseErrorStringEscapeInvalid,             //!< Invalid escape character in string.
00081     kParseErrorStringMissQuotationMark,         //!< Missing a closing quotation mark in string.
00082     kParseErrorStringInvalidEncoding,           //!< Invalid encoding in string.
00083 
00084     kParseErrorNumberTooBig,                    //!< Number too big to be stored in double.
00085     kParseErrorNumberMissFraction,              //!< Miss fraction part in number.
00086     kParseErrorNumberMissExponent,              //!< Miss exponent in number.
00087 
00088     kParseErrorTermination,                     //!< Parsing was terminated.
00089     kParseErrorUnspecificSyntaxError            //!< Unspecific syntax error.
00090 };
00091 
00092 //! Result of parsing (wraps ParseErrorCode)
00093 /*!
00094     \ingroup RAPIDJSON_ERRORS
00095     \code
00096         Document doc;
00097         ParseResult ok = doc.Parse("[42]");
00098         if (!ok) {
00099             fprintf(stderr, "JSON parse error: %s (%u)",
00100                     GetParseError_En(ok.Code()), ok.Offset());
00101             exit(EXIT_FAILURE);
00102         }
00103     \endcode
00104     \see GenericReader::Parse, GenericDocument::Parse
00105 */
00106 struct ParseResult {
00107 public:
00108     //! Default constructor, no error.
00109     ParseResult() : code_(kParseErrorNone), offset_(0) {}
00110     //! Constructor to set an error.
00111     ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {}
00112 
00113     //! Get the error code.
00114     ParseErrorCode Code() const { return code_; }
00115     //! Get the error offset, if \ref IsError(), 0 otherwise.
00116     size_t Offset() const { return offset_; }
00117 
00118     //! Conversion to \c bool, returns \c true, iff !\ref IsError().
00119     operator bool() const { return !IsError(); }
00120     //! Whether the result is an error.
00121     bool IsError() const { return code_ != kParseErrorNone; }
00122 
00123     bool operator==(const ParseResult& that) const { return code_ == that.code_; }
00124     bool operator==(ParseErrorCode code) const { return code_ == code; }
00125     friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; }
00126 
00127     //! Reset error code.
00128     void Clear() { Set(kParseErrorNone); }
00129     //! Update error code and offset.
00130     void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; }
00131 
00132 private:
00133     ParseErrorCode code_;
00134     size_t offset_;
00135 };
00136 
00137 //! Function pointer type of GetParseError().
00138 /*! \ingroup RAPIDJSON_ERRORS
00139 
00140     This is the prototype for \c GetParseError_X(), where \c X is a locale.
00141     User can dynamically change locale in runtime, e.g.:
00142 \code
00143     GetParseErrorFunc GetParseError = GetParseError_En; // or whatever
00144     const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode());
00145 \endcode
00146 */
00147 typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode);
00148 
00149 RAPIDJSON_NAMESPACE_END
00150 
00151 #ifdef __clang__
00152 RAPIDJSON_DIAG_POP
00153 #endif
00154 
00155 #endif // RAPIDJSON_ERROR_ERROR_H_