Extract common class behaviour in a template

I observed that in my program I needed to make several classes use the following common pattern. The idea behind it is that resource_mgr maintains a list of reference-counted pointers to resource objects, and exclusively controls their lifetime. Clients may not create or delete resource instances, but may request them from resource_mgr.

class resource_impl
{
    public:
        // ...

    private:
        resource_impl(...);
        ~resource_impl();
        // ...
        friend class resource_mgr;
}

class resource_mgr
{
    public:
        // ...
        shared_ptr<resource_impl> new_resource(...);

    private:
        std::vector<shared_ptr<resource_impl> > resources_;
        static void delete_resource(resource* p); // for shared_ptr
}

How can I (or can I?) define a template to capture this common behavior? The following illustrates how this template might be used:

class texture_data
{
    // texture-specific stuff
}

typedef resource_impl<texture_data> texture_impl;
// this makes texture_impl have private *tors and friend resource_mgr<texture_impl>

typedef resource_mgr<texture_impl> texture_mgr;

//...

texture_mgr tmgr;
shared_ptr<texture_impl> texture = tmgr.new_resource(...);

Update: Various instantiations of resource_impl should all have in common the following properties:

  • They have private constructors and destructor
  • Their "associated" resource_mgr (the manager class that manages the same type of resource) is a friend class (so it can create/destroy instances)

Answers


First add the interface :

class resource_interface
{
  public:
    virtual ~resource_interface() = 0;
    virtual void foo() = 0;
};

Then change the resource_impl into template :

template< typename T >
class resource_impl : public T
{
    public:
        // ...

    private:
        resource_impl(...);
        ~resource_impl();
        // ...
        friend template< typename > class resource_mgr;
}

Then change resource_mgr into template:

template< typename T >
class resource_mgr
{
    public:
        // ...
        shared_ptr<T> new_resource(...);

    private:
        std::vector<shared_ptr<T> > resources_;
        static void delete_resource(T* p); // for shared_ptr
}

And you should have very generic resource_impl and resource_mgr classes.


Need Your Help

Best way to validate drag/drop operations for a TreeView in C#

c# validation user-controls treeview

I want to verify a drag &amp; drop operation is allowed. A valid item can come from another one of our "controls", or internally from within the custom treeview. Currently I have this:

Every third element should have a date beside it

php arrays loops multidimensional-array

So I have been asking a lot of array based questions as I strive to understand PHP's main source of data structure.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.