1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2022-2025 Maarten L. hekkelman
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
/**
* @file error.hpp
*
* Header file containing the error codes used by libmcfp
*
*/
#include <cassert>
#include <string>
#include <system_error>
namespace mcfp
{
// we use the new system_error stuff.
/**
* @enum config_error error.hpp mcfp/error.hpp
*
* @brief A stronly typed class containing the error codes reported by @ref mcfp::config
*/
enum class config_error
{
unknown_option = 1, /**< The option requested does not exist, was not part of @ref mcfp::config::init. This error is returned by @ref mcfp::config::get */
option_does_not_accept_argument, /**< When parsing the command line arguments a value (argument) was specified for an option that should not have one */
missing_argument_for_option, /**< A option without a required argument was found while parsing the command line arguments */
option_not_specified, /**< There was not option found on the command line and no default argument was specified for the option passed in @ref mcfp::config::get */
invalid_config_file, /**< The config file is not of the expected format */
wrong_type_cast, /**< An attempt was made to ask for an option in another type than used when registering this option in @ref mcfp::config::init */
config_file_not_found /**< The specified config file was not found */
};
/**
* @brief The implementation for @ref config_category error messages
*
*/
class config_category_impl : public std::error_category
{
public:
/**
* @brief User friendly name
*
* @return const char*
*/
const char *name() const noexcept override
{
return "configuration";
}
/**
* @brief Provide the error message as a string for the error code @a ev
*
* @param ev The error code
* @return std::string
*/
std::string message(int ev) const override
{
switch (static_cast<config_error>(ev))
{
case config_error::unknown_option:
return "unknown option";
case config_error::option_does_not_accept_argument:
return "option does not accept argument";
case config_error::missing_argument_for_option:
return "missing argument for option";
case config_error::option_not_specified:
return "option was not specified";
case config_error::invalid_config_file:
return "config file contains a syntax error";
case config_error::wrong_type_cast:
return "the implementation contains a type cast error";
case config_error::config_file_not_found:
return "the specified config file was not found";
default:
assert(false);
return "unknown error code";
}
}
/**
* @brief Return whether two error codes are equivalent, always false in this case
*
*/
bool equivalent(const std::error_code & /*code*/, int /*condition*/) const noexcept override
{
return false;
}
};
/**
* @brief Return the implementation for the config_category
*
* @return std::error_category&
*/
inline std::error_category &config_category()
{
static config_category_impl instance;
return instance;
}
inline std::error_code make_error_code(config_error e)
{
return std::error_code(static_cast<int>(e), config_category());
}
inline std::error_condition make_error_condition(config_error e)
{
return std::error_condition(static_cast<int>(e), config_category());
}
} // namespace mcfp
|