FCF 2.0 development in progress...
> > > >
[News] [C++ Libraries API] [C++ Downloads] [Donate to the project] [Contacts]

fcf::NTest::SharedPtrAny class

Type:
class fcf::NTest::SharedPtrAny

Package: fcfTest

File: test.hpp

Available from version: 1.1.1

A lightweight, type-erased smart pointer implementation providing reference-counted storage for arbitrary data types.

The fcf::NTest::SharedPtrAny class is a specialized utility for storing objects of arbitrary type. It functions similarly to the simplified std::shared_ptr, but with built-in type safety thanks to runtime type information (RTTI). It is primarily used within the framework to pass complex data (such as formatter instances or user metadata) through layers that don't know the specific type at compile time, ensuring proper data destruction when the last reference is released.

Static methods
fcf::NTest::SharedPtrAny make()
- Creates a new SharedPtrAny instance that manages the lifetime of an object of type T using perfect forwarding.
Methods
[CONSTRUCTOR] SharedPtrAny()
[CONSTRUCTOR] SharedPtrAny(std::nullptr_t)
[CONSTRUCTOR] SharedPtrAny(const fcf::NTest::SharedPtrAny& a_source)
[CONSTRUCTOR] SharedPtrAny(fcf::NTest::SharedPtrAny&& a_source)
- A lightweight, type-erased smart pointer implementation providing reference-counted storage for arbitrary data types.
fcf::NTest::SharedPtrAny& operator=(const fcf::NTest::SharedPtrAny& a_source)
fcf::NTest::SharedPtrAny& operator=(fcf::NTest::SharedPtrAny&& a_source)
- Assigns one SharedPtrAny instance to another, supporting both copy and move semantics.
bool operator bool()
- Provides a boolean representation of the pointer's validity.
template <typename Ty>
Ty* cast()
- Attempts to cast the managed object to a specific type using runtime type information (RTTI).
int count()
- Returns the current number of references to the managed object.
void release()
- Releases the ownership of the managed object, setting the current instance to null.

Example: Type-Erased Data Storage

Demonstrates how to create, store, and safely retrieve an object of a specific type using type erasure and RTTI.

#define FCF_TEST_IMPLEMENTATION #include <fcfTest/test.hpp> #include <string> #include <iostream> FCF_TEST_DECLARE("SharedPtrAny", "Demo", "TypeErasureTest"){ // A custom structure to store in SharedPtrAny struct CustomData { std::string message; int value; CustomData(std::string m, int v) : message(m), value(v) {} }; // 1. Create a SharedPtrAny instance holding a CustomData object // We use the static make method for allocation fcf::NTest::SharedPtrAny ptr = fcf::NTest::SharedPtrAny::make<CustomData>("Hello World", 42); // 2. Check if the pointer is valid FCF_TEST(ptr, "Pointer should not be null"); // 3. Attempt to retrieve the data using cast // We must provide the exact type to successfully cast CustomData* castedPtr = ptr.cast<CustomData>(); FCF_TEST(castedPtr != nullptr, "Cast should succeed"); FCF_TEST(castedPtr->message == "Hello World", castedPtr->message); FCF_TEST(castedPtr->value == 42, castedPtr->value); // 4. Demonstrate failed cast (requesting wrong type) int* wrongCast = ptr.cast<int>(); FCF_TEST(wrongCast == nullptr, "Cast to wrong type should return nullptr"); // 5. Check reference count FCF_TEST(ptr.count() == 1, "Reference count should be 1"); } int main(int a_argc, char* a_argv[]) { bool error = false; fcf::NTest::cmdRun(a_argc, a_argv, fcf::NTest::CRM_RUN, &error); return error ? 1 : 0; }

Output:

Performing the test: "SharedPtrAny" -> "Demo" -> "TypeErasureTest" ... [SUCCESS] Test completed successfully (0.000`002`100 sec) [SUCCESS] All tests were completed. Tests: 1 passed, 0 failed, 0 skipped, 1 total Duration: 0.000`002`100 sec