#include #include #include #include class DownloadObject { public: DownloadObject(std::string uri, int priority) :uri_(uri), pri_(priority) {} virtual void downloadCompleted() = 0; virtual ~DownloadObject() {} private: std::string uri_; int pri_; }; class Downloader { public: void download(DownloadObject *d) { // ダウンロード開始 objs_.push_back(d); // ダウンロード完了したとして。。 for (std::vector::iterator i(objs_.begin()); i != objs_.end(); ++i) (*i)->downloadCompleted(); } std::vector objs_; }; class Content; typedef boost::shared_ptr ContentSp; class Cache; typedef boost::shared_ptr CacheSp; class Cache { public: static Cache &getInstance() { static CacheSp cache_; if (!cache_) { cache_.reset(new Cache); } return *cache_.get(); } void addContent(const ContentSp& c) { contents_.push_back(c); } private: std::vector contents_; }; class Content:public DownloadObject { public: static ContentSp create(std::string uri, int priority) { ContentSp s(new Content(uri, priority)); s->setSp(s); return s; } virtual ~Content() { std::cout << "Destroyed" << std::endl; } virtual void downloadCompleted() { std::cout << "downloadCompleted" << std::endl; Cache::getInstance().addContent(self_); } private: Content(std::string uri, int priority) :DownloadObject(uri, priority) { std::cout << "Created" << std::endl; } void setSp(const ContentSp &sp) { self_ = sp; } ContentSp self_; }; int main() { std::cout << "Test started" << std::endl; { ContentSp sp = Content::create("hoge", 1); Downloader d; d.download(sp.get()); } std::cout << "Test finished" << std::endl; }