#ifndef REPOSITORY_H #define REPOSITORY_H ///////////////////////////////////////////////////////////////////// // Repository.h - application of Singleton Pattern // // // // Jim Fawcett, CSE776 - Design Patterns, Fall 2017 // ///////////////////////////////////////////////////////////////////// /* * Repository derives from SingletonBase * - T is the type of data held in a Repository list * - Lock is a template policy that determines whether * Repository will be thread safe. * - I thought using a Singleton base class would make this implementation * simpler, but in retrospect, maybe not. */ #include "../SingletonBase/SingletonBase.h" #include namespace SingletonRepository { /////////////////////////////////////////////////////////////////// // Repository class // - template parameter T is type of data held in Repo list // - Lock is a locking policy template class Repository : public Singleton::SingletonBase { public: /*---< get pointer to singleton instance >---------------------*/ /* * - lazy load, e.g., singleton instance isn't created until * we call this method */ static Repository* Instance() { Singleton::SingletonBase* pTemp = Singleton::SingletonBase::Instance(); if (pRepo == nullptr) { // uses double check locking if (pRepo == nullptr) { Lock lock; // creating repo by copying existing base pRepo = new Repository(pTemp); } } return pRepo; } /*---< tell singleton we're done with this reference >---------*/ void Release() { pInstance->Release(); } /*---< add item to Repo list >---------------------------------*/ /* * - adds can be chained */ Repository& add(T t) { Lock lock; repoList.push_back(t); return *this; } /*---< retrieve copy of Repo List >----------------------------*/ std::list getList() { Lock lock; return repoList; } /*---< set new Repo List >-------------------------------------*/ void setList(const std::list& list) { Lock lock; repoList = list; } protected: std::list repoList; static Repository* pRepo; Repository(Singleton::SingletonBase* pBase); ~Repository() {}; Repository(const Repository&) = delete; }; template Repository* Repository::pRepo = nullptr; template Repository::Repository(Singleton::SingletonBase* pBase) : SingletonBase(*pBase) {} } #endif