OGS
PropertyVectorMetaData.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5
6#include <istream>
7#include <optional>
8#include <ostream>
9#include <string>
10
11#include "BaseLib/Logging.h"
12
13namespace MeshLib
14{
15namespace IO
16{
17
19{
20 std::string property_name;
23 bool is_int_type = false;
26 bool is_data_type_signed = false;
27 unsigned long data_type_size_in_bytes = 0;
28 unsigned long number_of_components = 0;
29 unsigned long number_of_tuples = 0;
30
31 template <typename T>
33 {
34 is_int_type = std::numeric_limits<T>::is_integer;
35 is_data_type_signed = std::numeric_limits<T>::is_signed;
36 data_type_size_in_bytes = sizeof(T);
37 }
38};
39
40inline void writePropertyVectorMetaData(std::ostream& os,
41 PropertyVectorMetaData const& pvmd)
42{
43 std::string::size_type s(pvmd.property_name.length());
44 os.write(reinterpret_cast<char*>(&s), sizeof(std::string::size_type));
45
46 os.write(
47 const_cast<char*>(
48 const_cast<PropertyVectorMetaData&>(pvmd).property_name.data()),
49 s);
50 os.write(reinterpret_cast<char*>(
51 &const_cast<PropertyVectorMetaData&>(pvmd).is_int_type),
52 sizeof(bool));
53 os.write(
54 reinterpret_cast<char*>(
55 &const_cast<PropertyVectorMetaData&>(pvmd).is_data_type_signed),
56 sizeof(bool));
57 os.write(
58 reinterpret_cast<char*>(
59 &const_cast<PropertyVectorMetaData&>(pvmd).data_type_size_in_bytes),
60 sizeof(unsigned long));
61 os.write(
62 reinterpret_cast<char*>(
63 &const_cast<PropertyVectorMetaData&>(pvmd).number_of_components),
64 sizeof(unsigned long));
65 os.write(reinterpret_cast<char*>(
66 &const_cast<PropertyVectorMetaData&>(pvmd).number_of_tuples),
67 sizeof(unsigned long));
68}
69
71{
72 DBUG(
73 "name: '{:s}':\t is_int_data_type: {:d}, is_data_type_signed: "
74 "{:d}, data_type_size_in_bytes: {:d}, number of components / "
75 "tuples: {:d} / {:d}",
78 pvmd.number_of_tuples);
79}
80
81inline std::optional<PropertyVectorMetaData> readPropertyVectorMetaData(
82 std::istream& is)
83{
84 // read the size of the name of the PropertyVector
85 std::string::size_type s = 0;
86 if (!is.read(reinterpret_cast<char*>(&s), sizeof(std::string::size_type)))
87 {
88 return std::optional<PropertyVectorMetaData>();
89 }
90
92 char* dummy = new char[s];
93 if (!is.read(dummy, s))
94 {
95 return std::nullopt;
96 }
97 pvmd.property_name = std::string(dummy, s);
98 delete[] dummy;
99
100 if (!is.read(reinterpret_cast<char*>(&pvmd.is_int_type), sizeof(bool)))
101 {
102 return std::nullopt;
103 }
104 if (!is.read(reinterpret_cast<char*>(&pvmd.is_data_type_signed),
105 sizeof(bool)))
106 {
107 return std::nullopt;
108 }
109 if (!is.read(reinterpret_cast<char*>(&pvmd.data_type_size_in_bytes),
110 sizeof(unsigned long)))
111 {
112 return std::nullopt;
113 }
114 if (!is.read(reinterpret_cast<char*>(&pvmd.number_of_components),
115 sizeof(unsigned long)))
116 {
117 return std::nullopt;
118 }
119 if (!is.read(reinterpret_cast<char*>(&pvmd.number_of_tuples),
120 sizeof(unsigned long)))
121 {
122 return std::nullopt;
123 }
124 return std::optional<PropertyVectorMetaData>(pvmd);
125}
126
128{
129 unsigned long offset = 0;
130 unsigned long number_of_tuples = 0;
131};
132
134 std::ostream& os, PropertyVectorPartitionMetaData const& pvpmd)
135{
136 os.write(reinterpret_cast<char*>(
137 &const_cast<PropertyVectorPartitionMetaData&>(pvpmd).offset),
138 sizeof(unsigned long));
139 os.write(reinterpret_cast<char*>(
140 &const_cast<PropertyVectorPartitionMetaData&>(pvpmd)
141 .number_of_tuples),
142 sizeof(unsigned long));
143}
144
145inline std::optional<PropertyVectorPartitionMetaData>
147{
149 if (!is.read(reinterpret_cast<char*>(&pvpmd.offset), sizeof(unsigned long)))
150 {
151 return std::optional<PropertyVectorPartitionMetaData>();
152 }
153 if (!is.read(reinterpret_cast<char*>(&pvpmd.number_of_tuples),
154 sizeof(unsigned long)))
155 {
156 return std::optional<PropertyVectorPartitionMetaData>();
157 }
158 return std::optional<PropertyVectorPartitionMetaData>(pvpmd);
159}
160} // namespace IO
161} // namespace MeshLib
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
void writePropertyVectorPartitionMetaData(std::ostream &os, PropertyVectorPartitionMetaData const &pvpmd)
void writePropertyVectorMetaData(std::ostream &os, PropertyVectorMetaData const &pvmd)
std::optional< PropertyVectorMetaData > readPropertyVectorMetaData(std::istream &is)
std::optional< PropertyVectorPartitionMetaData > readPropertyVectorPartitionMetaData(std::istream &is)