Skip to content

Commit

Permalink
Revue xtd::io::file_not_found_exception code
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Oct 14, 2024
1 parent 9c1f798 commit d65b57f
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 32 deletions.
1 change: 1 addition & 0 deletions src/xtd.core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,7 @@ add_sources(
src/xtd/io/directory_info.cpp
src/xtd/io/file.cpp
src/xtd/io/file_info.cpp
src/xtd/io/file_not_found_exception.cpp
src/xtd/io/file_system_info.cpp
src/xtd/io/directory_not_found_exception.cpp
src/xtd/io/drive_info.cpp
Expand Down
88 changes: 56 additions & 32 deletions src/xtd.core/include/xtd/io/file_not_found_exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,64 +32,86 @@ namespace xtd {
/// @name Public Constructors

/// @{
/// @brief Create a new instance of class file_not_found_exception
/// @brief Create a new instance of class xtd::io::file_not_found_exception
/// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically #current_stack_frame_.
/// @remarks Message is set with the default message associate to the exception.
explicit file_not_found_exception(const xtd::diagnostics::stack_frame& stack_frame = null) : io_exception(default_message(), stack_frame) {}
/// @brief Create a new instance of class file_not_found_exception
explicit file_not_found_exception(const xtd::diagnostics::stack_frame& stack_frame = null);
/// @brief Create a new instance of class xtd::io::file_not_found_exception
/// @param message Message string associate to the exception.
/// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically #current_stack_frame_.
explicit file_not_found_exception(const xtd::string& message, const xtd::diagnostics::stack_frame& stack_frame = null) : io_exception(message, stack_frame) {}
/// @brief Create a new instance of class file_not_found_exception
explicit file_not_found_exception(const xtd::optional<xtd::string>& message, const xtd::diagnostics::stack_frame& stack_frame = null);
/// @brief Create a new instance of class xtd::io::file_not_found_exception
/// @param message Message string associate to the exception.
/// @param inner_exception The exception that is the cause of the current exception.
/// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically #current_stack_frame_.
template<typename exception_t>
file_not_found_exception(const xtd::optional<xtd::string>& message, const exception_t& inner_exception, const xtd::diagnostics::stack_frame& stack_frame = null) : io_exception(message, inner_exception, stack_frame) {error_code(h_result::make_error_code(h_result::COR_E_FILENOTFOUND));}
/// @brief Create a new instance of class xtd::io::file_not_found_exception
/// @param message Message string associate to the exception.
/// @param file_name The full name of the file that cannot be found.
/// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically #current_stack_frame_.
file_not_found_exception(const xtd::optional<xtd::string>& message, const xtd::optional<xtd::string>& file_name, const xtd::diagnostics::stack_frame& stack_frame = null);
/// @brief Create a new instance of class xtd::io::file_not_found_exception
/// @param message Message string associate to the exception.
/// @param file_name The full name of the file that cannot be found.
/// @param inner_exception The exception that is the cause of the current exception.
/// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically #current_stack_frame_.
template<typename exception_t>
file_not_found_exception(const xtd::optional<xtd::string>& message, const xtd::optional<xtd::string>& file_name, const exception_t& inner_exception, const xtd::diagnostics::stack_frame& stack_frame = null) : io_exception(message, inner_exception, stack_frame), file_name_ {file_name} {error_code(h_result::make_error_code(h_result::COR_E_FILENOTFOUND));}

/// @brief Create a new instance of class xtd::io::file_not_found_exception
/// @param message Message string associate to the exception.
/// @param error Error code associate to the exception.
/// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically #current_stack_frame_.
explicit file_not_found_exception(const xtd::string& message, const std::error_code& error, const xtd::diagnostics::stack_frame& stack_frame = null) : io_exception(message, error, stack_frame) {}
/// @brief Create a new instance of class file_not_found_exception
/// @deprecated Use xtd::io::directory_not_found_exception (const xtd::string& message, const xtd::diagnostics::stack_frame& stack_frame) and manually set the property xtd::exception::error_code - Will be removed in version 0.4.0.
[[deprecated("Use xtd::io::directory_not_found_exception (const xtd::string& message, const xtd::diagnostics::stack_frame& stack_frame) and manually set the property xtd::exception::error_code - Will be removed in version 0.4.0.")]]
explicit file_not_found_exception(const xtd::string& message, const std::error_code& error, const xtd::diagnostics::stack_frame& stack_frame = null);
/// @brief Create a new instance of class xtd::io::file_not_found_exception
/// @param message Message string associate to the exception.
/// @param error Error code associate to the exception.
/// @param help_link Help link string associate to the exception.
explicit file_not_found_exception(const xtd::string& message, const std::error_code& error, const xtd::string& help_link, const xtd::diagnostics::stack_frame& stack_frame = null) : io_exception(message, error, help_link, stack_frame) {}
/// @brief Create a new instance of class file_not_found_exception
/// @deprecated Use xtd::io::directory_not_found_exception (const xtd::string& message, const xtd::diagnostics::stack_frame& stack_frame) and manually set the properties xtd::exception::error_code and xtd::exception::help_link - Will be removed in version 0.4.0.
[[deprecated("Use xtd::io::directory_not_found_exception (const xtd::string& message, const xtd::diagnostics::stack_frame& stack_frame) and manually set the properties xtd::exception::error_code and xtd::exception::help_link - Will be removed in version 0.4.0.")]]
explicit file_not_found_exception(const xtd::string& message, const std::error_code& error, const xtd::string& help_link, const xtd::diagnostics::stack_frame& stack_frame = null);
/// @brief Create a new instance of class xtd::io::file_not_found_exception
/// @param inner_exception The exception that is the cause of the current exception.
/// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically #current_stack_frame_.
/// @remarks Message is set with the default message associate to the exception.
explicit file_not_found_exception(const std::exception& inner_exception, const xtd::diagnostics::stack_frame& stack_frame = null) : io_exception(default_message(), inner_exception, stack_frame) {}
/// @brief Create a new instance of class file_not_found_exception
/// @deprecated Use xtd::io::directory_not_found_exception (const xtd::string& message, const exception_t& inner_exception, const xtd::diagnostics::stack_frame& stack_frame) - Will be removed in version 0.4.0.
[[deprecated("Use xtd::io::directory_not_found_exception (const xtd::string& message, const exception_t& inner_exception, const xtd::diagnostics::stack_frame& stack_frame) - Will be removed in version 0.4.0.")]]
explicit file_not_found_exception(const std::exception& inner_exception, const xtd::diagnostics::stack_frame& stack_frame = null);
/// @brief Create a new instance of class xtd::io::file_not_found_exception
/// @param message Message string associate to the exception.
/// @param inner_exception The exception that is the cause of the current exception.
/// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically #current_stack_frame_.
explicit file_not_found_exception(const xtd::string& message, const std::exception& inner_exception, const xtd::diagnostics::stack_frame& stack_frame = null) : io_exception(message, inner_exception, stack_frame) {}
/// @brief Create a new instance of class file_not_found_exception
/// @deprecated Use xtd::io::directory_not_found_exception (const xtd::string& message, const exception_t& inner_exception, const xtd::diagnostics::stack_frame& stack_frame) - Will be removed in version 0.4.0.
[[deprecated("Use xtd::io::directory_not_found_exception (const xtd::string& message, const exception_t& inner_exception, const xtd::diagnostics::stack_frame& stack_frame) - Will be removed in version 0.4.0.")]]
explicit file_not_found_exception(const xtd::string& message, const std::exception& inner_exception, const xtd::diagnostics::stack_frame& stack_frame = null);
/// @brief Create a new instance of class xtd::io::file_not_found_exception
/// @param message Message string associate to the exception.
/// @param inner_exception The exception that is the cause of the current exception.
/// @param error Error code associate to the exception.
/// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically #current_stack_frame_.
explicit file_not_found_exception(const xtd::string& message, const std::exception& inner_exception, const std::error_code& error, const xtd::diagnostics::stack_frame& stack_frame = null) : io_exception(message, inner_exception, error, stack_frame) {}
/// @brief Create a new instance of class file_not_found_exception
/// @deprecated Use xtd::io::directory_not_found_exception (const xtd::string& message, const exception_t& inner_exception, const xtd::diagnostics::stack_frame& stack_frame) and manually set the property xtd::exception::error_code - Will be removed in version 0.4.0.
[[deprecated("Use xtd::io::directory_not_found_exception (const xtd::string& message, const exception_t& inner_exception, const xtd::diagnostics::stack_frame& stack_frame) and manually set the property xtd::exception::error_code - Will be removed in version 0.4.0.")]]
explicit file_not_found_exception(const xtd::string& message, const std::exception& inner_exception, const std::error_code& error, const xtd::diagnostics::stack_frame& stack_frame = null);
/// @brief Create a new instance of class xtd::io::file_not_found_exception
/// @param message Message string associate to the exception.
/// @param inner_exception The exception that is the cause of the current exception.
/// @param help_link Help link string associate to the exception.
/// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically #current_stack_frame_.
explicit file_not_found_exception(const xtd::string& message, const std::exception& inner_exception, const xtd::string& help_link, const xtd::diagnostics::stack_frame& stack_frame = null) : io_exception(message, inner_exception, help_link, stack_frame) {}
/// @brief Create a new instance of class file_not_found_exception
/// @deprecated Use xtd::io::directory_not_found_exception (const xtd::string& message, const exception_t& inner_exception, const xtd::diagnostics::stack_frame& stack_frame) and manually set the property xtd::exception::help_link - Will be removed in version 0.4.0.
[[deprecated("Use xtd::io::directory_not_found_exception (const xtd::string& message, const exception_t& inner_exception, const xtd::diagnostics::stack_frame& stack_frame) and manually set the property xtd::exception::help_link - Will be removed in version 0.4.0.")]]
explicit file_not_found_exception(const xtd::string& message, const std::exception& inner_exception, const xtd::string& help_link, const xtd::diagnostics::stack_frame& stack_frame = null);
/// @brief Create a new instance of class xtd::io::file_not_found_exception
/// @param message Message string associate to the exception.
/// @param inner_exception The exception that is the cause of the current exception.
/// @param error Error code associate to the exception.
/// @param help_link Help link string associate to the exception.
/// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically #current_stack_frame_.
explicit file_not_found_exception(const xtd::string& message, const std::exception& inner_exception, const std::error_code& error, const xtd::string& help_link, const xtd::diagnostics::stack_frame& stack_frame = null) : io_exception(message, inner_exception, error, help_link, stack_frame) {}
/// @brief Create a new instance of class file_not_found_exception
/// @param message Message string associate to the exception.
/// @param file_name The full name of the file that cannot be found.
/// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically #current_stack_frame_.
file_not_found_exception(const xtd::string& message, const xtd::string& file_name, const xtd::diagnostics::stack_frame& stack_frame = null) : io_exception(message, stack_frame), file_name_ {file_name} {}
/// @brief Create a new instance of class file_not_found_exception
/// @param message Message string associate to the exception.
/// @param file_name The full name of the file that cannot be found.
/// @param inner_exception The exception that is the cause of the current exception.
/// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically #current_stack_frame_.
file_not_found_exception(const xtd::string& message, const xtd::string& file_name, const std::exception& inner_exception, const xtd::diagnostics::stack_frame& stack_frame = null) : io_exception(message, inner_exception, stack_frame), file_name_ {file_name} {}
/// @deprecated Use xtd::io::directory_not_found_exception (const xtd::string& message, const exception_t& inner_exception, const xtd::diagnostics::stack_frame& stack_frame) and manually set the properties xtd::exception::error_code and xtd::exception::help_link - Will be removed in version 0.4.0.
[[deprecated("Use xtd::io::directory_not_found_exception (const xtd::string& message, const exception_t& inner_exception, const xtd::diagnostics::stack_frame& stack_frame) and manually set the properties xtd::exception::error_code and xtd::exception::help_link - Will be removed in version 0.4.0.")]]
explicit file_not_found_exception(const xtd::string& message, const std::exception& inner_exception, const std::error_code& error, const xtd::string& help_link, const xtd::diagnostics::stack_frame& stack_frame = null);
/// @}
/// @cond
file_not_found_exception(const file_not_found_exception&) = default;
Expand All @@ -102,12 +124,14 @@ namespace xtd {
/// @brief Gets the name of the file that cannot be found.
/// @return The name of the file, or `empty` if no file name was passed to the constructor for this instance.
/// @remrks This property is read-only.
const xtd::string& file_name() const noexcept {return file_name_;}
virtual const xtd::optional<xtd::string>& file_name() const noexcept;
/// @brief Gets message associate to the exception
/// @return A string represent a massage associate to the exception
const xtd::string& message() const noexcept override;
/// @}

private:
const char* default_message() const noexcept {return "Unable to find the specified file."_t;}
xtd::string file_name_;
xtd::optional<xtd::string> file_name_;
};
}
}
59 changes: 59 additions & 0 deletions src/xtd.core/src/xtd/io/file_not_found_exception.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "../../../include/xtd/io/file_not_found_exception.h"

using namespace xtd;
using namespace xtd::diagnostics;
using namespace xtd::io;

file_not_found_exception::file_not_found_exception(const stack_frame& stack_frame) : io_exception {stack_frame} {
error_code(h_result::make_error_code(h_result::COR_E_FILENOTFOUND));
}

file_not_found_exception::file_not_found_exception(const optional<string>& message, const stack_frame& stack_frame) : io_exception {message, stack_frame} {
error_code(h_result::make_error_code(h_result::COR_E_FILENOTFOUND));
}

file_not_found_exception::file_not_found_exception(const optional<string>& message, const xtd::optional<xtd::string>& file_name, const xtd::diagnostics::stack_frame& stack_frame) : io_exception {message, stack_frame}, file_name_{file_name} {
error_code(h_result::make_error_code(h_result::COR_E_FILENOTFOUND));
}

file_not_found_exception::file_not_found_exception(const string& message, const std::error_code& error, const stack_frame& stack_frame) : io_exception(message, stack_frame) {
error_code(error);
}

file_not_found_exception::file_not_found_exception(const string& message, const std::error_code& error, const string& help_link, const stack_frame& stack_frame) : io_exception(message, stack_frame) {
error_code(error);
this->help_link(help_link);
}

file_not_found_exception::file_not_found_exception(const std::exception& inner_exception, const stack_frame& stack_frame) : io_exception(stack_frame) {
error_code(h_result::make_error_code(h_result::COR_E_FILENOTFOUND));
}

file_not_found_exception::file_not_found_exception(const string& message, const std::exception& inner_exception, const stack_frame& stack_frame) : io_exception(message, stack_frame) {
error_code(h_result::make_error_code(h_result::COR_E_FILENOTFOUND));
}

file_not_found_exception::file_not_found_exception(const string& message, const std::exception& inner_exception, const std::error_code& error, const stack_frame& stack_frame) : io_exception(message, stack_frame) {
error_code(error);
}

file_not_found_exception::file_not_found_exception(const string& message, const std::exception& inner_exception, const string& help_link, const stack_frame& stack_frame) : io_exception(message, stack_frame) {
error_code(h_result::make_error_code(h_result::COR_E_FILENOTFOUND));
this->help_link(help_link);
}

file_not_found_exception::file_not_found_exception(const string& message, const std::exception& inner_exception, const std::error_code& error, const string& help_link, const xtd::diagnostics::stack_frame& stack_frame) : io_exception(message, stack_frame) {
error_code(error);
this->help_link(help_link);
}

const xtd::optional<xtd::string>& file_not_found_exception::file_name() const noexcept {
return file_name_;
}

const xtd::string& file_not_found_exception::message() const noexcept {
if (!file_name_.has_value()) return exception::message();
thread_local static string message;
message = string::format("Could not load file '{}'. The system cannot find the file specified.", file_name_.value());
return message;
}

0 comments on commit d65b57f

Please sign in to comment.