OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
MsgBuffer.h
Go to the documentation of this file.
1//
2// Class MsgBuffer
3// MsgBuffer class to allow serializing message objects into plain buffers
4// to send directly with mpi calls or similar means
5//
6// Copyright (c) 2008 - 2020, Paul Scherrer Institut, Villigen PSI, Switzerland
7// All rights reserved
8//
9// This file is part of OPAL.
10//
11// OPAL is free software: you can redistribute it and/or modify
12// it under the terms of the GNU General Public License as published by
13// the Free Software Foundation, either version 3 of the License, or
14// (at your option) any later version.
15//
16// You should have received a copy of the GNU General Public License
17// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
18//
19#ifndef MSGBUFFER_H
20#define MSGBUFFER_H
21
22#include <algorithm>
23#include <cstring>
24#include <vector>
25#include "Message/Message.h"
26
27class Format;
28
29class MsgBuffer {
30public:
31 // creates buffer with space to hold count messages of format f
32 MsgBuffer(Format* f, int count, int offset = 0);
33 MsgBuffer(Format* f, char* d, int size);
34
35 bool add(Message*);
36 Message* get();
37
38 template <class T>
39 void get(T& v) {
40 std::memcpy(&v, data.data() + readpos, sizeof(T));
41 readpos += sizeof(T);
42 }
43
44 template <class T>
45 void put(T& v) {
46 std::memcpy(data.data() + writepos, &v, sizeof(T));
47 writepos += sizeof(T);
48 }
49
50 int getSize() {
51 return writepos;
52 }
53
54 void* getBuffer() {
55 char* data_ptr = data.empty() ? static_cast<char*>(0) : &(data[0]);
56 return data_ptr;
57 }
58
60 return format;
61 }
62
63 ~MsgBuffer();
64
65private:
67 unsigned int datasize, writepos, readpos;
68 std::vector<char> data;
69};
70
71#endif
Definition: Format.h:27
void * getBuffer()
Definition: MsgBuffer.h:54
bool add(Message *)
Definition: MsgBuffer.cpp:44
void get(T &v)
Definition: MsgBuffer.h:39
void put(T &v)
Definition: MsgBuffer.h:45
Format * getFormat()
Definition: MsgBuffer.h:59
MsgBuffer(Format *f, int count, int offset=0)
Definition: MsgBuffer.cpp:23
int getSize()
Definition: MsgBuffer.h:50
unsigned int readpos
Definition: MsgBuffer.h:67
unsigned int writepos
Definition: MsgBuffer.h:67
std::vector< char > data
Definition: MsgBuffer.h:68
Message * get()
Definition: MsgBuffer.cpp:71
Format * format
Definition: MsgBuffer.h:66
unsigned int datasize
Definition: MsgBuffer.h:67