{"id":47693504,"url":"https://github.com/skillman1337/modern-loki","last_synced_at":"2026-04-03T19:00:44.790Z","repository":{"id":347458743,"uuid":"1194109250","full_name":"skillman1337/modern-loki","owner":"skillman1337","description":"C++20 header-only port of Alexandrescu's Loki library — policy-based design patterns with concepts, std::variant, and standard threading","archived":false,"fork":false,"pushed_at":"2026-03-28T00:37:46.000Z","size":144,"stargazers_count":28,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2026-04-03T02:50:13.925Z","etag":null,"topics":["abstract-factory","alexandrescu","cpp","cpp20","design-patterns","header-only","loki","modern-cpp","policy-based-design","singleton","template-metaprogramming","typelist","visitor-pattern"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/skillman1337.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-03-27T23:43:39.000Z","updated_at":"2026-04-02T09:50:12.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/skillman1337/modern-loki","commit_stats":null,"previous_names":["skillman1337/modern-loki"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/skillman1337/modern-loki","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skillman1337%2Fmodern-loki","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skillman1337%2Fmodern-loki/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skillman1337%2Fmodern-loki/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skillman1337%2Fmodern-loki/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/skillman1337","download_url":"https://codeload.github.com/skillman1337/modern-loki/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skillman1337%2Fmodern-loki/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31371620,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-03T17:53:18.093Z","status":"ssl_error","status_checked_at":"2026-04-03T17:53:17.617Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["abstract-factory","alexandrescu","cpp","cpp20","design-patterns","header-only","loki","modern-cpp","policy-based-design","singleton","template-metaprogramming","typelist","visitor-pattern"],"created_at":"2026-04-02T16:05:28.264Z","updated_at":"2026-04-03T19:00:44.777Z","avatar_url":"https://github.com/skillman1337.png","language":"C++","readme":"# Modern Loki\n\n[![CI](https://github.com/skillman1337/modern-loki/actions/workflows/ci.yml/badge.svg)](https://github.com/skillman1337/modern-loki/actions/workflows/ci.yml)\n[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)\n[![C++20](https://img.shields.io/badge/C%2B%2B-20-blue.svg)](https://en.cppreference.com/w/cpp/20)\n\nC++20 port of Andrei Alexandrescu's Loki library from *Modern C++ Design*.\n\nHeader-only. Zero dependencies. Optional C++20 named module. Targets MSVC 2022 (v143), GCC 12+, Clang 15+.\n\nThis is primarily a **teaching and reference library**. It demonstrates how the policy-based design patterns from the book translate into modern C++20 idioms (concepts, variadic templates, `std::variant`, standard threading). For production code, prefer `std::unique_ptr`/`std::shared_ptr` over `smart_ptr`, `std::flat_map` (C++23) over `assoc_vector`, and mature concurrency libraries over the threading policies. Use Modern Loki when you want to study the patterns, teach generic programming, or need a policy-based component that the standard library doesn't provide (e.g. abstract factories, multi-methods, small object allocators).\n\n## What's Modernized\n\n| Original Loki | Modern Loki | C++20 Feature Used |\n|---|---|---|\n| Recursive `Typelist\u003cH, T\u003e` | Variadic `typelist\u003cTs...\u003e` | Parameter packs, fold expressions, `consteval` |\n| `SingletonHolder` with OS locks | `singleton_holder` with DCLP | `std::atomic`, policy-based `ThreadingModel` |\n| Manual `SmartPtr` policies | `smart_ptr` with move semantics | Move constructors, `operator\u003c=\u003e` |\n| `Functor` with virtual dispatch | `functor` wrapping `std::function` | `std::function`, `std::invoke`, concepts |\n| — | `functor_ref` (non-owning) | C++20 polyfill for `std::function_ref` (P0792) |\n| `AbstractFactory` with virtual MI | Tuple-based `abstract_factory` | `std::tuple`, fold expressions, concepts |\n| `Factory` with raw pointers | `factory` with `std::unique_ptr` | `std::unique_ptr`, `std::function` |\n| Acyclic Visitor with macros | CRTP `visitable\u003cDerived\u003e` | `std::variant`, `overloaded` pattern |\n| `AssocVector` | `assoc_vector` flat-map polyfill | `std::input_iterator` concept, `operator\u003c=\u003e`, heterogeneous lookup |\n| `SmallObject` with C casts + O(N) alloc | `small_object` with O(log C) dealloc | `std::byte`, `std::memcpy`, RAII chunks, binary-searched chunk index |\n| Windows-only threading (copy-pasted) | Portable threading (CRTP DRY) | `std::mutex`, `std::shared_mutex`, `std::atomic` |\n| `GenScatterHierarchy` (recursive) | Flat variadic inheritance | `public Unit\u003cTs\u003e...` pack expansion |\n| `Tuple` (custom) | Deleted — use `std::tuple` | — |\n\n## Requirements\n\n- **Compiler**: MSVC 2022 (17.x, `/std:c++20`), GCC 12+, or Clang 15+\n- **CMake**: 3.20+\n- **No external dependencies** (tests use bundled [doctest](https://github.com/doctest/doctest))\n\n## Build\n\n**MSVC** (Developer Command Prompt or any terminal with `cl.exe` on PATH):\n\n```\ncmake -B build -G \"Visual Studio 17 2022\" -A x64\ncmake --build build --config Release\n```\n\n**GCC**:\n\n```\ncmake -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=g++-13\ncmake --build build\n```\n\n**Clang**:\n\n```\ncmake -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=clang++-17\ncmake --build build\n```\n\n### C++20 Named Module (optional)\n\nModern Loki ships a named module interface (`loki.cppm`) so consumers can `import loki;` instead of `#include \u003cloki/loki.hpp\u003e`. Requires CMake 3.28+. Append `-DLOKI_BUILD_MODULE=ON` to any of the configure commands above:\n\n```\ncmake -B build -G \"Visual Studio 17 2022\" -A x64 -DLOKI_BUILD_MODULE=ON\ncmake --build build --config Release\n```\n\nLink against the `modern_loki_module` target and use `import loki;` in your source files.\n\n## Run Tests\n\n```\ncd build\nctest -C Release --output-on-failure\n```\n\nOr run the test binary directly:\n\n```\nbuild/tests/Release/loki_tests.exe          # MSVC\nbuild/tests/loki_tests                      # GCC / Clang\n```\n\n## Benchmarks\n\nThe small object allocator includes a benchmark comparing `loki::small_obj_allocator` against global `new`/`delete` and `std::pmr::monotonic_buffer_resource` across allocation sizes from 8 to 256 bytes:\n\n```\ncmake -B build -DCMAKE_BUILD_TYPE=Release -DLOKI_BUILD_BENCH=ON\ncmake --build build --config Release\nbuild/bench/Release/bench_small_obj.exe     # MSVC\nbuild/bench/bench_small_obj                 # GCC / Clang\n```\n\nThe benchmark performs 100,000 allocations and deallocations per size in random order over 10 rounds and reports median nanoseconds per operation.\n\n## Documentation\n\nEach header has a companion doc with API overview and 10 compiled, tested examples.\n\n| Header | Doc | Description |\n|--------|-----|-------------|\n| `loki/loki.hpp` | [loki.md](docs/loki.md) | Umbrella header — cross-component examples |\n| `loki/typelist.hpp` | [typelist.md](docs/typelist.md) | Variadic typelist and `tl::` metafunctions |\n| `loki/type_traits.hpp` | [type_traits.md](docs/type_traits.md) | `type_traits\u003cT\u003e`, `type2type`, `conversion` |\n| `loki/threads.hpp` | [threads.md](docs/threads.md) | Threading policies |\n| `loki/singleton.hpp` | [singleton.md](docs/singleton.md) | Policy-based singletons |\n| `loki/smart_ptr.hpp` | [smart_ptr.md](docs/smart_ptr.md) | Policy-based smart pointers |\n| `loki/functor.hpp` | [functor.md](docs/functor.md) | Type-erased callables, composition |\n| `loki/factory.hpp` | [factory.md](docs/factory.md) | Generic factory, clone factory |\n| `loki/abstract_factory.hpp` | [abstract_factory.md](docs/abstract_factory.md) | Tuple-based abstract factory |\n| `loki/visitor.hpp` | [visitor.md](docs/visitor.md) | Acyclic, cyclic, and variant visitors |\n| `loki/multi_methods.hpp` | [multi_methods.md](docs/multi_methods.md) | Static, dynamic, and variant double dispatch |\n| `loki/small_obj.hpp` | [small_obj.md](docs/small_obj.md) | Small object allocator |\n| `loki/assoc_vector.hpp` | [assoc_vector.md](docs/assoc_vector.md) | Sorted flat map |\n| `loki/hierarchy_generators.hpp` | [hierarchy_generators.md](docs/hierarchy_generators.md) | Scatter/linear hierarchy generators |\n| `loki/loki.cppm` | — | C++20 named module interface (`import loki;`) |\n\n## Design Decisions\n\n**Kept**: Policy-based `smart_ptr`, `singleton_holder`, `abstract_factory`, `small_object`, `assoc_vector`, hierarchy generators — these demonstrate design patterns that `std::` doesn't cover. The policy architecture is Loki's core contribution and remains relevant.\n\n**Merged**: `type_manip.hpp` (`type2type`, `conversion`) folded into `type_traits.hpp`.\n\n**Deleted**: `loki::tuple` (replaced by `std::tuple`), `type_manip.hpp` (merged into `type_traits.hpp`), all custom type-trait aliases that were 1:1 wrappers for `std::` equivalents, OS-specific threading primitives.\n\n**Threading model**: All four policies (`single_threaded`, `object_level_lockable`, `class_level_lockable`, `rw_lockable`) expose a uniform API via CRTP bases (`detail::plain_atomic_ops`, `detail::std_atomic_ops`) — zero duplication, fully substitutable.\n\n**Multi-methods**: `variant_dispatch` provides O(1) compile-time double dispatch via `std::visit` for `std::variant` types, complementing the existing `static_dispatcher` (RTTI-based) and `basic_dispatcher`/`fn_dispatcher` (dynamic registration).\n\n**Smart pointers**: `ref_counted` uses `std::atomic\u003cunsigned int\u003e` for thread-safe reference counting out of the box. Base class order places ownership before storage, enabling single-phase copy construction (clone result initializes storage directly, no default-construct-then-overwrite).\n\n**Abstract factory**: Uses `std::tuple\u003cstd::function\u003c...\u003e...\u003e` for compile-time product dispatch. Zero virtual inheritance, zero `dynamic_cast`, zero `void*`. Creators are frozen after construction.\n\n**`assoc_vector` heterogeneous lookup**: When the comparator defines an `is_transparent` tag (e.g. `std::less\u003c\u003e`), `find`, `contains`, `count`, `lower_bound`, `upper_bound`, `equal_range`, and `erase` accept any key type comparable via the comparator, avoiding temporary key construction.\n\n**Small object allocator**: `fixed_allocator::deallocate` uses a sorted vector of chunk start pointers for O(log C) binary-searched chunk ownership lookup, with a last-used chunk cache as a fast path. The previous O(N) linear scan is eliminated.\n\n**C++20 modules**: A named module interface (`loki.cppm`) wraps all headers via `export extern \"C++\"`, with standard library headers pre-included in the global module fragment. The header-only `#pragma once` path remains the default; the module is opt-in via `-DLOKI_BUILD_MODULE=ON`.\n\n## License\n\n[MIT](LICENSE). The original Loki library was released under the MIT License by Andrei Alexandrescu.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskillman1337%2Fmodern-loki","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fskillman1337%2Fmodern-loki","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskillman1337%2Fmodern-loki/lists"}