simple resource manager

來源:互聯網
上載者:User
一個簡單的資源管理員:

這樣寫主要是比較方便,可以省一些代碼。

功能還比較少。

代碼,附帶一個test:

http://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__

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.