Skip to content

infinity-MSFS/infinity-cpp-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Infinity C++ SDK

Rust-inspired lightweight utilities for modern C++ projects.

  • Option: an optional value type.
  • Result: a success-or-error value type.
  • Match (C++23): simple pattern-like matching macros.
  • Vec helpers (C++23): convenience functions to build vectors.

C++


Supported standards

  • C++14 mode: define INFINITY_LANGUAGE_STD_CPP14
  • C++23 mode: define INFINITY_LANGUAGE_STD_CPP23
  • Optional: define INFINITY_NO_EXCEPTIONS (no exceptions for WASM)

Only one of the language macros should be defined at a time.


Features

  • Option

    • Queries: is_some(), is_none()
    • Accessors: unwrap(), unwrap_or(default)
    • Combinators: map(F)
    • C++14 extras: expect(msg), and_then(F), operator*, operator->
    • C++23 helpers: None sentinel, Some(T) factory
  • Result

    • Queries: is_ok(), is_err()
    • Accessors: unwrap(), unwrap_err()
    • Combinators: map(F)
    • C++14 extras: map_err(F), and_then(F), ok(), err() to Option
    • Utility: collect_results aggregates Container<Result<T,E>> into Result<Container<T>,E>
  • Match (C++23)

    • MATCH(expr, { SomeCase(x) {...} NoneCase {...} }) for Option
    • MATCH(expr, { OkCase(x) {...} ErrCase(e) {...} }) for Result
  • Vec (C++23)

    • vec_of(a, b, c, ...)
    • vec_repeat(value, n)

Installation

This is a header-only library. Copy the include/infinity/ folder into your project and include the headers you need.

Alternatively, add the repository as a submodule and include headers by path.


Build

A minimal CMake project is provided for running the examples in main.cpp.

  1. Select the mode in main.cpp by defining one of:
    • INFINITY_LANGUAGE_STD_CPP23 (default in main.cpp)
    • INFINITY_LANGUAGE_STD_CPP14
    • Optionally INFINITY_NO_EXCEPTIONS
  2. Configure and build:
    • cmake -S . -B build
    • cmake --build build

Quick start (C++23)

  • Option
    using namespace Infinity;
    Option<int> o = Some(5);
    if (o.is_some()) {
      int v = o.unwrap();
    }
    auto m = o.map([](int x){ return x * 2; });
    int d = Option<int>{None}.unwrap_or(42);
  • Result

      using namespace Infinity;
      Result<int, std::string> r(123);
      if (r.is_ok()) {
        int v = r.unwrap();
      }
      auto rm = r.map([](int x){ return x + 1; });
    
      Result<int, std::string> e(std::string("err"));
      if (e.is_err()) {
        std::string s = e.unwrap_err();
      }
  • collect_results

      using namespace Infinity;
      std::vector<Result<int, std::string>> v = { Result<int, std::string>(1), Result<int, std::string>(2) };
      auto cr = collect_results<int, std::string>(v); // Ok(vector<int>) or Err(std::string)
  • Match

      using namespace Infinity;
      MATCH(o, {
        SomeCase(v) { /* use v */ break; }
        NoneCase { /* none */ break; }
      });
      MATCH(r, {
        OkCase(v) { /* ok */ break; }
        ErrCase(e) { /* err */ break; }
      });
  • Vec

      using namespace Infinity;
      auto v1 = vec_of(1,2,3);
      auto v2 = vec_repeat(std::string("x"), 3);

Quick start (C++14)

  • Option

      Infinity::Option<int> o = Infinity::Some(5);
      o.map([](int x){ return x * 2; });
      o.and_then([](int x){ return Infinity::Some(x + 1); });
      o.unwrap_or(42);
  • Result

      auto r = Infinity::Result<int, std::string>::ok(7);
      r.map([](int x){ return x + 1; });
      r.map_err([](const std::string& e){ return e + "!"; });
      r.and_then([](int x){ return Infinity::Result<int, std::string>::ok(x * 2); });

Notes

  • Keep MATCH blocks with explicit break statements to avoid fallthrough in macro-generated loops.
  • collect_results preserves the input container type and reserves capacity when possible (e.g., std::vector).
  • In C++23 mode, Some and None are inline helpers. In C++14 mode, use Infinity::Some() and the Infinity::None constant.

About

Rust abstractions for C++

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published