[696] | 1 | #ifndef __XIOS_REGISTRY_HPP__ |
---|
| 2 | #define __XIOS_REGISTRY_HPP__ |
---|
| 3 | |
---|
| 4 | #include "base_type.hpp" |
---|
| 5 | #include "type.hpp" |
---|
| 6 | #include "mpi.hpp" |
---|
| 7 | #include "message.hpp" |
---|
| 8 | |
---|
[700] | 9 | // Those two headers can be replaced by the C++11 equivalent in the future |
---|
| 10 | |
---|
[696] | 11 | namespace xios |
---|
| 12 | { |
---|
| 13 | /*! |
---|
| 14 | \class CRegistry |
---|
[700] | 15 | This class is a registry database which store key with an associated value. Internally the value is stored as a memory bloc |
---|
| 16 | and the key is a string. The registry can be gathered and merge between MPI process, broadcast and read or wrote from a file |
---|
[696] | 17 | */ |
---|
| 18 | class CRegistry : virtual public CBaseType |
---|
| 19 | { |
---|
| 20 | public: |
---|
| 21 | |
---|
| 22 | /** Constructor, the communicator is used for bcast or gather operation between MPI processes */ |
---|
[1639] | 23 | CRegistry(const MPI_Comm& comm=MPI_COMM_WORLD) : communicator(comm) {} |
---|
[700] | 24 | |
---|
[696] | 25 | /** Copy constructor */ |
---|
| 26 | CRegistry(const CRegistry& reg) ; |
---|
| 27 | |
---|
[700] | 28 | |
---|
[696] | 29 | /** insert a value associated to a key*/ |
---|
[700] | 30 | void setKey(const std::string& key, const CBaseType& value) { this->setKey_(key,value); } |
---|
[696] | 31 | |
---|
| 32 | /** insert a value associated to a key*/ |
---|
[2629] | 33 | template<typename T> typename std::enable_if<!std::is_convertible<T&, CBaseType&>::value>::type |
---|
[700] | 34 | setKey(const std::string& key, const T& value) { this->setKey_(key,CType<T>(value)); } |
---|
[696] | 35 | |
---|
| 36 | |
---|
[700] | 37 | /** retrieve a value from a key */ |
---|
[2209] | 38 | void getKey(const std::string& key, CBaseType& value) const { this->getKey_(key,value); } |
---|
[696] | 39 | |
---|
[700] | 40 | /** retrieve a value from a key */ |
---|
[2629] | 41 | template<typename T> typename std::enable_if<!std::is_convertible<T&, CBaseType&>::value>::type |
---|
[2209] | 42 | getKey(const std::string& key, T& value) const { CType_ref<T> valRef(value); this->getKey_(key,valRef); } |
---|
[696] | 43 | |
---|
| 44 | |
---|
| 45 | /** query for an already inserted key */ |
---|
| 46 | bool foundKey(const std::string& key) const ; |
---|
[700] | 47 | |
---|
[696] | 48 | /** The registry is wrote into a memory buffer */ |
---|
| 49 | bool toBuffer(CBufferOut& buffer) const ; |
---|
| 50 | |
---|
| 51 | /** The registry is read from a memory buffer */ |
---|
| 52 | bool fromBuffer(CBufferIn& buffer) ; |
---|
| 53 | |
---|
| 54 | /** The registry is wrote to the file given by "filename". If the registry is empty no file is wrote */ |
---|
| 55 | void toFile(const string& filename) ; |
---|
| 56 | |
---|
[700] | 57 | /** The registry is read from the file given by "filename". If no file exist, the registry remain empty */ |
---|
[696] | 58 | void fromFile(const string& filename) ; |
---|
| 59 | |
---|
[700] | 60 | /** Merge the registry with an other. Existing keys in the current registry are not overwritten */ |
---|
[696] | 61 | void mergeRegistry(const CRegistry& inRegistry) ; |
---|
| 62 | |
---|
[700] | 63 | /** Broadcast registry from the root process (rank 0) to the other processes of the communicator */ |
---|
[696] | 64 | void bcastRegistry(void) ; |
---|
| 65 | |
---|
[700] | 66 | /** Gather registry to the root process (rank 0) from the other processes of the communicator */ |
---|
[696] | 67 | void gatherRegistry(void) ; |
---|
| 68 | |
---|
[700] | 69 | /** Gather registry with a hierarchical algorithm which avoid root process to get registries from whole processes of the communicator. |
---|
| 70 | Registry are merged two by two hierarchically. */ |
---|
[696] | 71 | void hierarchicalGatherRegistry(void) ; |
---|
| 72 | |
---|
| 73 | /** Destructor */ |
---|
| 74 | ~CRegistry() { reset() ; } |
---|
| 75 | |
---|
| 76 | /** Unimplemented, do not use (need for CBaseType pure virtual class) */ |
---|
| 77 | void fromString(const string& str) ; |
---|
| 78 | |
---|
[700] | 79 | /** Dump registry to a string (need for CBaseType pure virtual class)*/ |
---|
[696] | 80 | string toString(void) const ; |
---|
| 81 | |
---|
[700] | 82 | /** Clone the registry (need for CBaseType pure virtual class)*/ |
---|
[696] | 83 | CRegistry* clone(void) const { return new CRegistry(*this); } |
---|
| 84 | |
---|
[700] | 85 | /** return the size needed to bufferize the registry (need for CBaseType pure virtual class)*/ |
---|
[696] | 86 | size_t size(void) const ; |
---|
| 87 | |
---|
[700] | 88 | /** return true if the registry is empty (need for CBaseType pure virtual class)*/ |
---|
[696] | 89 | bool isEmpty(void) const { return registry.empty(); } |
---|
| 90 | |
---|
[700] | 91 | /** Clean the registry and delete associated memory (need for CBaseType pure virtual class)*/ |
---|
[696] | 92 | void reset(void) ; |
---|
| 93 | |
---|
[700] | 94 | /** Set the prefix added systematically to the keys, with "::" as separator*/ |
---|
[696] | 95 | void setPath(const string& str) { path=str+"::" ; } |
---|
[700] | 96 | |
---|
[696] | 97 | private: |
---|
| 98 | |
---|
| 99 | /** insert a value associated to a key (internal use)*/ |
---|
| 100 | void setKey_(const std::string& key, const CBaseType& value) ; |
---|
| 101 | |
---|
[700] | 102 | /** retrieve a value from a key (internal use)*/ |
---|
[2209] | 103 | void getKey_(const std::string& key, CBaseType& value) const ; |
---|
[700] | 104 | |
---|
[696] | 105 | /** use internally for recursivity */ |
---|
[1639] | 106 | void gatherRegistry(const MPI_Comm& comm) ; |
---|
[696] | 107 | |
---|
| 108 | /** use internally for recursivity */ |
---|
[1639] | 109 | void hierarchicalGatherRegistry(const MPI_Comm& comm) ; |
---|
[696] | 110 | |
---|
| 111 | |
---|
[700] | 112 | /** Prefix added systematically to the keys, with "::" as separator*/ |
---|
[696] | 113 | std::string path ; |
---|
| 114 | |
---|
| 115 | /** Map containing registry, the key is a string type and the value is stored in a pair with the size |
---|
[700] | 116 | * of the memory bloc and the associated pointer*/ |
---|
[696] | 117 | std::map<std::string,std::pair<size_t,char*> > registry ; |
---|
| 118 | |
---|
| 119 | /** MPI communicator used for broadcast and gather operation */ |
---|
[1639] | 120 | MPI_Comm communicator ; |
---|
[696] | 121 | } ; |
---|
| 122 | |
---|
| 123 | inline CMessage& operator<<(CMessage& msg, CRegistry& registry) |
---|
| 124 | { |
---|
| 125 | msg.push(registry) ; |
---|
| 126 | return msg ; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | inline CMessage& operator<<(CMessage& msg, const CRegistry& registry) |
---|
| 130 | { |
---|
| 131 | msg.push(registry) ; |
---|
| 132 | return msg ; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | #endif |
---|