在我看来,你想要实现的是一个Policy-Based Design。我不确定Model
和Strategy
是做什么的,但看起来好像是Model
是根类,而Strategy
是Policy类,在某些情况下,用户希望提供该类来执行特殊处理。它也似乎是你保持一个指向Strategy<T>
对象的唯一原因是你可以调用它的函数。
在这种情况下,你可以设计你的类是这样的:
template<class Strategy>
class Model : public Strategy {
public:
void DoSomething()
{
// magic happens (we will fill this in shortly)
};
};
你呼吁Strategy
类的方法做你的魔术。通过让用户定义他们自己的课程,让他们有机会定义他们自己的“魔力”。您需要应用规则,至少要提供什么方法,以便您可以调用Model
中的这些方法。
例如,假设Model
实际上是某种资源管理器,能够成为简单的智能指针,或者其他类似Windows关键部分的资源管理器。我们将Model
重命名为auto_resource
,Strategy
将变为release_policy
,并将负责释放分配给它的任何资源。在这种情况下,你可能有:
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_;
};
,你可以使用std::string
指针这样的:
typedef auto_resource<std::string*, pointer_release_policy> string_ptr;
string_ptr my_str;
...和my_str
脱落的堆栈时,该release
方法会自动被调用。
以后要添加新的政策发布的Windows互斥HANDLE
S:
class handle_release_policy
{
public:
template<class Handle> void release(Handle h)
{
CloseHandle(h); // this is a WINAPI function that deallocates the specified resource
};
};
您可以使用此正是如此:
typedef auto_resource<HANDLE, handle_resource_policy> handle_resource;
//... allocate & use the mutex...
handle_resource mutex = CreateMutex(0, 0, 0);
当然,为了充实这一切了你需要添加分配,复制,释放资源等功能。这是一个完整的工作示例,它将所有内容放在一起我提供了2套的政策,一个用于Windows CRITICAL_SECTION
s,而另一个用于SOCKET
S:
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;
更多基于策略的设计,见Modern C++ Design。
...
您正在参数化'DoSomething'中的* data member *。它甚至如何编译? “T”来自“策略< T >”哪里? – 2010-06-11 11:37:28