$treeview $search $mathjax
Palabos  Version 1.1
$projectbrief
$projectbrief
$searchbox

dynamicsIdentifiers.hh

Go to the documentation of this file.
00001 /* This file is part of the Palabos library.
00002  *
00003  * Copyright (C) 2011 FlowKit Sarl
00004  * Avenue de Chailly 23
00005  * 1012 Lausanne, Switzerland
00006  * E-mail contact: contact@flowkit.com
00007  *
00008  * The most recent release of Palabos can be downloaded at 
00009  * <http://www.palabos.org/>
00010  *
00011  * The library Palabos is free software: you can redistribute it and/or
00012  * modify it under the terms of the GNU Affero General Public License as
00013  * published by the Free Software Foundation, either version 3 of the
00014  * License, or (at your option) any later version.
00015  *
00016  * The library is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Affero General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Affero General Public License
00022  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00023 */
00024 
00025 
00026 #ifndef DYNAMICS_IDENTIFIERS_HH
00027 #define DYNAMICS_IDENTIFIERS_HH
00028 
00029 #include "core/dynamicsIdentifiers.h"
00030 #include "core/runTimeDiagnostics.h"
00031 #include <sstream>
00032 
00033 namespace plb {
00034 
00035 namespace meta {
00036 
00037 template<typename T, template<typename U> class Descriptor>
00038 void createIdIndirection (
00039         std::map<int,std::string> const& foreignIdToName,
00040         std::map<int,int>& idIndirect )
00041 {
00042     idIndirect.clear();
00043     std::map<int,std::string>::const_iterator it = foreignIdToName.begin();
00044     for (; it != foreignIdToName.end(); ++it) {
00045         int foreignId = it->first;
00046         std::string dynamicsName = it->second;
00047         int id = dynamicsRegistration<T,Descriptor>().getId(dynamicsName);
00048         idIndirect.insert(std::pair<int,int>(foreignId,id));
00049     }
00050 }
00051 
00052 template<typename T, template<typename U> class Descriptor>
00053 DynamicsRegistration<T,Descriptor>::~DynamicsRegistration()
00054 {
00055     for (pluint iEntry=0; iEntry<dynamicsByNumber.size(); ++iEntry) {
00056         delete dynamicsByNumber[iEntry].generator;
00057     }
00058 }
00059 
00060 template<typename T, template<typename U> class Descriptor>
00061 int DynamicsRegistration<T,Descriptor>::announce (
00062         std::string nameOfDynamics,
00063         DynamicsGenerator<T,Descriptor>* generator )
00064 {
00065     Entry entry(nameOfDynamics, generator);
00066     typename EntryMap::iterator it = dynamicsByName.find(entry);
00067     if (it != dynamicsByName.end()) {
00068         plbLogicError( std::string("The dynamics class ") + nameOfDynamics +
00069                        std::string(" was registered twice") );
00070     }
00071     dynamicsByNumber.push_back(entry);
00072     int nextId = dynamicsByNumber.size();
00073     dynamicsByName[entry] = nextId;
00074     return nextId;
00075 }
00076 
00077 template<typename T, template<typename U> class Descriptor>
00078 int DynamicsRegistration<T,Descriptor>::getId(std::string name) const
00079 {
00080     Entry entry(name, 0);
00081     typename EntryMap::const_iterator it = dynamicsByName.find(entry);
00082     if (it == dynamicsByName.end()) {
00083         return 0;
00084     }
00085     else {
00086         return it->second;
00087     }
00088 }
00089 
00090 template<typename T, template<typename U> class Descriptor>
00091 int DynamicsRegistration<T,Descriptor>::getNumId() const
00092 {
00093     return (int)(dynamicsByNumber.size());
00094 }
00095 
00096 template<typename T, template<typename U> class Descriptor>
00097 std::string DynamicsRegistration<T,Descriptor>::getName(int id) const
00098 {
00099     if (id==0) {
00100         return std::string("Undefined");
00101     }
00102     if (id < 0 || id > (int)dynamicsByNumber.size()) {
00103         std::stringstream message;
00104         message << "A dynamics class with ID " << id << " doesn't exist.";
00105         plbLogicError(message.str());
00106     }
00107     return dynamicsByNumber[id-1].name;
00108 }
00109 
00110 template<typename T, template<typename U> class Descriptor>
00111 Dynamics<T,Descriptor>* DynamicsRegistration<T,Descriptor>::generate
00112                              ( HierarchicUnserializer& unserializer )
00113 {
00114     plint id = unserializer.getId();
00115     PLB_ASSERT (id>0 && (pluint)id <= dynamicsByNumber.size());
00116     return dynamicsByNumber[id-1].generator->generate(unserializer);
00117 }
00118 
00119 template<typename T, template<typename U> class Descriptor>
00120 typename DynamicsRegistration<T,Descriptor>::EntryMap::const_iterator
00121     DynamicsRegistration<T,Descriptor>::begin() const
00122 {
00123     return dynamicsByName.begin();
00124 }
00125 
00126 template<typename T, template<typename U> class Descriptor>
00127 typename DynamicsRegistration<T,Descriptor>::EntryMap::const_iterator
00128     DynamicsRegistration<T,Descriptor>::end() const
00129 {
00130     return dynamicsByName.end();
00131 }
00132 
00133 
00134 template<typename T, template<typename U> class Descriptor>
00135 DynamicsRegistration<T,Descriptor>& dynamicsRegistration() {
00136     static DynamicsRegistration<T,Descriptor> instance;
00137     return instance;
00138 }
00139 
00140 template<typename T, template<typename U> class Descriptor>
00141 std::string constructIdNameChain(std::vector<int> const& ids, std::string separator)
00142 {
00143     std::string nameChain;
00144     if (ids.size()>0 && ids[0]>=0) {
00145         nameChain += dynamicsRegistration<T,Descriptor>().
00146                          getName(ids[0]);
00147     }
00148     for (pluint i=1; i<ids.size(); ++i) {
00149         if (ids[i]>=0) {
00150             nameChain += separator;
00151             nameChain += dynamicsRegistration<T,Descriptor>().
00152                              getName(ids[i]);
00153         }
00154     }
00155     return nameChain;
00156 }
00157 
00158 }  // namespace meta
00159 
00160 }  // namespace plb
00161 
00162 #endif  // DYNAMICS_IDENTIFIERS_HH