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

jQuery $.ajax memory leak?

jquery ajax firefox memory-leaks

Why does jQuery ajax cause a horrible memory leak on Firefox &amp; other browsers?

How can I check if a UILabel's value is more than 0 in an if statement?

iphone objective-c ios xcode uilabel

How can I check if a UILabel's value is more than 0 in an if statement?

PHPUnit error while creating unit test cases

php netbeans phpunit

i have controller class "UserController"(in controller folder) which extends BaseController (present in app folder),

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.