TsukiNi22/ai-utils / skills/cpp/cpp-class
cpp-class
VerifiedGenerate C++ (C++20) .hpp/.cpp files and class architectures in Tsukini's style (Xartania header, indented include guards, INCLUDE/CLASS sections, Pre-Function/Function/Operator/Constructor/Destructor blocks, libutils attributes or standard [[...]]), with empty bodies only (never the logic), then register the .cpp in the CMakeLists.txt. Also creates the main.cpp entry point in the same style. Use whenever the user asks to create, scaffold or add a C++ class, a main / entry point, interface (I*), abstract class (A*), template, struct/Type or Define header, module or set of .hpp/.cpp files.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 10.5 KB |
| examples/ASocket.cpp | other | 11.7 KB |
| examples/ASocket.hpp | other | 7.5 KB |
| examples/ArgParserType.hpp | other | 3.6 KB |
| examples/ISocket.hpp | other | 5.4 KB |
| examples/IdHandler.hpp | other | 8.3 KB |
| examples/NetworkDefine.hpp | other | 1.1 KB |
| examples/Scheduler.cpp | other | 2.5 KB |
| examples/Scheduler.hpp | other | 4.7 KB |
| reference/layout.md | markdown | 17.5 KB |
| requires.txt | markdown | 32 B |
| scripts/cmake_add.pyexecutable | script | 4.7 KB |
| scripts/detect_libutils.shexecutable | script | 1.5 KB |
| scripts/fonts/ansi_shadow.flf | other | 11.9 KB |
| scripts/header.pyexecutable | script | 4.2 KB |
| scripts/list_attributes.shexecutable | script | 1.6 KB |
| templates/abstract.hpp | other | 1.5 KB |
| templates/class.cpp | other | 262 B |
| templates/class.hpp | other | 1.5 KB |
| templates/define.hpp | other | 463 B |
| templates/family.hpp | other | 417 B |
| templates/interface.hpp | other | 1.2 KB |
| templates/main-std.cpp | other | 474 B |
| templates/main.cpp | other | 753 B |
| templates/template.hpp | other | 1.6 KB |
| templates/type.hpp | other | 823 B |
examples/IdHandler.hpp
View on GitHub/**************************************************************\
██╗ ██╗ █████╗ ██████╗ ████████╗ █████╗ ███╗ ██╗██╗ █████╗
╚██╗██╔╝██╔══██╗██╔══██╗╚══██╔══╝██╔══██╗████╗ ██║██║██╔══██╗
╚███╔╝ ███████║██████╔╝ ██║ ███████║██╔██╗ ██║██║███████║
██╔██╗ ██╔══██║██╔══██╗ ██║ ██╔══██║██║╚██╗██║██║██╔══██║
██╔╝ ██╗██║ ██║██║ ██║ ██║ ██║ ██║██║ ╚████║██║██║ ██║
╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═══╝╚═╝╚═╝ ╚═╝
Edition:
## @date 01/10/2026 by @author Tsukini
File Name:
## @file IdHandler.hpp
File Description:
## Handle id allocation
\**************************************************************/
#ifndef IDHANDLER_H
#define IDHANDLER_H
//----------------------------------------------------------------//
/* INCLUDE */
/* type */
#include "../exception/custom/FatalException.hpp" // utils::exception::FatalException
#include "../exception/basic/ErrorException.hpp" // utils::exception::ErrorException
#include "../exception/ExceptionDefine.hpp" // utils::exception::InternalCode
#include "../attribute/Attribute.hpp" // _hot, _cold, _unlikely, _likely, _nodiscard
#include <type_traits> // std::is_integral_v
#include <limits> // std::numeric_limits<T>
#include <mutex> // std::mutex
#include <set> // std::set
namespace utils::system { // namespace start
//----------------------------------------------------------------//
/* CLASS */
template<typename T>
class IdHandler {
static_assert(std::is_integral_v<T>, "T must be an integral type");
private:
mutable std::mutex _lock; // Handling of multithreading
// 0 is reserved for unallocated ones / default value
T _id = std::numeric_limits<T>::min();
std::set<T> _usedIds; // only for forced allocated ids
std::set<T> _freeIds;
// ------------ Function ---------- //
_hot T allocate_(const bool safe_mode = true)
{
std::unique_lock<std::mutex> lock(this->_lock, std::defer_lock);
if (safe_mode) lock.lock();
else (void)lock.try_lock();
T id = 0;
if (this->_freeIds.size() > 0) {
auto it = this->_freeIds.begin();
id = *it;
this->_freeIds.erase(id);
} else _likely {
do { // skip 0 (reserved) & the forced ids, the overflow is checked at each step
if (this->_id == std::numeric_limits<T>::max()) _unlikely {
throw utils::exception::FatalException(utils::exception::InternalCode::IdOverflow);
}
id = ++this->_id;
} while (id == 0 || this->_usedIds.contains(id));
}
return id;
};
_hot void free_(const T id, const bool safe_mode = true)
{
std::unique_lock<std::mutex> lock(this->_lock, std::defer_lock);
if (safe_mode) lock.lock();
else (void)lock.try_lock();
// 0 is never distributed
if (id == 0) _unlikely {
throw utils::exception::ErrorException(utils::exception::InternalCode::InvalidId, "The id 0 is reserved");
}
// Above the counter: only a forced id can be freed (the counter will give it later)
if (id > this->_id) _unlikely {
if (this->_usedIds.erase(id) == 0) _unlikely {
throw utils::exception::ErrorException(utils::exception::InternalCode::UnknownId, "This id was never distributed");
}
return;
}
// Only if the id wasn't already free
if (!this->_freeIds.insert(id).second) _unlikely {
throw utils::exception::ErrorException(utils::exception::InternalCode::DoubleFree);
}
// Remove it from the forced ones (if it was forced)
this->_usedIds.erase(id);
};
public:
// ------------ Function ---------- //
_cold inline void use(T id, const bool safe_mode = true)
{
std::unique_lock<std::mutex> lock(this->_lock, std::defer_lock);
if (safe_mode) lock.lock();
else (void)lock.try_lock();
// Check if the id is free (released or never distributed)
if (id == 0) _unlikely {
throw utils::exception::ErrorException(utils::exception::InternalCode::InvalidId, "The id 0 is reserved");
} else if (this->_freeIds.contains(id)) _unlikely {
this->_freeIds.erase(id);
} else if (id <= this->_id) _unlikely { // already distributed and still in use
throw utils::exception::ErrorException(utils::exception::InternalCode::DoubleUse);
}
// Store the forced id
if (!this->_usedIds.insert(id).second) _unlikely {
throw utils::exception::ErrorException(utils::exception::InternalCode::DoubleUse);
}
};
_cold _nodiscard inline T id(void) const {return this->_id;};
_cold _nodiscard inline T actual(const bool safe_mode = true) const
{
std::unique_lock<std::mutex> lock(this->_lock, std::defer_lock);
if (safe_mode) lock.lock();
else (void)lock.try_lock();
if (this->_freeIds.size() > 0) _likely {return *this->_freeIds.begin();}
else _unlikely {return this->_id;}
};
_cold _nodiscard T preview(const bool safe_mode = true) const
{
std::unique_lock<std::mutex> lock(this->_lock, std::defer_lock);
if (safe_mode) lock.lock();
else (void)lock.try_lock();
if (this->_freeIds.size() > 0) {
return *this->_freeIds.begin();
}
// Same as allocate (skip 0 & the forced ids) without changing anything
T id = this->_id;
do {
if (id == std::numeric_limits<T>::max()) _unlikely {
throw utils::exception::ErrorException(utils::exception::InternalCode::IdOverflow);
}
++id;
} while (id == 0 || this->_usedIds.contains(id));
return id;
};
_hot inline T allocate(T& id, const bool safe_mode = true) {return (id = this->allocate_(safe_mode));};
_hot _nodiscard inline T allocate(const bool safe_mode = true) {return this->allocate_(safe_mode);};
_hot inline void free(T& id, const bool safe_mode = true) {this->free_(id, safe_mode); id = 0;};
_hot inline void free(const T& id, const bool safe_mode = true) {this->free_(id, safe_mode);};
_cold inline void free(void) {this->clear(true);}; // free every id
_cold void clear(const bool safe_mode = true) // free every id
{
std::unique_lock<std::mutex> lock(this->_lock, std::defer_lock);
if (safe_mode) lock.lock();
else (void)lock.try_lock();
// Reset value (no id in circulation)
this->_id = std::numeric_limits<T>::min();
this->_freeIds.clear();
this->_usedIds.clear();
};
// ------------ Operator ---------- //
IdHandler& operator=(const IdHandler& other) = delete;
IdHandler& operator=(IdHandler&& other) = delete;
// ---------- Constructor --------- //
IdHandler() = default;
IdHandler(const IdHandler& other) = delete;
IdHandler(IdHandler&& other) = delete;
// ----------- Destructor --------- //
~IdHandler() = default;
};
} // namespace end
#endif /* IDHANDLER_H */
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 192k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 192k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 192k | Updated | Claude CodeCodex | Verified | Install |