CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
byteDataView.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2024 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
5 * Homepage: http://scicomp.rptu.de
6 * Contact: Prof. Nicolas R. Gauger (codi@scicomp.uni-kl.de)
7 *
8 * Lead developers: Max Sagebaum, Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
9 *
10 * This file is part of CoDiPack (http://scicomp.rptu.de/software/codi).
11 *
12 * CoDiPack is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * CoDiPack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty
19 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU General Public License for more details.
22 * You should have received a copy of the GNU
23 * General Public License along with CoDiPack.
24 * If not, see <http://www.gnu.org/licenses/>.
25 *
26 * For other licensing options please contact us.
27 *
28 * Authors:
29 * - SciComp, University of Kaiserslautern-Landau:
30 * - Max Sagebaum
31 * - Johannes Blühdorn
32 * - Former members:
33 * - Tim Albring
34 */
35#pragma once
36
37#include <algorithm>
38#include <vector>
39
40#include "../config.h"
41#include "macros.hpp"
42
44namespace codi {
45
51 struct ByteDataView {
52 private:
53
54 char* pointer;
55 size_t pos;
56
57 size_t end;
58
59 public:
60
63
65 CODI_INLINE ByteDataView(char* pointer, size_t pos, size_t end) : pointer(pointer), pos(pos), end(end) {
66 codiAssert(pos <= end);
67 }
68
71 return pos;
72 }
73
75 CODI_INLINE void init(char* pointer, size_t pos, size_t end) {
76 this->pointer = pointer;
77 this->pos = pos;
78 this->end = end;
79
80 codiAssert(pos <= end);
81 }
82
84 template<typename T>
85 CODI_INLINE T* read(size_t size) {
86 T* convPointer = cast<T>();
87 pos += sizeof(T) * size;
88
89 codiAssert(pos <= end);
90
91 return convPointer;
92 }
93
95 template<typename T>
97 return read<T>(1)[0];
98 }
99
102 template<typename T>
103 CODI_INLINE T* reserve(size_t size) {
104 T* convPointer = cast<T>();
105 pos += sizeof(T) * size;
106
107 codiAssert(pos <= end);
108
109 return convPointer;
110 }
111
113 template<typename T>
114 CODI_INLINE T* write(T const& data) {
115 return write(&data, 1);
116 }
117
119 template<typename T>
120 CODI_INLINE T* write(T const* data, size_t size) {
121 T* convPointer = cast<T>();
122 for (size_t i = 0; i < size; i += 1) {
123 convPointer[i] = data[i];
124 }
125 pos += sizeof(T) * size;
126
127 codiAssert(pos <= end);
128
129 return convPointer;
130 }
131
132 private:
133
134 template<typename T>
135 CODI_INLINE T* cast() {
136 return reinterpret_cast<T*>(&pointer[pos]);
137 }
138 };
139
140}
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:457
#define codiAssert(x)
See codi::Config::EnableAssert.
Definition config.h:432
CoDiPack - Code Differentiation Package.
Definition codi.hpp:90
Definition byteDataView.hpp:51
T * reserve(size_t size)
Reserve memory for an array of type T with length size. The returned pointer can be written with the ...
Definition byteDataView.hpp:103
T * read(size_t size)
Read an array of length size of types T.
Definition byteDataView.hpp:85
T * write(T const &data)
Write a single entry of type T.
Definition byteDataView.hpp:114
T * write(T const *data, size_t size)
Write an array of type T with length size.
Definition byteDataView.hpp:120
ByteDataView(char *pointer, size_t pos, size_t end)
Constructor.
Definition byteDataView.hpp:65
size_t getPosition()
Get the current data position.
Definition byteDataView.hpp:70
T read()
Read a single object of type T.
Definition byteDataView.hpp:96
ByteDataView()=default
Empty initialization. Innit needs to be called on this object.
void init(char *pointer, size_t pos, size_t end)
Initialize the object.
Definition byteDataView.hpp:75