warfactoryracine/include/warfactory/contracts.hpp
StillHammer 5bdd072117 Complete 10-engine architecture with CMake build system
 All 10 engines now build successfully:
- Designer, Economy, Event, Factory, Intelligence
- Logistic, MacroEntity, Map, Operation, War

🚀 Features implemented:
- FAST_BUILD vs FULL_BUILD presets for efficient development
- Comprehensive defensive programming (sanitizers, contracts)
- 16 C++ libraries integrated via FetchContent
- GCC-compatible configuration with stack protection
- Unified CMake system across all engines

🛠️ Build commands:
- Fast: cmake -DFAST_BUILD=ON .. && make claude-workflow-fast
- Full: cmake .. && make (all sanitizers + validation)
- Single engine: make economy-engine

🔧 Development workflow optimized for daily iteration.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-19 14:19:25 +08:00

229 lines
7.8 KiB
C++

#pragma once
#include <cassert>
#include <stdexcept>
#include <string_view>
#include <source_location>
#include <cmath>
// =============================================================================
// WARFACTORY CONTRACT-BASED PROGRAMMING
// Simulation des contracts C++20 + fail-fast patterns
// =============================================================================
namespace warfactory::contracts {
// =============================================================================
// CONTRACT VIOLATION HANDLING
// =============================================================================
class ContractViolation : public std::logic_error {
public:
ContractViolation(const std::string& message,
const std::source_location& location = std::source_location::current())
: std::logic_error(format_message(message, location)) {}
private:
static std::string format_message(const std::string& message,
const std::source_location& location) {
return std::string("CONTRACT VIOLATION: ") + message +
" at " + location.file_name() + ":" + std::to_string(location.line());
}
};
// =============================================================================
// FAIL-FAST MACROS
// =============================================================================
#ifdef WARFACTORY_FAIL_FAST
// Terminate immédiatement sur condition impossible
#define WARFACTORY_UNREACHABLE() \
do { \
fprintf(stderr, "UNREACHABLE CODE at %s:%d\n", __FILE__, __LINE__); \
std::terminate(); \
} while(0)
// Crash immédiat vs corruption silencieuse
#define WARFACTORY_FAIL_FAST_IF(condition) \
do { \
if (__builtin_expect(!!(condition), 0)) { \
fprintf(stderr, "FAIL-FAST TRIGGERED: %s at %s:%d\n", #condition, __FILE__, __LINE__); \
std::terminate(); \
} \
} while(0)
#else
#define WARFACTORY_UNREACHABLE() ((void)0)
#define WARFACTORY_FAIL_FAST_IF(condition) ((void)0)
#endif
// =============================================================================
// PRECONDITIONS
// =============================================================================
#ifdef WARFACTORY_PRECONDITION_ENABLED
#define WARFACTORY_PRECONDITION(condition, message) \
do { \
if (__builtin_expect(!(condition), 0)) { \
throw warfactory::contracts::ContractViolation( \
"PRECONDITION FAILED: " #condition " - " message); \
} \
} while(0)
#define WARFACTORY_REQUIRE(condition) \
WARFACTORY_PRECONDITION(condition, "Requirement not met")
#define WARFACTORY_REQUIRE_NOT_NULL(ptr) \
WARFACTORY_PRECONDITION((ptr) != nullptr, "Pointer must not be null")
#define WARFACTORY_REQUIRE_IN_RANGE(value, min, max) \
WARFACTORY_PRECONDITION((value) >= (min) && (value) <= (max), "Value must be in range")
#else
#define WARFACTORY_PRECONDITION(condition, message) ((void)0)
#define WARFACTORY_REQUIRE(condition) ((void)0)
#define WARFACTORY_REQUIRE_NOT_NULL(ptr) ((void)0)
#define WARFACTORY_REQUIRE_IN_RANGE(value, min, max) ((void)0)
#endif
// =============================================================================
// POSTCONDITIONS
// =============================================================================
#ifdef WARFACTORY_POSTCONDITION_ENABLED
#define WARFACTORY_POSTCONDITION(condition, message) \
do { \
if (__builtin_expect(!(condition), 0)) { \
throw warfactory::contracts::ContractViolation( \
"POSTCONDITION FAILED: " #condition " - " message); \
} \
} while(0)
#define WARFACTORY_ENSURE(condition) \
WARFACTORY_POSTCONDITION(condition, "Postcondition not met")
#define WARFACTORY_ENSURE_NOT_NULL(ptr) \
WARFACTORY_POSTCONDITION((ptr) != nullptr, "Return value must not be null")
#else
#define WARFACTORY_POSTCONDITION(condition, message) ((void)0)
#define WARFACTORY_ENSURE(condition) ((void)0)
#define WARFACTORY_ENSURE_NOT_NULL(ptr) ((void)0)
#endif
// =============================================================================
// INVARIANTS
// =============================================================================
#ifdef WARFACTORY_ASSERT_ENABLED
#define WARFACTORY_ASSERT(condition, message) \
do { \
if (__builtin_expect(!(condition), 0)) { \
throw warfactory::contracts::ContractViolation( \
"ASSERTION FAILED: " #condition " - " message); \
} \
} while(0)
#define WARFACTORY_INVARIANT(condition) \
WARFACTORY_ASSERT(condition, "Class invariant violated")
#else
#define WARFACTORY_ASSERT(condition, message) ((void)0)
#define WARFACTORY_INVARIANT(condition) ((void)0)
#endif
// =============================================================================
// INPUT/OUTPUT VALIDATION
// =============================================================================
#ifdef WARFACTORY_VALIDATE_ALL_INPUTS
#define WARFACTORY_VALIDATE_INPUT(condition, message) \
WARFACTORY_PRECONDITION(condition, "INPUT VALIDATION: " message)
#define WARFACTORY_VALIDATE_JSON_SCHEMA(json, schema) \
do { \
if (!validate_json_against_schema(json, schema)) { \
throw warfactory::contracts::ContractViolation("JSON schema validation failed"); \
} \
} while(0)
#else
#define WARFACTORY_VALIDATE_INPUT(condition, message) ((void)0)
#define WARFACTORY_VALIDATE_JSON_SCHEMA(json, schema) ((void)0)
#endif
#ifdef WARFACTORY_VALIDATE_ALL_OUTPUTS
#define WARFACTORY_VALIDATE_OUTPUT(condition, message) \
WARFACTORY_POSTCONDITION(condition, "OUTPUT VALIDATION: " message)
#else
#define WARFACTORY_VALIDATE_OUTPUT(condition, message) ((void)0)
#endif
// =============================================================================
// PROPERTY-BASED TESTING MACROS
// =============================================================================
#ifdef WARFACTORY_PROPERTY_TESTING
#define WARFACTORY_PROPERTY(name, ...) \
namespace { \
struct Property_##name { \
static void test(__VA_ARGS__); \
}; \
} \
void Property_##name::test(__VA_ARGS__)
#define WARFACTORY_CHECK_PROPERTY(condition) \
do { \
if (!(condition)) { \
throw std::runtime_error("Property violation: " #condition); \
} \
} while(0)
// Propriétés communes pour le business domain
#define WARFACTORY_PROPERTY_CONSERVATION_OF_MASS(input, output) \
WARFACTORY_CHECK_PROPERTY(std::abs((input) - (output)) < 1e-6)
#define WARFACTORY_PROPERTY_FINITE_VALUES(value) \
WARFACTORY_CHECK_PROPERTY(std::isfinite(value))
#else
#define WARFACTORY_PROPERTY(name, ...)
#define WARFACTORY_CHECK_PROPERTY(condition) ((void)0)
#define WARFACTORY_PROPERTY_CONSERVATION_OF_MASS(input, output) ((void)0)
#define WARFACTORY_PROPERTY_FINITE_VALUES(value) ((void)0)
#endif
} // namespace warfactory::contracts
// =============================================================================
// HELPER FUNCTIONS FOR RUNTIME VALIDATION
// =============================================================================
namespace warfactory::validation {
// JSON Schema validation placeholder
inline bool validate_json_against_schema(const std::string& json, const std::string& schema) {
// TODO: Intégrer avec nlohmann::json validator ou équivalent
return true; // Placeholder
}
// Runtime health checks
inline void perform_health_check() {
// TODO: Vérifier état des engines, mémoire, etc.
}
// Memory usage monitoring
inline size_t get_current_memory_usage() {
// TODO: Implémenter monitoring mémoire
return 0; // Placeholder
}
} // namespace warfactory::validation