|
| 1 | +/* |
| 2 | + * Copyright 2022 Open Source Robotics Foundation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | +#ifndef SDF_USD_USDERROR_HH_ |
| 18 | +#define SDF_USD_USDERROR_HH_ |
| 19 | + |
| 20 | +#include <optional> |
| 21 | +#include <string> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +#include <ignition/utils/ImplPtr.hh> |
| 25 | + |
| 26 | +#include <sdf/Error.hh> |
| 27 | +#include <sdf/usd/Export.hh> |
| 28 | +#include <sdf/config.hh> |
| 29 | + |
| 30 | +namespace sdf |
| 31 | +{ |
| 32 | + // Inline bracket to help doxygen filtering. |
| 33 | + inline namespace SDF_VERSION_NAMESPACE |
| 34 | + { |
| 35 | + namespace usd |
| 36 | + { |
| 37 | + // |
| 38 | + /// \enum ErrorCode |
| 39 | + /// \brief Set of error codes. Usually one or more errors are returned in |
| 40 | + /// an Errors vector. The collection of Errors should be taken as a whole, |
| 41 | + /// where an error toward the beginning of the vector can inform errors |
| 42 | + /// toward the end of the vector. |
| 43 | + /// \sa Errors |
| 44 | + enum class UsdErrorCode |
| 45 | + { |
| 46 | + // \brief No error |
| 47 | + NONE = 0, |
| 48 | + |
| 49 | + /// \brief A wrapped SDF error. |
| 50 | + SDF_ERROR, |
| 51 | + |
| 52 | + /// \brief Parsing of SDF object to a USD object failed. |
| 53 | + SDF_TO_USD_PARSING_ERROR, |
| 54 | + |
| 55 | + /// \brief The pxr::SdfPath does not point to a valid USD prim. |
| 56 | + INVALID_PRIM_PATH, |
| 57 | + |
| 58 | + /// \brief A pxr API was not able to be applied to a USD prim. |
| 59 | + FAILED_PRIM_API_APPLY, |
| 60 | + }; |
| 61 | + |
| 62 | + class IGNITION_SDFORMAT_USD_VISIBLE UsdError |
| 63 | + { |
| 64 | + /// \brief Default constructor |
| 65 | + public: UsdError(); |
| 66 | + |
| 67 | + /// \brief Constructor. |
| 68 | + /// \param[in] _code The error code. If the error code is SDF_ERROR, the |
| 69 | + /// constructor that takes an sdf::Error object should be used instead. |
| 70 | + /// \param[in] _message A description of the error. |
| 71 | + /// \sa ErrorCode. |
| 72 | + public: UsdError(const UsdErrorCode _code, const std::string &_message); |
| 73 | + |
| 74 | + /// \brief Constructor. |
| 75 | + /// \param[in] _code The error code. If the error code is SDF_ERROR, the |
| 76 | + /// constructor that takes an sdf::Error object should be used instead. |
| 77 | + /// \param[in] _message A description of the error. |
| 78 | + /// \param[in] _filePath The file path that is related to this error. |
| 79 | + /// \sa ErrorCode. |
| 80 | + public: UsdError(const UsdErrorCode _code, const std::string &_message, |
| 81 | + const std::string &_filePath); |
| 82 | + |
| 83 | + /// \brief Constructor. |
| 84 | + /// \param[in] _code The error code. If the error code is SDF_ERROR, the |
| 85 | + /// constructor that takes an sdf::Error object should be used instead. |
| 86 | + /// \param[in] _message A description of the error. |
| 87 | + /// \param[in] _filePath The file path that is related to this error. |
| 88 | + /// \param[in] _lineNumber The line number in the provided file path where |
| 89 | + /// this error was raised. |
| 90 | + /// \sa ErrorCode. |
| 91 | + public: UsdError(const UsdErrorCode _code, const std::string &_message, |
| 92 | + const std::string &_filePath, int _lineNumber); |
| 93 | + |
| 94 | + /// \brief Constructor. |
| 95 | + /// \param[in] _sdf_error Wrap an sdf error. |
| 96 | + /// \sa ErrorCode. |
| 97 | + public: explicit UsdError(const sdf::Error &_sdfError); |
| 98 | + |
| 99 | + /// \brief Get the error code. |
| 100 | + /// \return An error code. |
| 101 | + /// \sa ErrorCode. |
| 102 | + public: UsdErrorCode Code() const; |
| 103 | + |
| 104 | + /// \brief Get the error message, which is a description of the error. |
| 105 | + /// \return Error message. |
| 106 | + public: const std::string &Message() const; |
| 107 | + |
| 108 | + /// \brief Get the file path associated with this error. |
| 109 | + /// \return Returns the path of the file that this error is related to, |
| 110 | + /// nullopt otherwise. |
| 111 | + public: const std::optional<std::string> &FilePath() const; |
| 112 | + |
| 113 | + /// \brief Sets the file path that is associated with this error. |
| 114 | + /// \param[in] _filePath The file path that is related to this error. (e.g. |
| 115 | + /// /tmp/test_file.usd) |
| 116 | + public: void SetFilePath(const std::string &_filePath); |
| 117 | + |
| 118 | + /// \brief Get the line number associated with this error. |
| 119 | + /// \return Returns the line number. nullopt otherwise. |
| 120 | + public: std::optional<int> LineNumber() const; |
| 121 | + |
| 122 | + /// \brief Sets the line number that is associated with this error. |
| 123 | + /// \param[in] _lineNumber The line number that is related to this error. |
| 124 | + public: void SetLineNumber(int _lineNumber); |
| 125 | + |
| 126 | + /// \brief Get the underlying sdf error. |
| 127 | + /// \return The underlying sdf error or nullopt otherwise. |
| 128 | + public: std::optional<sdf::Error> SdfError() const; |
| 129 | + |
| 130 | + /// \brief Safe bool conversion. |
| 131 | + /// \return True if this Error's Code() != NONE. In otherwords, this is |
| 132 | + /// true when there is an error. |
| 133 | + public: explicit operator bool() const; |
| 134 | + |
| 135 | + /// \brief Compare this Error to a boolean value. |
| 136 | + /// \return True if the boolean evaluation of this Error equals _value. |
| 137 | + /// If _value == false, then true is returned when this Error's Code() is |
| 138 | + /// equal to NONE and false is returned otherwise. If _value == true, |
| 139 | + /// then true is returned when this Error's Code() is not equal to NONE |
| 140 | + /// and false is returned otherwise. |
| 141 | + /// \sa explicit operator bool() const |
| 142 | + public: bool operator==(const bool _value) const; |
| 143 | + |
| 144 | + /// \brief Output operator for an error. |
| 145 | + /// \param[in,out] _out The output stream. |
| 146 | + /// \param[in] _err The error to output. |
| 147 | + /// \return Reference to the given output stream |
| 148 | + public: friend IGNITION_SDFORMAT_VISIBLE std::ostream &operator<<( |
| 149 | + std::ostream &_out, const sdf::usd::UsdError &_err); |
| 150 | + |
| 151 | + /// \brief Private data pointer. |
| 152 | + IGN_UTILS_IMPL_PTR(dataPtr) |
| 153 | + }; |
| 154 | + |
| 155 | + using UsdErrors = std::vector<UsdError>; |
| 156 | + |
| 157 | + } // namespace usd |
| 158 | + } // namespace SDF_VERSION_NAMESPACE |
| 159 | +} // namespace sdf |
| 160 | + |
| 161 | +#ifdef _WIN32 |
| 162 | +#pragma warning(pop) |
| 163 | +#endif |
| 164 | + |
| 165 | +#endif |
0 commit comments