00001 /* 00002 00003 Copyright (C) University of Oxford, 2005-2010 00004 00005 University of Oxford means the Chancellor, Masters and Scholars of the 00006 University of Oxford, having an administrative office at Wellington 00007 Square, Oxford OX1 2JD, UK. 00008 00009 This file is part of Chaste. 00010 00011 Chaste is free software: you can redistribute it and/or modify it 00012 under the terms of the GNU Lesser General Public License as published 00013 by the Free Software Foundation, either version 2.1 of the License, or 00014 (at your option) any later version. 00015 00016 Chaste is distributed in the hope that it will be useful, but WITHOUT 00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00018 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00019 License for more details. The offer of Chaste under the terms of the 00020 License is subject to the License being interpreted in accordance with 00021 English Law and subject to any action against the University of Oxford 00022 being under the jurisdiction of the English Courts. 00023 00024 You should have received a copy of the GNU Lesser General Public License 00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>. 00026 00027 */ 00028 00029 #ifndef CELLMUTATIONSTATEREGISTRY_HPP_ 00030 #define CELLMUTATIONSTATEREGISTRY_HPP_ 00031 00032 #include <boost/shared_ptr.hpp> 00033 #include <vector> 00034 00035 #include "AbstractCellMutationState.hpp" 00036 #include "Exception.hpp" 00037 00038 #include "ChasteSerialization.hpp" 00039 #include <boost/serialization/shared_ptr.hpp> 00040 #include <boost/serialization/vector.hpp> 00041 00045 class CellMutationStateRegistry 00046 { 00047 public: 00054 template<class SUBCLASS> 00055 boost::shared_ptr<AbstractCellMutationState> Get(); 00056 00060 static CellMutationStateRegistry* Instance(); 00061 00065 const std::vector<boost::shared_ptr<AbstractCellMutationState> >& rGetAllMutationStates(); 00066 00070 void Clear(); 00071 00080 CellMutationStateRegistry* TakeOwnership(); 00081 00090 void SpecifyOrdering(const std::vector<boost::shared_ptr<AbstractCellMutationState> >& rOrdering); 00091 00095 bool HasOrderingBeenSpecified(); 00096 00097 private: 00098 00102 CellMutationStateRegistry(); 00103 00107 CellMutationStateRegistry(const CellMutationStateRegistry&); 00108 00112 CellMutationStateRegistry& operator= (const CellMutationStateRegistry&); 00113 00117 static CellMutationStateRegistry* mpInstance; 00118 00122 std::vector<boost::shared_ptr<AbstractCellMutationState> > mMutationStates; 00123 00125 bool mOrderingHasBeenSpecified; 00126 00128 friend class boost::serialization::access; 00135 template<class Archive> 00136 void serialize(Archive & archive, const unsigned int version) 00137 { 00138 archive & mMutationStates; 00139 archive & mOrderingHasBeenSpecified; 00140 } 00141 }; 00142 00143 template<class SUBCLASS> 00144 boost::shared_ptr<AbstractCellMutationState> CellMutationStateRegistry::Get() 00145 { 00146 boost::shared_ptr<AbstractCellMutationState> p_state; 00147 for (unsigned i=0; i<mMutationStates.size(); i++) 00148 { 00149 if (mMutationStates[i]->IsType<SUBCLASS>()) 00150 { 00151 p_state = mMutationStates[i]; 00152 break; 00153 } 00154 } 00155 if (!p_state) 00156 { 00157 if (mOrderingHasBeenSpecified) 00158 { 00159 EXCEPTION("Cannot add a new mutation state not specified in the ordering."); 00160 } 00161 else 00162 { 00163 // Create a new mutation state 00164 p_state.reset(new SUBCLASS); 00165 mMutationStates.push_back(p_state); 00166 } 00167 } 00168 return p_state; 00169 } 00170 00171 00172 #endif /* CELLMUTATIONSTATEREGISTRY_HPP_ */