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

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

template <typename Ty, typename... ArgPack>
[STATIC] fcf::NTest::SharedPtrAny make()

Class: fcf::NTest::SharedPtrAny

Package: fcfTest

File: test.hpp

Available from version: 1.1.1

Creates a new SharedPtrAny instance that manages the lifetime of an object of type T using perfect forwarding.

The make static method is the preferred way to create a new fcf::NTest::SharedPtrAny instance. It allocates a control block and the object itself in a single memory operation, similar to how std::make_shared works.

The method uses perfect forwarding to pass the provided arguments directly to the constructor of the target type, ensuring maximum efficiency and support for all constructor signatures.

Template arguments
Ty - The type of the object to be managed by the SharedPtrAny.
ArgPack - A pack of arguments to be forwarded to the constructor of type Ty.
Result
fcf::NTest::SharedPtrAny
- A new fcf::NTest::SharedPtrAny instance managing the newly created object of type Ty.

Example: Creating and retrieving type-erased objects

Demonstrates how to use the make method to instantiate a custom object and retrieve it using cast.

#define FCF_TEST_IMPLEMENTATION #include <fcfTest/test.hpp> #include <string> #include <iostream> FCF_TEST_DECLARE("SharedPtrAny", "Demo", "MakeExample"){ // A simple structure to demonstrate object creation struct User { std::string name; int age; User(std::string a_name, int a_age) : name(a_name), age(a_age) {} }; // 1. Create a SharedPtrAny instance using the make method. // The arguments "Alice" and 30 are forwarded to the User constructor. fcf::NTest::SharedPtrAny userPtr = fcf::NTest::SharedPtrAny::make<User>("Alice", 30); // 2. Verify that the pointer is not null FCF_TEST(userPtr, "The created pointer should be valid"); // 3. Retrieve the original object using cast User* user = userPtr.cast<User>(); // 4. Perform assertions on the retrieved data FCF_TEST(user != nullptr, "Cast should return a valid pointer"); FCF_TEST(user->name == "Alice", user->name); FCF_TEST(user->age == 30, user->age); // 5. Check the reference count FCF_TEST(userPtr.count() == 1, "Reference count should be exactly 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" -> "MakeExample" ... [SUCCESS] Test completed successfully (0.000`003`192 sec) [SUCCESS] All tests were completed. Tests: 1 passed, 0 failed, 0 skipped, 1 total Duration: 0.000`003`192 sec