I have a ServiceProvider class that contains a pair of pointers to different services, for example:
class ServiceProvider() { Service3* GetService3(); public: void Process(Object* o); void Shrink(); private: TAutoSpawningPtr<Service1> service1; TAutoSpawningPtr<Service2> service2; Service3* service3; }
Please note that TAutoSpawningPtr is the theoretical class of the smart pointer I'm looking for, and service3 is declared as a regular pointer to explicitly show the behavior I needed. Body Process() :
void ServiceProvider::Process(Object* o) { service1->Process(o); service2->Process(o); GetService3()->Process(o); }
The body of GetService3() :
void ServiceProvider::GetService3() { if(!service3) { service3 = new Service3(); } return service3; }
As you can see, an instance of Service3 is created lazily and does not exist until it is needed.
Shrink() method is called periodically to remove all internal services. Like this:
void ServiceProvider::Shrink() { service1.Release(); // delete its internal Service1 pointer if it exists. service2.Release(); // delete its internal Service2 pointer if it exists. if (service3) { // delete its internal Service3 pointer if it exists. delete service3; service3 = nullptr; } }
What I need: I want TAutoSpawningPtr<> be a smart pointer class that automatically instantiates the class, invoking the default construct when I cast the pointer using an overloaded operator-> . The internal resource available to the pointer had to be deleted once when the Release() method was called (and, of course, it had to be recreated when I needed it).
Why do I need it?
- Automatic control of the presence / absence of the object.
- To exclude nullptrs when the draining pointers directly (e.g.
this->service3->Process(o) ) instead of indirect GetService3() . - To free unused services without explicit checks.
Question: Does the standard (or any third-party) library have an automatic pointer class that will satisfy my needs? And if not, can you bring me some code examples that show the behavior I need. Thanks.
source share