feat(cpp-object-oriented-modular-refactoring): step 3 - foundation-google-style
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
#ifndef FESA_CORE_DIAGNOSTIC_H_
|
||||
#define FESA_CORE_DIAGNOSTIC_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "fesa/core/source_identity.h"
|
||||
|
||||
namespace fesa {
|
||||
|
||||
/// @brief Distinguishes recoverable warnings from operation-stopping errors.
|
||||
enum class Severity { kWarning, kError };
|
||||
|
||||
/// @brief Carries a structured, backend-independent diagnostic record.
|
||||
struct Diagnostic {
|
||||
Severity severity;
|
||||
std::string code;
|
||||
SourceLocation location;
|
||||
std::string keyword;
|
||||
std::string entity_identity;
|
||||
std::string message;
|
||||
};
|
||||
|
||||
/// @brief Orders diagnostics by their externally visible source tuple.
|
||||
/// @param diagnostics Records to reorder in place.
|
||||
/// @note Records with identical keys retain their discovery order.
|
||||
void SortDiagnostics(std::vector<Diagnostic>& diagnostics);
|
||||
|
||||
} // namespace fesa
|
||||
|
||||
#endif // FESA_CORE_DIAGNOSTIC_H_
|
||||
@@ -1,30 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "fesa/core/source_identity.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace fesa {
|
||||
|
||||
// Distinguishes recoverable warnings from errors that stop the current operation.
|
||||
enum class Severity {
|
||||
warning,
|
||||
error
|
||||
};
|
||||
|
||||
// Carries a structured, backend-independent diagnostic record.
|
||||
struct Diagnostic {
|
||||
Severity severity;
|
||||
std::string code;
|
||||
SourceLocation location;
|
||||
std::string keyword;
|
||||
std::string entityIdentity;
|
||||
std::string message;
|
||||
};
|
||||
|
||||
// Orders diagnostics by their externally visible source tuple while retaining
|
||||
// discovery order for records with identical keys.
|
||||
void sortDiagnostics(std::vector<Diagnostic>& diagnostics);
|
||||
|
||||
} // namespace fesa
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef FESA_CORE_SOURCE_IDENTITY_H_
|
||||
#define FESA_CORE_SOURCE_IDENTITY_H_
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
namespace fesa {
|
||||
|
||||
/// @brief Identifies the input location that produced an item or diagnostic.
|
||||
struct SourceLocation {
|
||||
std::filesystem::path file;
|
||||
std::size_t line;
|
||||
};
|
||||
|
||||
/// @brief Preserves semantic and raw-text forms of a source entity identity.
|
||||
struct SourceEntityId {
|
||||
std::string instance_name;
|
||||
std::int64_t source_label;
|
||||
std::string source_label_text;
|
||||
};
|
||||
|
||||
} // namespace fesa
|
||||
|
||||
#endif // FESA_CORE_SOURCE_IDENTITY_H_
|
||||
@@ -1,23 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
namespace fesa {
|
||||
|
||||
// Identifies the physical input location that produced a model item or diagnostic.
|
||||
struct SourceLocation {
|
||||
std::filesystem::path file;
|
||||
std::size_t line;
|
||||
};
|
||||
|
||||
// Preserves both semantic and raw-text forms of an input entity identity.
|
||||
struct SourceEntityId {
|
||||
std::string instanceName;
|
||||
std::int64_t sourceLabel;
|
||||
std::string sourceLabelText;
|
||||
};
|
||||
|
||||
} // namespace fesa
|
||||
@@ -0,0 +1,114 @@
|
||||
#ifndef FESA_CORE_STATUS_H_
|
||||
#define FESA_CORE_STATUS_H_
|
||||
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "fesa/core/diagnostic.h"
|
||||
|
||||
namespace fesa {
|
||||
|
||||
/// @brief Maps failures to the stable command-line exit-code categories.
|
||||
enum class FailureCategory { kInput, kModel, kSolver, kOutput };
|
||||
|
||||
/// @brief Transports success or structured failure diagnostics.
|
||||
class Status {
|
||||
public:
|
||||
/// @brief Creates a successful status.
|
||||
/// @return A status with no failure category or diagnostics.
|
||||
static Status Ok();
|
||||
|
||||
/// @brief Creates an uncategorized failed status.
|
||||
/// @param diagnostics Structured diagnostics owned by the returned status.
|
||||
/// @return A failed status with diagnostics in deterministic source order.
|
||||
static Status Failure(std::vector<Diagnostic> diagnostics);
|
||||
|
||||
/// @brief Creates a categorized failed status.
|
||||
/// @param category Stable external failure category.
|
||||
/// @param diagnostics Structured diagnostics owned by the returned status.
|
||||
/// @return A failed status with diagnostics in deterministic source order.
|
||||
static Status Failure(FailureCategory category,
|
||||
std::vector<Diagnostic> diagnostics);
|
||||
|
||||
/// @brief Reports whether the operation succeeded.
|
||||
bool IsOk() const noexcept;
|
||||
|
||||
/// @brief Returns the optional stable failure category.
|
||||
std::optional<FailureCategory> Category() const noexcept;
|
||||
|
||||
/// @brief Returns the deterministically ordered diagnostic records.
|
||||
const std::vector<Diagnostic>& Diagnostics() const noexcept;
|
||||
|
||||
private:
|
||||
/// @brief Constructs a status from its validated invariant fields.
|
||||
Status(bool is_ok, std::optional<FailureCategory> category,
|
||||
std::vector<Diagnostic> diagnostics);
|
||||
|
||||
bool is_ok_;
|
||||
std::optional<FailureCategory> category_;
|
||||
std::vector<Diagnostic> diagnostics_;
|
||||
};
|
||||
|
||||
/// @brief Owns exactly one successful value or one failed Status.
|
||||
template <class T>
|
||||
class Result {
|
||||
public:
|
||||
/// @brief Creates a successful result that owns the supplied value.
|
||||
static Result Success(T value) {
|
||||
return Result{SuccessTag{}, std::move(value)};
|
||||
}
|
||||
|
||||
/// @brief Creates a failed result that owns a failed status.
|
||||
/// @throws std::invalid_argument if status represents success.
|
||||
static Result Failure(Status status) {
|
||||
if (status.IsOk()) {
|
||||
throw std::invalid_argument{"A failed Result requires a failed Status."};
|
||||
}
|
||||
return Result{FailureTag{}, std::move(status)};
|
||||
}
|
||||
|
||||
/// @brief Reports whether this result owns a successful value.
|
||||
bool HasValue() const noexcept { return value_.has_value(); }
|
||||
|
||||
/// @brief Returns the owned successful value.
|
||||
/// @throws std::logic_error if this result represents failure.
|
||||
T& Value() {
|
||||
if (!value_) {
|
||||
throw std::logic_error{"Result has no value."};
|
||||
}
|
||||
return *value_;
|
||||
}
|
||||
|
||||
/// @brief Returns the owned successful value.
|
||||
/// @throws std::logic_error if this result represents failure.
|
||||
const T& Value() const {
|
||||
if (!value_) {
|
||||
throw std::logic_error{"Result has no value."};
|
||||
}
|
||||
return *value_;
|
||||
}
|
||||
|
||||
/// @brief Returns the success or failure status.
|
||||
const Status& GetStatus() const noexcept { return status_; }
|
||||
|
||||
private:
|
||||
struct SuccessTag {};
|
||||
struct FailureTag {};
|
||||
|
||||
/// @brief Constructs the successful value alternative.
|
||||
Result(SuccessTag, T value)
|
||||
: value_{std::move(value)}, status_{Status::Ok()} {}
|
||||
|
||||
/// @brief Constructs the failed status alternative.
|
||||
Result(FailureTag, Status status)
|
||||
: value_{std::nullopt}, status_{std::move(status)} {}
|
||||
|
||||
std::optional<T> value_;
|
||||
Status status_;
|
||||
};
|
||||
|
||||
} // namespace fesa
|
||||
|
||||
#endif // FESA_CORE_STATUS_H_
|
||||
@@ -1,94 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "fesa/core/diagnostic.hpp"
|
||||
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace fesa {
|
||||
|
||||
// Maps a failure to the stable command-line exit-code classes defined by V0.
|
||||
enum class FailureCategory {
|
||||
input,
|
||||
model,
|
||||
solver,
|
||||
output
|
||||
};
|
||||
|
||||
// Transports success or structured diagnostics without exposing backend errors.
|
||||
class Status {
|
||||
public:
|
||||
static Status ok();
|
||||
static Status failure(std::vector<Diagnostic> diagnostics);
|
||||
static Status failure(
|
||||
FailureCategory category, std::vector<Diagnostic> diagnostics);
|
||||
|
||||
bool isOk() const noexcept;
|
||||
std::optional<FailureCategory> failureCategory() const noexcept;
|
||||
const std::vector<Diagnostic>& diagnostics() const noexcept;
|
||||
|
||||
private:
|
||||
Status(
|
||||
bool isOk,
|
||||
std::optional<FailureCategory> category,
|
||||
std::vector<Diagnostic> diagnostics);
|
||||
|
||||
bool isOk_;
|
||||
std::optional<FailureCategory> category_;
|
||||
std::vector<Diagnostic> diagnostics_;
|
||||
};
|
||||
|
||||
// Owns exactly one successful value or one failed Status.
|
||||
template<class T>
|
||||
class Result {
|
||||
public:
|
||||
static Result success(T value) {
|
||||
return Result{SuccessTag{}, std::move(value)};
|
||||
}
|
||||
|
||||
static Result failure(Status status) {
|
||||
if (status.isOk()) {
|
||||
throw std::invalid_argument{"A failed Result requires a failed Status."};
|
||||
}
|
||||
return Result{FailureTag{}, std::move(status)};
|
||||
}
|
||||
|
||||
bool hasValue() const noexcept {
|
||||
return value_.has_value();
|
||||
}
|
||||
|
||||
T& value() {
|
||||
if (!value_) {
|
||||
throw std::logic_error{"Result has no value."};
|
||||
}
|
||||
return *value_;
|
||||
}
|
||||
|
||||
const T& value() const {
|
||||
if (!value_) {
|
||||
throw std::logic_error{"Result has no value."};
|
||||
}
|
||||
return *value_;
|
||||
}
|
||||
|
||||
const Status& status() const noexcept {
|
||||
return status_;
|
||||
}
|
||||
|
||||
private:
|
||||
struct SuccessTag {};
|
||||
struct FailureTag {};
|
||||
|
||||
Result(SuccessTag, T value)
|
||||
: value_{std::move(value)}, status_{Status::ok()} {}
|
||||
|
||||
Result(FailureTag, Status status)
|
||||
: value_{std::nullopt}, status_{std::move(status)} {}
|
||||
|
||||
std::optional<T> value_;
|
||||
Status status_;
|
||||
};
|
||||
|
||||
} // namespace fesa
|
||||
Reference in New Issue
Block a user