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

dynamicsIdentifiers.h

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_H
00027 #define DYNAMICS_IDENTIFIERS_H
00028 
00029 #include "core/globalDefs.h"
00030 #include "core/dynamics.h"
00031 #include "core/hierarchicSerializer.h"
00032 #include <vector>
00033 #include <string>
00034 #include <map>
00035 
00036 namespace plb {
00037 
00038 namespace meta {
00039 
00040 template<typename T, template<typename U> class Descriptor>
00041 void createIdIndirection (
00042         std::map<int,std::string> const& foreignIdToName,
00043         std::map<int,int>& idIndirect );
00044 
00045 template<typename T, template<typename U> class Descriptor>
00046 struct DynamicsGenerator {
00047     virtual ~DynamicsGenerator() { }
00048     virtual Dynamics<T,Descriptor>*
00049         generate(HierarchicUnserializer& unserializer) const =0;
00050 };
00051 
00052 template<typename T, template<typename U> class Descriptor>
00053 class DynamicsRegistration {
00054 public:
00055     struct Entry {
00056         Entry(std::string name_, DynamicsGenerator<T,Descriptor>* generator_)
00057             : name(name_),
00058               generator(generator_)
00059         { }
00060         std::string name;
00061         DynamicsGenerator<T,Descriptor>* generator;
00062     };
00063     struct EntryLessThan {
00064         bool operator()(Entry const& entry1, Entry const& entry2) const {
00065             return entry1.name < entry2.name;
00066         }
00067     };
00068     typedef std::map<Entry,int,EntryLessThan> EntryMap;
00069 public:
00070     ~DynamicsRegistration();
00071     int announce(std::string nameOfDynamics,
00072                  DynamicsGenerator<T,Descriptor>* generator_=0);
00073     int getId(std::string name) const;
00074     int getNumId() const;
00075     std::string getName(int id) const;
00076     Dynamics<T,Descriptor>* generate(HierarchicUnserializer& unserializer);
00077     typename EntryMap::const_iterator begin() const;
00078     typename EntryMap::const_iterator end() const;
00079 public:
00082     DynamicsRegistration() { }
00083 private:
00084     DynamicsRegistration(DynamicsRegistration<T,Descriptor> const& rhs) { }
00085     DynamicsRegistration<T,Descriptor>& operator= (
00086             DynamicsRegistration<T,Descriptor> const& rhs )
00087     {
00088         return *this;
00089     }
00090 private:
00091     EntryMap dynamicsByName;
00092     std::vector<Entry> dynamicsByNumber;
00093 
00094 // TODO: This friend declaration is not properly parsed in older (but not-so-old) GCC
00095 //   compilers. Therefore, it is commented for now, and the default constructor of
00096 //   DynamicsRegistration is public, although it should be private, because
00097 //   DynamicsRegistration is a singleton.
00098 //
00099 // template<typename T_, template<typename U_> class Descriptor_>
00100 // friend DynamicsRegistration<T_,Descriptor_>& dynamicsRegistration();
00101 };
00102 
00103 template<typename T, template<typename U> class Descriptor>
00104 DynamicsRegistration<T,Descriptor>& dynamicsRegistration();
00105 
00106 template<typename T, template<typename U> class Descriptor>
00107 std::string constructIdNameChain(std::vector<int> const& ids, std::string separator=".");
00108 
00109 
00110 template< typename T,
00111           template<typename U> class Descriptor,
00112           class NoParamDynamics >
00113 class NoParamDynamicsGenerator : public DynamicsGenerator<T,Descriptor>
00114 {
00115     virtual Dynamics<T,Descriptor>* generate(HierarchicUnserializer& unserializer) const {
00116         return new NoParamDynamics();
00117     }
00118 };
00119 
00120 template< typename T,
00121           template<typename U> class Descriptor,
00122           class OneParamDynamics >
00123 class OneParamDynamicsGenerator : public DynamicsGenerator<T,Descriptor>
00124 {
00125     virtual Dynamics<T,Descriptor>* generate(HierarchicUnserializer& unserializer) const {
00126         return new OneParamDynamics(unserializer.readValue<T>());
00127     }
00128 };
00129 
00130 template< typename T,
00131           template<typename U> class Descriptor,
00132           class TwoParamDynamics >
00133 class TwoParamDynamicsGenerator : public DynamicsGenerator<T,Descriptor>
00134 {
00135     virtual Dynamics<T,Descriptor>* generate(HierarchicUnserializer& unserializer) const {
00136         return new TwoParamDynamics (
00137                        unserializer.readValue<T>(),
00138                        unserializer.readValue<T>() );
00139     }
00140 };
00141 
00142 template< typename T,
00143           template<typename U> class Descriptor,
00144           class GeneralDynamics >
00145 class GeneralDynamicsGenerator : public DynamicsGenerator<T,Descriptor>
00146 {
00147     virtual Dynamics<T,Descriptor>* generate(HierarchicUnserializer& unserializer) const {
00148         return new GeneralDynamics(unserializer);
00149     }
00150 };
00151 
00152 template< typename T,
00153           template<typename U> class Descriptor,
00154           class CompDynamics >
00155 class CompositeDynamicsGenerator : public DynamicsGenerator<T,Descriptor>
00156 {
00157     virtual Dynamics<T,Descriptor>* generate(HierarchicUnserializer& unserializer) const {
00158         bool automaticPrepareCollision;
00159         unserializer.readValue(automaticPrepareCollision);
00160         Dynamics<T,Descriptor>* compositeDynamics = dynamicsRegistration<T,Descriptor>().generate(unserializer);
00161         return new CompDynamics(compositeDynamics, automaticPrepareCollision);
00162     }
00163 };
00164 
00165 template< typename T,
00166           template<typename U> class Descriptor,
00167           class OneParamDynamics >
00168 int registerOneParamDynamics(std::string name) {
00169     return dynamicsRegistration<T,Descriptor>().announce (
00170                name, new OneParamDynamicsGenerator<T,Descriptor,OneParamDynamics> );
00171 }
00172 
00173 template< typename T,
00174           template<typename U> class Descriptor,
00175           class TwoParamDynamics >
00176 int registerTwoParamDynamics(std::string name) {
00177     return dynamicsRegistration<T,Descriptor>().announce (
00178                name, new TwoParamDynamicsGenerator<T,Descriptor,TwoParamDynamics> );
00179 }
00180 
00181 template< typename T,
00182           template<typename U> class Descriptor,
00183           class NoParamDynamics >
00184 int registerNoParamDynamics(std::string name) {
00185     return dynamicsRegistration<T,Descriptor>().announce (
00186                name, new NoParamDynamicsGenerator<T,Descriptor,NoParamDynamics> );
00187 }
00188 
00189 template< typename T,
00190           template<typename U> class Descriptor,
00191           class GeneralDynamics >
00192 int registerGeneralDynamics(std::string name) {
00193     return dynamicsRegistration<T,Descriptor>().announce (
00194                name, new GeneralDynamicsGenerator<T,Descriptor,GeneralDynamics> );
00195 }
00196 
00197 template< typename T,
00198           template<typename U> class Descriptor,
00199           class CompDynamics >
00200 int registerCompositeDynamics(std::string name) {
00201     return dynamicsRegistration<T,Descriptor>().announce (
00202                name, new CompositeDynamicsGenerator<T,Descriptor,CompDynamics> );
00203 }
00204 
00205 }  // namespace meta
00206 
00207 }  // namespace plb
00208 
00209 #endif  // DYNAMICS_IDENTIFIERS_H
00210