Although class dcpp::shared_device is very similar with std::shared_ptr, and it borrows the idea from std::shared_ptr, but it is not std::shared_ptr. dcpp::shared_device is used to manage underlying device pointer, but it is not used for pointer, it is used for object (std::shared_ptr object is object too).
The std::shared_ptr object can be used as bool checker:
if (object) ...
And dcpp::shared_device needs to do similar things.
However, dcpp::shared_device is very different from std::shared_ptr. The device validation check is complicated. Checking underlying device is not just like checking a c++ integral value.
To distinguish clearly, operator bool won't be provided.
Instead, an explicit method .has_device() is provided.
#include <duckcpp/shared.hpp> #include <iostream> namespace my_space { class my_application { private: dcpp::shared_device __device; public: my_application(dcpp::shared_device device__): __device{device__} { // Before leaving constructor, the use count is 3. } public: void run() { if (! __device.has_device()) { throw std::runtime_error{"ERROR: sorry, no device"}; } while (__device.run()) { if (! __device.window_active()) { __device.yield(); continue; } __device.scene()->drawAll(); __device.video()->swapScenes(0xff123456); } } dcpp::int32_kt use_count() { return __device.use_count(); } }; } int main() try { dcpp::shared_device device{2560, 1440}; my_space::my_application app{device}; std::cout << "Use count: " << device.use_count() << std::endl; // 2 std::cout << "Use count: " << app.use_count() << std::endl; // 2 if (false) { device.destroy(); std::cout << "Use count: " << device.use_count() << std::endl; // 0 std::cout << "Use count: " << app.use_count() << std::endl; // 0 } // If above block is executed, the following run will throw exception. app.run(); return 0; } catch (const std::exception & e) { std::cout << e.what() << std::endl; return 0; }
> date Mon Apr 7 09:22:06 AM UTC 2025