PitchHut logo
patternia
Efficient pattern matching for modern C++ development.
Pitch

Patternia is a header-only, zero-overhead library designed for compile-time pattern matching in modern C++. With its rich set of built-in patterns and a fluent DSL, it enables developers to write clean and efficient match expressions without the burden of runtime overhead or external dependencies.

Description

Patternia: Compile-Time Pattern Matching for Modern C++

Patternia is a powerful header-only library designed for compile-time pattern matching in modern C++. This library provides a concise and efficient way to implement patterns that are both readable and performant, eliminating runtime overhead often associated with similar functionalities.

Patternia DSL Example

Key Features

  • Header-only: Simplifies the setup process; just include the headers to start using it.
  • Compile-Time Pattern Matching: Implementations are entirely inlined and utilize constexpr, ensuring no runtime closure or lambda overhead.
  • Rich Built-in Patterns: Supports various matching patterns, including:
    • Value patterns (value(v), ci_value(v)): Match values with case-sensitive or insensitive options for strings.
    • Relational patterns (lt(v), le(v), gt(v), ge(v), eq(v), ne(v)): Facilitate comparative matches.
    • Range patterns (between(lo, hi, closed)): Manage open or closed interval comparisons.
    • Predicate patterns (pred(f)): Accept callable objects for logical compositions like !p, p1 && p2, p1 || p2.
  • Fluent DSL: Offers a user-friendly syntax, allowing for clear and elegant match expressions by chaining .when(p >> handler).
  • Lightweight Dependency: Relies solely on the C++ standard library and avoids any external libraries, minimizing the overall project footprint unless benchmarks or tests are desired.

Usage Examples

Value Pattern

int x = 42;
auto result =
    match(x)
        .when(value(0) >> "zero")
        .when(value(42) >> "answer")
        .otherwise("other");
std::cout << result << "\n"; // → "answer"

Relational Pattern

int age = 30;
auto category =
    match(age)
        .when(lt(18) >> "minor")
        .when(between(18, 65, false) >> "adult")
        .when(ge(65) >> "senior")
        .otherwise("unknown");

Full Example

#include "ptn/patternia.hpp"
#include <iostream>
using namespace ptn;

struct User { std::string name; int age; bool active; };

int main() {
    User u{"Alice", 23, true};

    auto info = match(u)
        .when(pred([](auto& x) { return x.age >= 18 && x.active; }) >>
              [](auto& x) { return x.name + " is an active adult"; })
        .when(pred([](auto& x) { return x.age < 18; }) >>
              [](auto& x) { return x.name + " is underage"; })
        .otherwise([](auto& x) { return x.name + " is inactive"; });

    std::cout << info << "\n";
}

Installation Guidelines

To integrate Patternia into a project, include it following the recommended options for CMake. Users can opt for FetchContent to streamline the process or install from source, providing flexibility for development environments.

Future Roadmap

Patternia is continuously evolving, with planned enhancements across multiple phases that include developing advanced features such as logical and wildcard layers, compile-time optimization, and a reflection layer aimed at robust runtime dispatch.

For further information and in-depth documentation, please refer to the repository on GitHub.

0 comments

No comments yet.

Sign in to be the first to comment.