CoDiPack  3.1.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-2026 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 start;
58 size_t end;
59
60 public:
61
64
66 CODI_INLINE ByteDataView(char* pointer, size_t pos, size_t end)
67 : pointer(pointer), pos(pos), start(pos), end(end) {
68 codiAssert(pos <= end);
69 }
70
73 return end;
74 }
75
78 return pos;
79 }
80
83 return start;
84 }
85
87 CODI_INLINE void init(char* pointer, size_t pos, size_t end) {
88 this->pointer = pointer;
89 this->pos = pos;
90 this->start = pos;
91 this->end = end;
92
93 codiAssert(pos <= end);
94 }
95
97 template<typename T>
98 CODI_INLINE T* read(size_t size) {
99 T* convPointer = cast<T>();
100 pos += sizeof(T) * size;
101
102 codiAssert(pos <= end);
103
104 return convPointer;
105 }
106
108 template<typename T>
110 return read<T>(1)[0];
111 }
112
115 template<typename T>
116 CODI_INLINE T* reserve(size_t size) {
117 T* convPointer = cast<T>();
118 pos += sizeof(T) * size;
119
120 codiAssert(pos <= end);
121
122 return convPointer;
123 }
124
127 pos = start;
128 }
129
131 template<typename T>
132 CODI_INLINE T* write(T const& data) {
133 return write(&data, 1);
134 }
135
137 template<typename T>
138 CODI_INLINE T* write(T const* data, size_t size) {
139 T* convPointer = cast<T>();
140 for (size_t i = 0; i < size; i += 1) {
141 convPointer[i] = data[i];
142 }
143 pos += sizeof(T) * size;
144
145 codiAssert(pos <= end);
146
147 return convPointer;
148 }
149
150 private:
151
152 template<typename T>
153 CODI_INLINE T* cast() {
154 return reinterpret_cast<T*>(&pointer[pos]);
155 }
156 };
157
158}
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:469
#define codiAssert(x)
See codi::Config::EnableAssert.
Definition config.h:441
CoDiPack - Code Differentiation Package.
Definition codi.hpp:97
Definition byteDataView.hpp:51
inlineT * 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:116
inlineT * write(T const *data, size_t size)
Write an array of type T with length size.
Definition byteDataView.hpp:138
inlinevoid reset()
Reset the data position to the start of the data.
Definition byteDataView.hpp:126
inlineT * read(size_t size)
Read an array of length size of types T.
Definition byteDataView.hpp:98
inlinesize_t getPosition()
Get the current data position.
Definition byteDataView.hpp:77
inlinesize_t getEnd()
Get the end data position.
Definition byteDataView.hpp:72
inlinevoid init(char *pointer, size_t pos, size_t end)
Initialize the object.
Definition byteDataView.hpp:87
inlineT read()
Read a single object of type T.
Definition byteDataView.hpp:109
inlineT * write(T const &data)
Write a single entry of type T.
Definition byteDataView.hpp:132
inlinesize_t getStart()
Get the start data position.
Definition byteDataView.hpp:82