Class: fcf::NTest::SharedPtrAny
Package: fcfTest
File: test.hpp
Available from version: 1.1.1
Releases the ownership of the managed object, setting the current instance to null.
The release method explicitly breaks the connection between the fcf::NTest::SharedPtrAny instance and the object it currently manages.
When called, the internal reference count of the managed object is decremented. If this was the last remaining reference, the managed object is destroyed. The current instance is then reset to a null state.
Example: Manual Resource Release
Demonstrates how to manually release ownership of an object and how it affects the pointer's validity and the reference count.
#define FCF_TEST_IMPLEMENTATION
#include <fcfTest/test.hpp>
#include <string>
#include <iostream>
FCF_TEST_DECLARE("SharedPtrAny", "Demo", "ReleaseExample"){
struct Data {
std::string val;
Data(std::string v) : val(v) {}
};
// 1. Create two pointers sharing the same object
fcf::NTest::SharedPtrAny ptr1 = fcf::NTest::SharedPtrAny::make<Data>("Shared Resource");
fcf::NTest::SharedPtrAny ptr2 = ptr1;
FCF_TEST(ptr1.count() == 2, "Count should be 2", ptr1.count());
// 2. Release the first pointer
// This decrements the count but the object survives because ptr2 still holds it
ptr1.release();
FCF_TEST(!ptr1, "ptr1 should now be null");
FCF_TEST(ptr2.count() == 1, "ptr2 should now be the sole owner", ptr2.count());
// 3. Release the second pointer
// This will trigger the destruction of the 'Data' object
ptr2.release();
FCF_TEST(!ptr2, "ptr2 should now 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" -> "ReleaseExample" ...
[SUCCESS] Test completed successfully (0.000`001`685 sec)
[SUCCESS] All tests were completed.
Tests: 1 passed, 0 failed, 0 skipped, 1 total
Duration: 0.000`001`685 sec