DUNE PDELab (git)

polymorphicbufferwrapper.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_PDELAB_COMMON_POLYMORPHICBUFFERWRAPPER_HH
4#define DUNE_PDELAB_COMMON_POLYMORPHICBUFFERWRAPPER_HH
5
6#include <cstddef>
7#include <cassert>
8
9namespace Dune {
10 namespace PDELab {
11
13
30 template<typename Buffer>
32 {
33
34 public:
35
36 enum class Mode
37 {
38 send,
39 receive,
40 };
41
42 template<typename T>
43 void write(const T& data)
44 {
45 assert(_mode == Mode::send);
46 const char* raw_data = reinterpret_cast<const char*>(&data);
47 for (std::size_t i = 0; i < sizeof(T); ++i)
48 _buffer.write(*(raw_data++));
49 }
50
51 template<typename T>
52 void read(T& data)
53 {
54 assert(_mode == Mode::receive);
55 char* raw_data = reinterpret_cast<char*>(&data);
56 for (std::size_t i = 0; i < sizeof(T); ++i)
57 _buffer.read(*(raw_data++));
58 }
59
60 PolymorphicBufferWrapper(Buffer& buffer, Mode mode, int rank = -1, bool transmit_rank = false)
61 : _buffer(buffer)
62 , _mode(mode)
63 , _rank(rank)
64 , _transmit_rank(transmit_rank)
65 , _sender_rank(-1)
66 {
67 if (_transmit_rank)
68 {
69 assert(_rank >= 0);
70 if (_mode == Mode::receive)
71 read(_sender_rank);
72 else
73 write(_rank);
74 }
75 }
76
77 Mode mode() const
78 {
79 return _mode;
80 }
81
82 bool transmitRank() const
83 {
84 return _transmit_rank;
85 }
86
87 int senderRank() const
88 {
89 assert(_sender_rank >= 0);
90 return _sender_rank;
91 }
92
93 private:
94
95 Buffer& _buffer;
96 const Mode _mode;
97 int _rank;
98 const bool _transmit_rank;
99 int _sender_rank;
100
101 };
102
103 } // namespace PDELab
104} // namespace Dune
105
106#endif // DUNE_PDELAB_COMMON_POLYMORPHICBUFFERWRAPPER_HH
Wrapper for message buffers of grid DataHandles that allows for sending different types of data.
Definition: polymorphicbufferwrapper.hh:32
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 23, 23:29, 2024)