nicky

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
一个简单的资源管理器:

这样写主要是比较方便,可以省一些代码。
功能还比较少。

代码,附带一个test:
https://files.cnblogs.com/nicky/simple_res_mgr.rar


#ifndef _RESOURCEMGR_H__
#define _RESOURCEMGR_H__

#include <map>
#include <boost/shared_ptr.hpp>
//#include "simple_string.h"
#include <string>
#include <sstream>
typedef std::string String;

namespace Nicky
{
 template<typename ResPtr> //expecting boost shared_ptr type
 class ResourceMgr
 {
 public:
  typedef typename ResPtr::element_type res_type;
  typedef ResPtr       res_ptr_type;
  typedef std::map<String,ResPtr>   res_cont_type;

 public:
  ResourceMgr(void) {}
  virtual ~ResourceMgr(void) {}


  //derived class should implement its own create interface


  virtual void Delete(const String& name)
  {
   res_cont_type::iterator i = m_Resources.find(name);
   if (i != m_Resources.end())
    m_Resources.erase(i);
  }

  virtual void Delete(const res_ptr_type& p)
  {
   res_cont_type::iterator i, iend = m_Resources.end();
   for(i = m_Resources.begin(); i != iend; ++i)
   {
    if( i->second == p )
    {
     m_Resources.erase(i);
     return;
    }
   }
  }

  virtual void DeleteAll()
  {
   m_Resources.clear();
  }


  //search
  res_ptr_type getByName(const String& name) const
  {
   res_cont_type::const_iterator i = m_Resources.find(name);
   if (i != m_Resources.end())
    return i->second;

   return res_ptr_type();
  }

  template<typename T>
  boost::shared_ptr<T> getByName(const String& name, boost::shared_ptr<T>& p) const
  {
   res_cont_type::const_iterator i = m_Resources.find(name);
   if (i != m_Resources.end())
    return (p = boost::static_pointer_cast<T>(i->second));

   return boost::shared_ptr<T>();
  }

  bool isExist(const String& name) const
  {
   res_cont_type::const_iterator i = m_Resources.find(name);
   if (i != m_Resources.end())
    return true;

   return false;
  }

 protected:

  String generateName() const
  {
   static int num_gen_names = 0;
   String name;
   std::stringstream ss;
   ss<<"gen_"<<num_gen_names++;
   name = ss.str();

   return name;
  }


  //no care about name
  res_ptr_type insert(const res_ptr_type& p)
  {
   return m_Resources.insert(
    res_cont_type::value_type(this->generateName(), p)
    ).first->second;
  }

  template<typename T>
  boost::shared_ptr<T> insert(const boost::shared_ptr<T>& p)
  {
   m_Resources.insert(
    res_cont_type::value_type(this->generateName(), p)
    );

   return p;
  }


  //no care about name (name not yet existed)
  res_ptr_type insert(const String& name, const res_ptr_type& p)
  {
   return m_Resources.insert(
    res_cont_type::value_type(name, p)
    ).first->second;
  }

  template<typename T>
  boost::shared_ptr<T> insert(const String& name, const boost::shared_ptr<T>& p)
  {
   m_Resources.insert(
    res_cont_type::value_type(name, p)
    );

   return p;
  }


 protected:
  //resource holder  
  res_cont_type m_Resources;
 };


}

#endif //_RESOURCEMGR_H__


posted on 2005-02-18 20:21  nicky  阅读(993)  评论(0编辑  收藏  举报