source: XIOS3/trunk/src/registry.hpp

Last change on this file was 2629, checked in by jderouillat, 2 months ago

Delete boost dependencies, the few features used are replaced by functions stored in extern/boost_extraction

File size: 4.8 KB
Line 
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
9// Those two headers can be replaced by the C++11 equivalent in the future
10
11namespace xios
12{
13/*!
14  \class CRegistry
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
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 */
23      CRegistry(const MPI_Comm& comm=MPI_COMM_WORLD) : communicator(comm) {}
24
25/** Copy constructor */
26      CRegistry(const CRegistry& reg) ;
27
28
29/** insert a value associated to a key*/
30      void setKey(const std::string& key, const CBaseType& value) { this->setKey_(key,value); }
31
32/** insert a value associated to a key*/
33      template<typename T> typename std::enable_if<!std::is_convertible<T&, CBaseType&>::value>::type
34      setKey(const std::string& key, const T& value) { this->setKey_(key,CType<T>(value)); }
35
36
37/** retrieve a value from a key */
38      void getKey(const std::string& key, CBaseType& value) const { this->getKey_(key,value); }
39
40/** retrieve a value from a key */
41      template<typename T> typename std::enable_if<!std::is_convertible<T&, CBaseType&>::value>::type
42      getKey(const std::string& key, T& value) const { CType_ref<T> valRef(value); this->getKey_(key,valRef); }
43
44
45/** query for an already inserted key */
46      bool foundKey(const std::string& key) const ;
47
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
57/** The registry is read from the file given by "filename". If no file exist, the registry remain empty */
58      void fromFile(const string& filename) ;
59
60/** Merge the registry with an other. Existing keys in the current registry are not overwritten */
61      void mergeRegistry(const CRegistry& inRegistry) ;
62
63/** Broadcast registry from the root process (rank 0) to the other processes of the communicator */
64      void bcastRegistry(void) ;
65
66/** Gather registry to the root process (rank 0) from the other processes of the communicator */
67      void gatherRegistry(void) ;
68
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. */
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
79/** Dump registry to a string (need for CBaseType pure virtual class)*/
80      string toString(void) const ;
81
82/** Clone the registry (need for CBaseType pure virtual class)*/
83      CRegistry* clone(void) const { return new CRegistry(*this); }
84
85/** return the size needed to bufferize the registry (need for CBaseType pure virtual class)*/
86      size_t size(void) const ;
87
88/** return true if the registry is empty (need for CBaseType pure virtual class)*/
89      bool isEmpty(void) const { return registry.empty(); }
90
91/** Clean the registry and delete associated memory (need for CBaseType pure virtual class)*/
92      void reset(void) ;
93
94/** Set the prefix added systematically to the keys, with "::" as separator*/
95      void setPath(const string& str) { path=str+"::" ; }
96
97    private:
98
99/** insert a value associated to a key (internal use)*/
100      void setKey_(const std::string& key, const CBaseType& value) ;
101
102/** retrieve a value from a key (internal use)*/
103      void getKey_(const std::string& key, CBaseType& value) const ;
104
105/** use internally for recursivity */
106      void gatherRegistry(const MPI_Comm& comm) ;
107
108/** use internally for recursivity */
109      void hierarchicalGatherRegistry(const MPI_Comm& comm) ;
110
111
112/** Prefix added systematically to the keys, with "::" as separator*/
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
116 *  of the memory bloc and the associated pointer*/
117      std::map<std::string,std::pair<size_t,char*> > registry ;
118
119/** MPI communicator used for broadcast and gather operation */
120      MPI_Comm communicator ;
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
Note: See TracBrowser for help on using the repository browser.