From eeaf52ca86b9422d146e112bc62dc944fc63d899 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Tue, 6 Jan 2026 00:30:40 -0500 Subject: [PATCH] xo-arena: annex cmpresult.*pp from xo-alloc2 --- cmake/xo_arenaConfig.cmake.in | 1 + include/xo/arena/cmpresult.hpp | 87 ++++++++++++++++++++++++++++++++++ src/arena/CMakeLists.txt | 2 + src/arena/cmpresult.cpp | 38 +++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 include/xo/arena/cmpresult.hpp create mode 100644 src/arena/cmpresult.cpp diff --git a/cmake/xo_arenaConfig.cmake.in b/cmake/xo_arenaConfig.cmake.in index 3b32fb0..1700fb8 100644 --- a/cmake/xo_arenaConfig.cmake.in +++ b/cmake/xo_arenaConfig.cmake.in @@ -7,6 +7,7 @@ include(CMakeFindDependencyMacro) # in CMakeLists.txt # find_dependency(xo_reflectutil) +find_dependency(indentlog) include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake") check_required_components("@PROJECT_NAME@") diff --git a/include/xo/arena/cmpresult.hpp b/include/xo/arena/cmpresult.hpp new file mode 100644 index 0000000..0a9a911 --- /dev/null +++ b/include/xo/arena/cmpresult.hpp @@ -0,0 +1,87 @@ +/** @file cmpresult.hpp +* + * @author Roland Conybeare, Dec 2025 + **/ + +#pragma once + +#include +#include + +namespace xo { + namespace mm { + enum class comparison : int32_t { + invalid = -1, + comparable = 0, + incomparable = +1, + }; + + extern const char * comparison2str(comparison x); + + inline std::ostream & + operator<<(std::ostream & os, comparison x) { + os << comparison2str(x); + return os; + } + + /** Result of a generic comparison operation + **/ + struct cmpresult { + /** @defgroup mm-cmpresult-ctors cmpresult ctors **/ + ///@{ + cmpresult() : err_{comparison::invalid}, cmp_{0} {} + cmpresult(comparison err, std::int16_t cmp) : err_{err}, cmp_{cmp} {} + + static cmpresult incomparable() { return cmpresult(comparison::incomparable, 0); } + static cmpresult lesser() { return cmpresult(comparison::comparable, -1); } + static cmpresult equal() { return cmpresult(comparison::comparable, 0); } + static cmpresult greater() { return cmpresult(comparison::comparable, +1); } + template + static cmpresult from_cmp(T && x, T && y) { + if (x < y) + return cmpresult::lesser(); + else if (x == y) + return cmpresult::equal(); + else + return cmpresult::greater(); + } + + ///@} + + /** @defgroup mm-cmpresult-methods cmpresult methods **/ + ///@{ + + /** print to stream **/ + void display(std::ostream & os) const; + + bool is_lesser() const { + return (err_ == comparison::comparable) && (cmp_ < 0); + } + bool is_equal() const { + return (err_ == comparison::comparable) && (cmp_ == 0); + } + ///@} + + /** @defgroup mm-cmpresult-instance-vars cmpresult instance vars **/ + ///@{ + /** -1 -> invalid (sentinel) + * 0 -> comparable + * +1 -> incomparable (e.g. iterators from different arenas) + **/ + comparison err_ = comparison::invalid; + /** <0 -> lesser; 0 -> equal, >0 -> greater **/ + std::int16_t cmp_ = 0; + ///@} + }; + + inline std::ostream & operator<<(std::ostream & os, + const cmpresult & x) + { + x.display(os); + return os; + } + + } /*namespace mm*/ +} /*namespace xo*/ + +/* end cmpresult.hpp */ diff --git a/src/arena/CMakeLists.txt b/src/arena/CMakeLists.txt index 345e9cc..4097723 100644 --- a/src/arena/CMakeLists.txt +++ b/src/arena/CMakeLists.txt @@ -5,6 +5,7 @@ set(SELF_SRCS AllocError.cpp AllocInfo.cpp ArenaConfig.cpp + cmpresult.cpp ) xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 ${SELF_SRCS}) @@ -17,5 +18,6 @@ xo_install_include_tree3(include/xo/arena) # xo-arena/cmake/xo_arenaConfig.cmake.in xo_dependency(${SELF_LIB} xo_reflectutil) +xo_dependency(${SELF_LIB} indentlog) # end src/CMakeLists.txt diff --git a/src/arena/cmpresult.cpp b/src/arena/cmpresult.cpp new file mode 100644 index 0000000..cd7c299 --- /dev/null +++ b/src/arena/cmpresult.cpp @@ -0,0 +1,38 @@ +/** @file cmpresult.cpp + * + * @author Roland Conybeare, Dec 2025 + **/ + +#include "cmpresult.hpp" +#include +#include + +namespace xo { + namespace mm { + const char * + comparison2str(comparison x) + { + switch (x) { + case comparison::invalid: + break; + case comparison::comparable: + return "cmp"; + case comparison::incomparable: + return "!cmp"; + } + + return "?comparison"; + } + + void + cmpresult::display(std::ostream & os) const + { + os << ""; + } + } /*namespace mm*/ +} /*namespace xo*/ + +/* end cmpresult.cpp */