Expressive, Safe, Consistent.
The C++ Object Token Library (CppOtl) provides a set of tokens that are designed to manage an object's lifetime, similar to smart pointers from the C++ standard library. CppOtl contains tokens for unique and shared ownership of objects, weak observers for them, adds a unified type of ownership. Also, it introduces tokens with single (not_null) multiplicity, which can mitigate the "null references: billion-dollar mistake".
Tokens from CppOtl are integrated with smart pointers from the C++ standard library and can be consistently converted from one to another. CppOtl is intended for cases when there is not enough functionality of smart pointers from the C++ standard library and additional capabilities are required.
- A set of consistent tokens that can express associative links, function parameters and variables according to a UML model.
single
tokens that always have a linked object and can not be empty.- trackable and untrackable tokens for appropriate use-case requirements.
- The
safe::weak
can be created both from thesafe::unique
and thesafe::shared
. It will become expired (but not dangling) when an owner of a watched object is destroyed. - The
safe::unified
can temporarily prolong an object's lifetime in a given scope that allows finishing work with an object even other tokens is destroyed. - A constructor with the
itself
parameter allows a token itself to create an object with given attributes. - Algorithms for unified access to object tokens.
- Tokens meet the requirements of
std::ranges::range
, that allows to pass them to functions that acceptrange
argument. - Traits allow recognizing characteristics of tokens in compile-time.
- Focusing on maximum error detection in compile-time, several assertions in a debug build, minimum overhead in a release build, avoiding exceptions.
- The ability to integrate CppOtl tokens with third-party pointers and tokens.
All CppOtl entities are located in the otn
namespace. otn
is an abbreviation for object token.
-
safe
tokens provide an object's lifetime safety and can not be dangling.Name Description safe::unique[_single]<T>
A unique single owner of an object. safe::unique_optional<T>
A unique optional owner of an object. safe::shared[_single]<T>
A shared single owner of an object. safe::shared_optional<T>
A shared optional owner of an object. safe::weak_optional<T>
An optional observer of an object owned by the safe::unique
orsafe::shared
.safe::unified[_single]<T>
A unified single carrier and accessor of an object linked with safe
tokens.safe::unified_optional<T>
A unified optional carrier and accessor of an object linked with safe
tokens. -
slim
tokens are lightweight with minimum runtime overhead.Name Description slim::unique[_single]<T, D>
A unique single owner of an object. slim::unique_optional<T, D>
A unique optional owner of an object. -
raw
tokens are close to raw C++ objects and pointers.Name Description raw::unique[_single]<T>
A unique single owner of an object. raw::unique_optional<T>
A unique optional owner of an object. raw::weak[_single]<T>
A single observer of an object. raw::weak_optional<T>
An optional observer of an object. raw::unified[_single]<T>
A unified single carrier and accessor of an object. raw::unified_optional<T>
A unified optional carrier and accessor of an object.
Using otn
tokens is similar to using smart pointers from the C++ standard library.
C++17 compiler.
Tested with:
- GCC 9.2.1
- Clang 9.0.1
Note: It is a known issue that not all compilers can compile SFINAE "magic" from the otn/support/concept.hpp
. Use C++20 standard in this case, if a compiler supports C++20 Concepts.
CppOtl is a header-only library.
Copy the include/otn
directory to a compiler accessible place, add the path obtained to include
to a compiler's include paths, and include the otn/all.hpp
header file in a source code file.
See the Example complex.
See the Test/Token complex.
Beta version. Improving functionality, eliminating bugs, optimizing unoptimals.