Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed advice on RAII design for object/connection pool items
    text
    copied!<p>I'll open by saying I've been looking into this for a few days, trying to grok what is the "right way" of doing it. After plenty of googling on RAII/pool design/smart pointers, and having come to no definite conclusion (except that maybe there is no absolute "right way"), I thought maybe it's time for someone more knowledgeable to point me in the right direction.</p> <p>I'm building an object pool, and I'm trying to make sure the client code can use RAII, if desired.</p> <p>There are 3 entities involved:</p> <ul> <li>Resource. Expensive to build, semi-cheap to reuse (checking its state on release is not trivial). Difficult to copy, as it wraps some C constructs, with their own allocated memory/resources.</li> <li>Wrapper/Handle. Wrapper for the Resource. Is given a Resource on ctor, releases it on dtor.</li> <li>Controller/Pool. Maintains a pool of Resources and manages their use by clients, via Wrapper or, at the client's discretion, directly.</li> </ul> <p>I present below a simplified example of what I've come up with. In the function <code>DoSomethingElse()</code>, you can see what I'm after - I get a reference to a Wrapper and, at the end of the scope, its dtor is invoked, and the Resource is released back to the pool.</p> <p>My question has to do with the definition of <code>Factory::GetResource()</code>. The simplified version presented here just allocates a new one every time; my actual implementation checks the pool for an available Resource (creates one if there's none available), marks it as in use, and returns a reference to it.</p> <p>I'd rather avoid having to define a proper copy ctor for the Resource, hence the return by reference, instead of by value. The Resource is guaranteed to outlive the caller, and the Controller maintains ownership throughout the app's life - they're not handed to the client code for life-cycle management. Of course, if the client asks for a direct reference, i.e., without the Wrapper, all bets are off.</p> <p>Is this design sound? Would I be better off using shared_ptr? Or some other mechanism/design?</p> <p>Thanks for your time.</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; using namespace std; static int seq = 0; // POOR MAN'S SEQUENCE FOR INSTANCE IDs class Resource { public: Resource() : id(seq++) { cout &lt;&lt; "Resource ctor: " &lt;&lt; id &lt;&lt; endl; } ~Resource() { cout &lt;&lt; "Resource dtor: " &lt;&lt; id &lt;&lt; endl; } private: int id; }; class Wrapper { public: // ON ACTUAL IMPLEMENTATION, NOTIFY THE CONTROLLER OF THE RELEASE ~Wrapper() { cout &lt;&lt; "Wrapper dtor: " &lt;&lt; id &lt;&lt; "Welease Bwian! Ee, I mean, the wesouwce" &lt;&lt; endl; } explicit Wrapper(Resource&amp; r) : id(seq++), res(r) { cout &lt;&lt; "Wrapper ctor: " &lt;&lt; id &lt;&lt; endl; } int getID() const { return id; } private: int id; Resource&amp; res; }; class Controller { public: ~Controller() { for (auto r : allres) delete r; } Resource&amp; GetResource(); private: // SIMPLIFIED. I'M USING Boost PTR CONTAINER vector&lt;Resource *&gt; allres; }; // SIMPLIFIED. IT WOULD ACTUALLY GET A RESOURCE FROM THE POOL Resource&amp; Controller::GetResource() { Resource* newres = new Resource(); allres.push_back(newres); return *(newres); } // SIMULATE GLOBAL CONTEXT Controller&amp; GetController() { static Controller f; return f; } void DoSomething(Wrapper&amp; wr) { cout &lt;&lt; "DoSth INI" &lt;&lt; endl; cout &lt;&lt; wr.getID() &lt;&lt; endl; cout &lt;&lt; "DoSth END" &lt;&lt; endl; } void DoSomethingElse() { cout &lt;&lt; "DoSthElse INI" &lt;&lt; endl; Wrapper w(GetController().GetResource()); DoSomething(w); cout &lt;&lt; "DoSthElse END" &lt;&lt; endl; } int main(int argc, char *argv[]) { cout &lt;&lt; "main INI" &lt;&lt; endl; cout &lt;&lt; "Calling DoSthElse" &lt;&lt; endl; DoSomethingElse(); cout &lt;&lt; "Called DoSthElse" &lt;&lt; endl; cout &lt;&lt; "main END" &lt;&lt; endl; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload