count() method from fcf::NTest::SharedPtrAny class
Class:
Package: fcfTest
File: test.hpp
Available from version: 1.1.1
Returns the current number of references to the managed object.
The
Result
- The current number of references to the managed object.
Example: Monitoring Reference Count
Demonstrates how the reference count changes during object creation, copying, and moving.
#define FCF_TEST_IMPLEMENTATION
#include <fcfTest/test.hpp>
#include <iostream>
FCF_TEST_DECLARE ("SharedPtrAny", "Demo", "CountExample"){
struct Data {
Data () {}
};
// 1. Create initial instance
fcf::NTest::SharedPtrAny ptr1 = fcf::NTest::SharedPtrAny ::make <Data >();
FCF_TEST (ptr1.count () == 1, "Initial count should be 1", ptr1.count ());
// 2. Copy the pointer
// ptr2 now shares ownership. Count should be 2.
fcf::NTest::SharedPtrAny ptr2 = ptr1;
FCF_TEST (ptr1.count () == 2, "Count should be 2 after copy", ptr1.count ());
FCF_TEST (ptr2.count () == 2, "Count should be 2 after copy", ptr2.count ());
// 3. Move the pointer
// We move ptr2 to ptr3. ptr2 becomes null, ptr1 and ptr3 share ownership.
fcf::NTest::SharedPtrAny ptr3;
ptr3 = std::move (ptr2);
FCF_TEST (ptr3.count () == 2, "ptr3 count should be 1 after move", ptr3.count ());
FCF_TEST (ptr2.count () == 0, "ptr2 should be null after move", ptr2.count ());
FCF_TEST (ptr1.count () == 2, "ptr1 count should be 1 after move", ptr1.count ());
// 4. Final check
fcf::NTest::log () << "Final count for ptr1: " << ptr1.count () << std::endl;
}
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" -> "CountExample" ...
> Final count for ptr1: 2
[SUCCESS] Test completed successfully (0.000`011`285 sec)
[SUCCESS] All tests were completed.
Tests: 1 passed, 0 failed, 0 skipped, 1 total
Duration: 0.000`011`285 sec
VPSDime is an industry leading VPS hosting company that provides virtualized server services with high performance, availability and friendly support.