source: XIOS/dev/branch_yushan/src/registry.hpp @ 1080

Last change on this file since 1080 was 1080, checked in by yushan, 5 years ago

Under development : save modification

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