OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Formatter.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  *
7  * Visit http://people.web.psi.ch/adelmann/ for more details
8  *
9  ***************************************************************************/
10 
11 #ifndef FORMATTER_H
12 #define FORMATTER_H
13 
14 #include "Message/Message.h"
15 #include <cstring>
16 #include <vector>
17 #include <algorithm>
18 
19 /*
20  * Format and MsgBuffer class to allow serializing message objects into plain buffers
21  * to send directly with mpi calls or similar means
22  */
23 
24 class Format
25 {
26 public:
27  Format(Message*);
28  unsigned int getItemCount()
29  {
30  return items;
31  }
32  unsigned int getSize()
33  {
34  return size;
35  }
36  unsigned int getFormatSize()
37  {
38  return 2*items*sizeof(int);
39  }
40  unsigned int getItemElems(int i)
41  {
42  return format_array[2*i+0];
43  }
44  unsigned int getItemBytes(int i)
45  {
46  return format_array[2*i+1];
47  }
48 
49  void print();
50 
51 private:
52  unsigned int items, size;
53  std::vector<unsigned int> format_array;
54 };
55 
56 
57 class MsgBuffer
58 {
59 public:
60  //creates buffer with space to hold count messages of format f
61  MsgBuffer(Format *f, int count, int offset = 0);
62  MsgBuffer(Format *f, char* d, int size);
63 
64  bool add(Message*);
65  Message* get();
66 
67  template<class T>
68  void get(T &v)
69  {
70  std::memcpy(&v, data.data()+readpos, sizeof(T));
71  readpos += sizeof(T);
72  }
73 
74  template<class T>
75  void put(T &v)
76  {
77  std::memcpy(data.data()+writepos, &v, sizeof(T));
78  writepos += sizeof(T);
79  }
80 
81  int getSize()
82  {
83  return writepos;
84  }
85  void* getBuffer()
86  {
87  return data.data();
88  }
89 
90  Format* getFormat() { return format; }
91 
92  ~MsgBuffer();
93 private:
95  unsigned int datasize, writepos, readpos;
96  std::vector<char> data;
97 };
98 
99 #endif // FORMATTER_H
unsigned int getSize()
Definition: Formatter.h:32
unsigned int getFormatSize()
Definition: Formatter.h:36
int getSize()
Definition: Formatter.h:81
Format(Message *)
Definition: Formatter.cpp:3
unsigned int items
Definition: Formatter.h:52
Definition: rbendmap.h:8
void * getBuffer()
Definition: Formatter.h:85
std::vector< char > data
Definition: Formatter.h:96
unsigned int size
Definition: Formatter.h:52
std::vector< unsigned int > format_array
Definition: Formatter.h:53
unsigned int getItemCount()
Definition: Formatter.h:28
unsigned int readpos
Definition: Formatter.h:95
void print()
Definition: Formatter.cpp:18
unsigned int getItemBytes(int i)
Definition: Formatter.h:44
Format * getFormat()
Definition: Formatter.h:90
Format * format
Definition: Formatter.h:94
unsigned int writepos
Definition: Formatter.h:95
unsigned int datasize
Definition: Formatter.h:95
unsigned int getItemElems(int i)
Definition: Formatter.h:40
MsgBuffer(Format *f, int count, int offset=0)
Definition: Formatter.cpp:28
bool add(Message *)
Definition: Formatter.cpp:49
void put(T &v)
Definition: Formatter.h:75