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

SharedPtrAny() method from fcf::NTest::SharedPtrAny class

[CONSTRUCTOR] SharedPtrAny()
[CONSTRUCTOR] SharedPtrAny(std::nullptr_t)
[CONSTRUCTOR] SharedPtrAny(const fcf::NTest::SharedPtrAny& a_source)
[CONSTRUCTOR] SharedPtrAny(fcf::NTest::SharedPtrAny&& a_source)

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.

Detailed description

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).

Example: Constructor Usage Patterns

Demonstrates the different ways to initialize a SharedPtrAny instance: default construction, null initialization, copy construction, and move construction.

#define FCF_TEST_IMPLEMENTATION #include <fcfTest/test.hpp> #include <string> FCF_TEST_DECLARE("SharedPtrAny", "Demo", "ConstructorTest"){ struct Data { std::string val; Data(std::string v) : val(v) {} }; // 1. Default constructor (creates a null pointer) fcf::NTest::SharedPtrAny ptrDefault; FCF_TEST(!ptrDefault, "Default constructor should result in a null pointer"); // 2. Nullptr constructor fcf::NTest::SharedPtrAny ptrNull(std::nullptr_t{}); FCF_TEST(!ptrNull, "Nullptr constructor should result in a null pointer"); // 3. Using make to create an object fcf::NTest::SharedPtrAny ptrOriginal = fcf::NTest::SharedPtrAny::make<Data>("Shared"); // 4. Copy constructor // Increases reference count fcf::NTest::SharedPtrAny ptrCopy(ptrOriginal); FCF_TEST(ptrCopy.count() == 2, "Copy constructor should increase ref count to 2", ptrCopy.count()); // 5. Move constructor // Transfers ownership, ptrOriginal becomes null fcf::NTest::SharedPtrAny ptrMove(std::move(ptrCopy)); FCF_TEST(ptrMove.count() == 2, "Move constructor should maintain ref count", ptrMove.count()); FCF_TEST(!ptrCopy, "Source of move should be null"); } 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" -> "ConstructorTest" ... [SUCCESS] Test completed successfully (0.000`002`494 sec) [SUCCESS] All tests were completed. Tests: 1 passed, 0 failed, 0 skipped, 1 total Duration: 0.000`002`494 sec