xo-umbrella2/xo-unit
2026-02-27 19:38:53 +11:00
..
.github/workflows Add 'xo-unit/' from commit 'b531e382c2' 2025-05-10 21:29:43 -05:00
cmake xo-cmake: setup to make share target available via cmake install 2026-02-27 19:38:53 +11:00
docs xo-tokenizer: docs + error-handling improvement 2025-06-23 23:08:12 -05:00
example xo-unit: dep fixes needed for submodule builds (in examples) 2025-06-22 16:15:12 -05:00
include/xo/unit xo-unit: bugfix: need ppdetail_atomic guard 2025-09-22 21:12:13 -04:00
pkgs Add 'xo-unit/' from commit 'b531e382c2' 2025-05-10 21:29:43 -05:00
utest pretty printing -- copmlete for xo::ast::GeneralizedExpression 2025-07-19 11:47:03 -05:00
.gitignore Add 'xo-unit/' from commit 'b531e382c2' 2025-05-10 21:29:43 -05:00
CMakeLists.txt nix build: xo-unit: build + install docs + examples 2025-09-22 12:24:57 -04:00
flake.nix Add 'xo-unit/' from commit 'b531e382c2' 2025-05-10 21:29:43 -05:00
LICENSE Add 'xo-unit/' from commit 'b531e382c2' 2025-05-10 21:29:43 -05:00
README.md Add 'xo-unit/' from commit 'b531e382c2' 2025-05-10 21:29:43 -05:00

unit library

Provides compile-time dimension checking and scaling.

Similar in spirit to boost::units, but:

  1. streamlined: assumes modern (c++20) support
  2. supports fractional dimensions (rational powers)

Documentation

Example

#include "xo/unit/quantity.hpp"
#include "xo/unit/quantity_iostream.hpp"

namespace q = xo::qty::qty;
namespace u = xo::qty::u;

constexpr auto t = q::minutes(2);
constexpr auto d = q::kilometers(2.5);

constexpr auto t2 = t*t;   // unit will be min^-2
constexpr auto a = d / t2; // unit will be km.min^-2

// convert to m.s^-2
constexpr quantity<u::meter / (u::second * u::second)> a2 = a;

//constexpr quantity<u::meter> a3 = a;  // dimension mismatch, will not compile

// get dimensionless scale value
double x = a2.scale();

Getting Started

See full install instructions for other installation strategies.

build + install xo-cmake dependency

Installs a few cmake ingredients, along with build assistant xo-build for XO projects such as this one.

build + install other XO dependencies

$ xo-build --clone --configure --build --install xo-flatstring
$ xo-build --clone --configure --build --install xo-ratio

Note: can use -n to dry-run here

copy xo-unit repository locally

$ xo-build --clone xo-unit

or equivalently

$ git clone https://github.com/rconybea/xo-unit

build + install xo-unit

$ xo-build --configure --build --install xo-unit

or equivalently:

$ PREFIX=/usr/local  # or wherever you prefer
$ cmake -DCMAKE_INSTALL_PREFIX=${PREFIX} -S xo-unit -B xo-unit/.build
$ cmake --build xo-unit/.build -j
$ cmake --install xo-unit/.build

build documentation

$ cd xo-unit
$ cmake -DCMAKE_INSTALL_PREFIX=${PREFIX}  --build .build -- sphinx

When this completes, can point local browser to xo-unit/.build/docs/sphinx/index.html.

build for unit test coverage

(Note that unit tests involve additional dependencies):

$ xo-build --clone --configure --build --install xo-indentlog
$ xo-build --clone --configure --build --install xo-randomgen
$ cmake -DCMAKE_BUILD_TYPE=coverage -DCMAKE_INSTALL_PREFIX=$PREFIX -DENABLE_TESTING=1 xo-unit/.build-ccov
$ cmake --build xo-unit/.build-ccov

run coverage-enabled unit tests:

$ cmake --build xo-unit/.build-ccov -- test

generate html+text coverage report:

$ xo-unit/.build-ccov/gen-ccov

To see coverage, can point local browser to xo-unit/.build-ccov/ccov/html/index.html

LSP support

$ cd xo-unit
$ ln -s .build/compile_commands.json  # lsp will look for compile_commands.json in the root of the source tree