ツリーとノード (DAG) を管理するプログラムを作成しています。
ノードは で表されますclass MCModule
。
class MCModule
{
...
};
typedef boost::shared_ptr<MCModule> MCModule_sp;
typedef MCModule * MCModule_p;
私はさまざまな種類のコンテナを持っています。
たとえば、私のクラス Node では、子ノードは共有ポインターで管理されます。
std::list<shared_ptr<Node>> childs;
親ノードは、生のポインター内で管理されます。
std::list<Node *> parents;
ただし、別のクラスにノードポインターのベクトルがある場合もあります。
これらのコンテナはすべて、クラス Module の「ポインタ」を shared_ptr または raw ポインタ内に格納します。
コンテナのタイプが何であれ、いくつかの同一のタスクを実行する必要があります: 名前に基づいて、ID に基づいて、ノードに基づいてノードを検索します。
次のテンプレート クラスを書き始めました。
template<typename _ListT, typename _ElemT>
class ModuleListWrapper
{
public:
explicit ModuleListWrapper(_ListT& lst)
: m_lst(lst){}
/*! Indicates whether the \a module was found in the list or not
* \return true if the \a module was found, otherwise return false
*/
inline bool find(const MCModule_sp& module) {
return (std::find_if(m_lst.begin(),
m_lst.end(),
compare_with(module->getName()) ) != m_lst.end() );
}
inline _ElemT find(const std::string& name) {
_ListT::iterator it = std::find_if(m_lst.begin(), m_lst.end(), compare_with(name));
return ( it != m_lst.end() ) ? (*it) : NullPtrT;
}
// ... much more accessors ...
/*! Overloads the -> operator to facilitate the access to the inner class.
\return a pointer to the inner \a _ListT object
*/
inline _ListT * operator->() const { return &m_lst; }
/*! Gets a reference to the inner \a _ListT object
*/
inline _ListT& get() const { return m_lst; }
private:
_ListT& m_lst;
};
typedef ModuleListWrapper<ModuleList_shareptr, Module_shareptr> ModuleListWrapper_shareptr;
typedef ModuleListWrapper<ModuleList_rawptr, Module_p> ModuleListWrapper_rawptr;
typedef ModuleListWrapper<ModuleVector_rawptr, Module_p> ModuleListWrapper_rawptr;
このクラスの目的は、基礎となるコンテナーに関係なくアクションを実行するためのインターフェースを提供することです。もちろん、コンテナは期待される型のポインタを「含む」必要があります。
それは受け入れられる解決策だと思いますか、それともまったく役に立たないと思いますか? 確かに完璧ではありませんが、 std::list または std::vector から継承するよりも良いかもしれません... ?
批評、意見、提案は大歓迎です。