tags:

views:

72

answers:

2

Hi Guys,

I want to breakup a class so that its decoupled from the logic of performing certain task so that users can write new strategies as they wish to without interfering with the central model. So, I want to use templated strategy class but without having to have the user of the strategy to be templatised:

   class Model {  
     ...  
     boost::shared_ptr< Strategy < T > > m_pStrategy;  
     ...  
     public:  
     template <  typename T  >  
     void DoSomething() { m_pStrategy < T > ::DoSomething(); }  
    };  

I would like the DoSomething function to not be templated. Is there any other way I can achieve what I want to do here?

thanks.

A: 

Move the function out of class Strategy<T>.

Viktor Sehr
A: 

It seems to me like what you want to implement is a Policy-Based Design. I'm not sure what Model and Strategy do exactly, but it seems like Model is the root class, and Strategy is the Policy class, which users would want to provide in some cases to perform special handling. It also seems like the only reason you keep a pointer to the Strategy<T> object around is so that you can call functions on it.

In this case, you can design your class like this:

template<class Strategy> 
class Model : public Strategy {  
 public:  
 void DoSomething() 
 { 
    // magic happens (we will fill this in shortly)
 };
};  

You call methods on the Strategy class to do your magic. By letting the users define their own Strategy class, you give them the opportunity to define their own "magic". You need to apply rules on what method the Strategy class will provide at a minimum, so that you can call those methods in Model.

For example, suppose Model is actually some kind of resource manager, capable of being a simple smart pointer, or something else like a resource manager for a Windows Critical Section. Let's rename Model to auto_resource and Strategy will become release_policy and will be responsible for releasing whatever resource was assigned to it. In that case, you might have:

class pointer_release_policy
{
public:
  template<class Object> void release(Object* obj) { delete obj; }  
};

template<class Managed, class release_policy>
class auto_resource : public release_policy
{
public:
  // ... ctors etc defined here
  ~auto_resource()
  {
    release_policy::release(managed_);
  }
private:
  Managed managed_;
};

Which you could use for std::string pointers like this:

typedef auto_resource<std::string*, pointer_release_policy> string_ptr;
string_ptr my_str;

...and when my_str falls off the stack, the release method will automatically be called.

Later you want to add a new policy for releasing Windows mutex HANDLEs:

class handle_release_policy
{
public:
  template<class Handle> void release(Handle h)
  { 
    CloseHandle(h); // this is a WINAPI function that deallocates the specified resource
  };
};

You can use this thusly:

typedef auto_resource<HANDLE, handle_resource_policy> handle_resource;
//... allocate & use the mutex...
handle_resource mutex = CreateMutex(0, 0, 0);

Of course, in order to flesh all this out you need to add functionality for assigning, copying, releasing etc the resources. Here is a complete working example that puts everything together. I've provided 2 sets of policies, one for Windows CRITICAL_SECTIONs, and another for SOCKETs:

class SimpleCopyPolicy
{
public:
    template<class Resource> Resource copy(const Resource& rhs) const { Resource ret = rhs; return ret; }
protected:
    ~SimpleCopyPolicy(){};
};

class CritsecReleasePolicy
{
public:
    template<class Handle> bool release(Handle& h)
    {
        DeleteCriticalSection(&h);
        return true;
    }
protected:
    ~CritsecReleasePolicy() {};
};

class CritsecLockPolicy  // CRITICAL_SECTION lock/unlock policies
{
public:
    template<class Handle> bool lock(Handle& h)
    {
        EnterCriticalSection(const_cast<CRITICAL_SECTION*>(&h));
        return true;
    }
    template<class Handle> bool unlock(Handle& h) 
    {
        LeaveCriticalSection(&h);
        return true;
    }
};


class SocketReleasePolicy
{
public:
    template<class Handle> bool release(Handle h) { return 0 != closesocket(h); }
protected:
    ~SocketReleasePolicy(){};
};

template<class Resource, typename ReleasePolicy, typename CopyPolicy = SimpleCopyPolicy>
class simple_auto_resource : public ReleasePolicy, public CopyPolicy
{
public:
    typedef simple_auto_resource<Resource,ReleasePolicy,CopyPolicy> base_type;

    simple_auto_resource() : res(0) {}
    simple_auto_resource(const Resource & r) : res(copy(r)) {}
    ~simple_auto_resource() { if(res) release(res); }

    void clear() { if(res) release(res); res = 0; }

    Resource& get() { return res; }
    const Resource& get() const { return res; }

    Resource detach() { Resource ret = res; res = 0; return ret; }

    operator const Resource&() const { return get(); }
    operator Resource&() { return get(); }

    base_type& operator=(const Resource& rhs) { clear(); res = copy(rhs); return * this; }

    template<class Comp> bool operator==(const Comp& rhs) const { return res == (Resource)rhs; }
    template<class Comp> bool operator!=(const Comp& rhs) const { return res != (Resource)rhs; }
    template<class Comp> bool operator<(const Comp& rhs) const { return res < (Resource)rhs; }
private:
    Resource res;
};

typedef simple_auto_resource<CRITICAL_SECTION, CritsecReleasePolicy> auto_critsec;
typedef simple_auto_resource<SOCKET,SocketReleasePolicy> auto_socket;

For more on policy-based design, see Modern C++ Design.

...

John Dibling
Thanks for a great answer... :)
SWKK
@SWKK: My pleasure, enjoy.
John Dibling