based

Opinionated utility library
git clone git://git.dimitrijedobrota.com/based.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

commit 3b951dc1601fa334635f36eb093cdf1ec072de41
parent 32dbe6106d0a67a6fea16db955e6b99b3dc587d2
author Dimitrije Dobrota < mail@dimitrijedobrota.com >
date Thu, 17 Apr 2025 21:41:27 +0200

registry as CRTP example

Diffstat:
M example/instrumentation.cpp | ++++++++++++++++++
M include/based/instrumentation.hpp | ++++++++++++++++++++++++++++++++++++++++++++++++++++++

2 files changed, 72 insertions(+), 0 deletions(-)


diff --git a/ example/instrumentation.cpp b/ example/instrumentation.cpp

@@ -1,7 +1,19 @@

#include <algorithm>
#include <iostream>

#include "based/instrumentation.hpp"

class reg : public based::registry<reg>
{
public:
explicit reg(int i)
: m_i(i)
{
}

int m_i;
};

int main()
{
{

@@ -23,4 +35,10 @@ int main()

[](const auto& a, const auto& b) { std::stable_sort(a, b); },
based::normalize_nlogn);
}

const reg a(0);
const reg b(1);
const reg c(2);

std::cout << based::registry<reg>::count << '\n';
}

diff --git a/ include/based/instrumentation.hpp b/ include/based/instrumentation.hpp

@@ -202,6 +202,60 @@ struct instrumented : instrumented_base

}
};

template<typename D>
class registry
{
public:
static size_t count;
static D* head;
D* prev;
D* next;

registry(registry&&) = delete;

registry& operator=(const registry&) = delete;
registry& operator=(registry&&) = delete;

private:
registry()
: prev(nullptr)
, next(head)
{
head = static_cast<D*>(this);
++count;
if (next) {
next->prev = head;
}
}

registry(const registry& /* reg */)
: registry()
{
}

~registry()
{
--count;
if (prev != nullptr) {
prev->next = next;
}
if (next != nullptr) {
next->prev = prev;
}
if (head == this) {
head = next;
}
}

friend D;
};

template<typename D>
size_t registry<D>::count(0);

template<typename D>
D* registry<D>::head(nullptr);

template<typename Function>
void count_operations(size_t i,
size_t j,