Skip to content

Commit f489633

Browse files
franzpoeschelpre-commit-ci[bot]ax3l
authored
Enable clang-format also for .tpp files by using a regex instead of a predefined filter (#1403)
* Manually specify file regex in clang-format hook * Some whitespace change to trigger pre-commit * Add a comment on what we did * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * More C++ and Json * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Axel Huebl <axel.huebl@plasma.ninja>
1 parent 5f0293f commit f489633

File tree

3 files changed

+103
-116
lines changed

3 files changed

+103
-116
lines changed

.pre-commit-config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ repos:
6969
rev: v16.0.0
7070
hooks:
7171
- id: clang-format
72+
# By default, the clang-format hook configures:
73+
# 'types_or': [c++, c, c#, cuda, java, javascript, json, objective-c, proto, textproto]
74+
# Unfortunately, the c++ option does not recognize .tpp files, so we need to do this manually
75+
# Since file filters in pre-commit work by logical AND, it's only possible to narrow the filter definition
76+
# So, we add a regex for the type of file that we want and additionally disable the 'types_or'
77+
# option entirely.
78+
'types_or': []
79+
files: .*\.(tpp|h|hpp|hpp\.in|cpp|cxx|js|json)$
7280

7381
# Autoremoves unused Python imports
7482
- repo: https://github.com/hadialqattan/pycln

include/openPMD/RecordComponent.tpp

Lines changed: 89 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -30,57 +30,53 @@
3030

3131
#include <memory>
3232

33-
3433
namespace openPMD
3534
{
36-
template< typename T >
37-
inline RecordComponent&
38-
RecordComponent::makeConstant(T value)
35+
template <typename T>
36+
inline RecordComponent &RecordComponent::makeConstant(T value)
3937
{
40-
if( written() )
41-
throw std::runtime_error("A recordComponent can not (yet) be made constant after it has been written.");
38+
if (written())
39+
throw std::runtime_error(
40+
"A recordComponent can not (yet) be made constant after it has "
41+
"been written.");
4242

43-
auto & rc = get();
43+
auto &rc = get();
4444

4545
rc.m_constantValue = Attribute(value);
4646
rc.m_isConstant = true;
4747
return *this;
4848
}
4949

50-
template< typename T >
51-
inline RecordComponent&
52-
RecordComponent::makeEmpty( uint8_t dimensions )
50+
template <typename T>
51+
inline RecordComponent &RecordComponent::makeEmpty(uint8_t dimensions)
5352
{
54-
return makeEmpty( Dataset(
55-
determineDatatype< T >(),
56-
Extent( dimensions, 0 ) ) );
53+
return makeEmpty(Dataset(determineDatatype<T>(), Extent(dimensions, 0)));
5754
}
5855

59-
template< typename T >
60-
inline std::shared_ptr< T > RecordComponent::loadChunk(
61-
Offset o, Extent e )
56+
template <typename T>
57+
inline std::shared_ptr<T> RecordComponent::loadChunk(Offset o, Extent e)
6258
{
6359
uint8_t dim = getDimensionality();
6460

6561
// default arguments
6662
// offset = {0u}: expand to right dim {0u, 0u, ...}
6763
Offset offset = o;
68-
if( o.size() == 1u && o.at(0) == 0u && dim > 1u )
64+
if (o.size() == 1u && o.at(0) == 0u && dim > 1u)
6965
offset = Offset(dim, 0u);
7066

7167
// extent = {-1u}: take full size
7268
Extent extent(dim, 1u);
73-
if( e.size() == 1u && e.at(0) == -1u )
69+
if (e.size() == 1u && e.at(0) == -1u)
7470
{
7571
extent = getExtent();
76-
for( uint8_t i = 0u; i < dim; ++i )
72+
for (uint8_t i = 0u; i < dim; ++i)
7773
extent[i] -= offset[i];
7874
}
7975
else
8076
extent = e;
8177

8278
uint64_t numPoints = 1u;
83-
for( auto const& dimensionSize : extent )
79+
for (auto const &dimensionSize : extent)
8480
numPoints *= dimensionSize;
8581

8682
#if (defined(_LIBCPP_VERSION) && _LIBCPP_VERSION < 11000) || \
@@ -96,83 +92,84 @@ inline std::shared_ptr< T > RecordComponent::loadChunk(
9692
#endif
9793
}
9894

99-
template< typename T >
100-
inline void RecordComponent::loadChunk(
101-
std::shared_ptr< T > data,
102-
Offset o,
103-
Extent e )
95+
template <typename T>
96+
inline void
97+
RecordComponent::loadChunk(std::shared_ptr<T> data, Offset o, Extent e)
10498
{
10599
Datatype dtype = determineDatatype(data);
106-
if( dtype != getDatatype() )
107-
if( !isSameInteger< T >( getDatatype() ) &&
108-
!isSameFloatingPoint< T >( getDatatype() ) &&
109-
!isSameComplexFloatingPoint< T >( getDatatype() ) )
100+
if (dtype != getDatatype())
101+
if (!isSameInteger<T>(getDatatype()) &&
102+
!isSameFloatingPoint<T>(getDatatype()) &&
103+
!isSameComplexFloatingPoint<T>(getDatatype()))
110104
{
111105
std::string const data_type_str = datatypeToString(getDatatype());
112-
std::string const requ_type_str = datatypeToString(determineDatatype<T>());
113-
std::string err_msg = "Type conversion during chunk loading not yet implemented! ";
106+
std::string const requ_type_str =
107+
datatypeToString(determineDatatype<T>());
108+
std::string err_msg =
109+
"Type conversion during chunk loading not yet implemented! ";
114110
err_msg += "Data: " + data_type_str + "; Load as: " + requ_type_str;
115-
throw std::runtime_error( err_msg );
111+
throw std::runtime_error(err_msg);
116112
}
117113

118114
uint8_t dim = getDimensionality();
119115

120116
// default arguments
121117
// offset = {0u}: expand to right dim {0u, 0u, ...}
122118
Offset offset = o;
123-
if( o.size() == 1u && o.at(0) == 0u && dim > 1u )
119+
if (o.size() == 1u && o.at(0) == 0u && dim > 1u)
124120
offset = Offset(dim, 0u);
125121

126122
// extent = {-1u}: take full size
127123
Extent extent(dim, 1u);
128-
if( e.size() == 1u && e.at(0) == -1u )
124+
if (e.size() == 1u && e.at(0) == -1u)
129125
{
130126
extent = getExtent();
131-
for( uint8_t i = 0u; i < dim; ++i )
127+
for (uint8_t i = 0u; i < dim; ++i)
132128
extent[i] -= offset[i];
133129
}
134130
else
135131
extent = e;
136132

137-
if( extent.size() != dim || offset.size() != dim )
133+
if (extent.size() != dim || offset.size() != dim)
138134
{
139135
std::ostringstream oss;
140136
oss << "Dimensionality of chunk ("
141137
<< "offset=" << offset.size() << "D, "
142138
<< "extent=" << extent.size() << "D) "
143-
<< "and record component ("
144-
<< int(dim) << "D) "
139+
<< "and record component (" << int(dim) << "D) "
145140
<< "do not match.";
146141
throw std::runtime_error(oss.str());
147142
}
148143
Extent dse = getExtent();
149-
for( uint8_t i = 0; i < dim; ++i )
150-
if( dse[i] < offset[i] + extent[i] )
151-
throw std::runtime_error("Chunk does not reside inside dataset (Dimension on index " + std::to_string(i)
152-
+ ". DS: " + std::to_string(dse[i])
153-
+ " - Chunk: " + std::to_string(offset[i] + extent[i])
154-
+ ")");
155-
if( !data )
156-
throw std::runtime_error("Unallocated pointer passed during chunk loading.");
157-
158-
auto & rc = get();
159-
if( constant() )
144+
for (uint8_t i = 0; i < dim; ++i)
145+
if (dse[i] < offset[i] + extent[i])
146+
throw std::runtime_error(
147+
"Chunk does not reside inside dataset (Dimension on index " +
148+
std::to_string(i) + ". DS: " + std::to_string(dse[i]) +
149+
" - Chunk: " + std::to_string(offset[i] + extent[i]) + ")");
150+
if (!data)
151+
throw std::runtime_error(
152+
"Unallocated pointer passed during chunk loading.");
153+
154+
auto &rc = get();
155+
if (constant())
160156
{
161157
uint64_t numPoints = 1u;
162-
for( auto const& dimensionSize : extent )
158+
for (auto const &dimensionSize : extent)
163159
numPoints *= dimensionSize;
164160

165-
T value = rc.m_constantValue.get< T >();
161+
T value = rc.m_constantValue.get<T>();
166162

167-
T* raw_ptr = data.get();
163+
T *raw_ptr = data.get();
168164
std::fill(raw_ptr, raw_ptr + numPoints, value);
169-
} else
165+
}
166+
else
170167
{
171-
Parameter< Operation::READ_DATASET > dRead;
168+
Parameter<Operation::READ_DATASET> dRead;
172169
dRead.offset = offset;
173170
dRead.extent = extent;
174171
dRead.dtype = getDatatype();
175-
dRead.data = std::static_pointer_cast< void >(data);
172+
dRead.data = std::static_pointer_cast<void>(data);
176173
rc.m_chunks.push(IOTask(this, dRead));
177174
}
178175
}
@@ -190,13 +187,10 @@ inline void RecordComponent::loadChunk(
190187
template <typename T>
191188
inline void RecordComponent::loadChunkRaw(T *ptr, Offset offset, Extent extent)
192189
{
193-
loadChunk(
194-
auxiliary::shareRaw(ptr),
195-
std::move(offset),
196-
std::move(extent));
190+
loadChunk(auxiliary::shareRaw(ptr), std::move(offset), std::move(extent));
197191
}
198192

199-
template< typename T >
193+
template <typename T>
200194
inline void
201195
RecordComponent::storeChunk(std::shared_ptr<T> data, Offset o, Extent e)
202196
{
@@ -250,13 +244,10 @@ RecordComponent::storeChunk(std::shared_ptr<T[]> data, Offset o, Extent e)
250244
template <typename T>
251245
void RecordComponent::storeChunkRaw(T *ptr, Offset offset, Extent extent)
252246
{
253-
storeChunk(
254-
auxiliary::shareRaw(ptr),
255-
std::move(offset),
256-
std::move(extent));
247+
storeChunk(auxiliary::shareRaw(ptr), std::move(offset), std::move(extent));
257248
}
258249

259-
template< typename T_ContiguousContainer >
250+
template <typename T_ContiguousContainer>
260251
inline typename std::enable_if_t<
261252
auxiliary::IsContiguousContainer_v<T_ContiguousContainer> >
262253
RecordComponent::storeChunk(T_ContiguousContainer &data, Offset o, Extent e)
@@ -266,64 +257,57 @@ RecordComponent::storeChunk(T_ContiguousContainer &data, Offset o, Extent e)
266257
// default arguments
267258
// offset = {0u}: expand to right dim {0u, 0u, ...}
268259
Offset offset = o;
269-
if( o.size() == 1u && o.at(0) == 0u && dim > 1u )
260+
if (o.size() == 1u && o.at(0) == 0u && dim > 1u)
270261
offset = Offset(dim, 0u);
271262

272263
// extent = {-1u}: take full size
273264
Extent extent(dim, 1u);
274265
// avoid outsmarting the user:
275266
// - stdlib data container implement 1D -> 1D chunk to write
276-
if( e.size() == 1u && e.at(0) == -1u && dim == 1u )
267+
if (e.size() == 1u && e.at(0) == -1u && dim == 1u)
277268
extent.at(0) = data.size();
278269
else
279270
extent = e;
280271

281-
storeChunk(
282-
auxiliary::shareRaw(data.data()),
283-
offset,
284-
extent);
272+
storeChunk(auxiliary::shareRaw(data.data()), offset, extent);
285273
}
286274

287-
template< typename T, typename F >
288-
inline DynamicMemoryView< T >
289-
RecordComponent::storeChunk( Offset o, Extent e, F && createBuffer )
275+
template <typename T, typename F>
276+
inline DynamicMemoryView<T>
277+
RecordComponent::storeChunk(Offset o, Extent e, F &&createBuffer)
290278
{
291-
if( constant() )
279+
if (constant())
292280
throw std::runtime_error(
293-
"Chunks cannot be written for a constant RecordComponent." );
294-
if( empty() )
281+
"Chunks cannot be written for a constant RecordComponent.");
282+
if (empty())
295283
throw std::runtime_error(
296-
"Chunks cannot be written for an empty RecordComponent." );
284+
"Chunks cannot be written for an empty RecordComponent.");
297285
Datatype dtype = determineDatatype<T>();
298-
if( dtype != getDatatype() )
286+
if (dtype != getDatatype())
299287
{
300288
std::ostringstream oss;
301-
oss << "Datatypes of chunk data ("
302-
<< dtype
303-
<< ") and record component ("
304-
<< getDatatype()
305-
<< ") do not match.";
289+
oss << "Datatypes of chunk data (" << dtype
290+
<< ") and record component (" << getDatatype() << ") do not match.";
306291
throw std::runtime_error(oss.str());
307292
}
308293
uint8_t dim = getDimensionality();
309-
if( e.size() != dim || o.size() != dim )
294+
if (e.size() != dim || o.size() != dim)
310295
{
311296
std::ostringstream oss;
312297
oss << "Dimensionality of chunk ("
313298
<< "offset=" << o.size() << "D, "
314299
<< "extent=" << e.size() << "D) "
315-
<< "and record component ("
316-
<< int(dim) << "D) "
300+
<< "and record component (" << int(dim) << "D) "
317301
<< "do not match.";
318302
throw std::runtime_error(oss.str());
319303
}
320304
Extent dse = getExtent();
321-
for( uint8_t i = 0; i < dim; ++i )
322-
if( dse[i] < o[i] + e[i] )
323-
throw std::runtime_error("Chunk does not reside inside dataset (Dimension on index " + std::to_string(i)
324-
+ ". DS: " + std::to_string(dse[i])
325-
+ " - Chunk: " + std::to_string(o[i] + e[i])
326-
+ ")");
305+
for (uint8_t i = 0; i < dim; ++i)
306+
if (dse[i] < o[i] + e[i])
307+
throw std::runtime_error(
308+
"Chunk does not reside inside dataset (Dimension on index " +
309+
std::to_string(i) + ". DS: " + std::to_string(dse[i]) +
310+
" - Chunk: " + std::to_string(o[i] + e[i]) + ")");
327311

328312
/*
329313
* The openPMD backend might not yet know about this dataset.
@@ -333,25 +317,25 @@ RecordComponent::storeChunk( Offset o, Extent e, F && createBuffer )
333317
seriesFlush({FlushLevel::SkeletonOnly});
334318

335319
size_t size = 1;
336-
for( auto ext : e )
320+
for (auto ext : e)
337321
{
338322
size *= ext;
339323
}
340324
/*
341325
* Flushing the skeleton does not create datasets,
342326
* so we might need to do it now.
343327
*/
344-
if( !written() )
328+
if (!written())
345329
{
346-
auto & rc = get();
347-
Parameter< Operation::CREATE_DATASET > dCreate;
330+
auto &rc = get();
331+
Parameter<Operation::CREATE_DATASET> dCreate;
348332
dCreate.name = rc.m_name;
349333
dCreate.extent = getExtent();
350334
dCreate.dtype = getDatatype();
351335
dCreate.options = rc.m_dataset.options;
352336
IOHandler()->enqueue(IOTask(this, dCreate));
353337
}
354-
Parameter< Operation::GET_BUFFER_VIEW > getBufferView;
338+
Parameter<Operation::GET_BUFFER_VIEW> getBufferView;
355339
getBufferView.offset = o;
356340
getBufferView.extent = e;
357341
getBufferView.dtype = getDatatype();
@@ -369,22 +353,17 @@ RecordComponent::storeChunk( Offset o, Extent e, F && createBuffer )
369353
return DynamicMemoryView<T>{std::move(getBufferView), size, *this};
370354
}
371355

372-
template< typename T >
373-
inline DynamicMemoryView< T >
374-
RecordComponent::storeChunk( Offset offset, Extent extent )
356+
template <typename T>
357+
inline DynamicMemoryView<T>
358+
RecordComponent::storeChunk(Offset offset, Extent extent)
375359
{
376-
return storeChunk< T >(
377-
std::move( offset ),
378-
std::move( extent ),
379-
[]( size_t size )
380-
{
360+
return storeChunk<T>(std::move(offset), std::move(extent), [](size_t size) {
381361
#if (defined(_LIBCPP_VERSION) && _LIBCPP_VERSION < 11000) || \
382362
(defined(__apple_build_version__) && __clang_major__ < 14)
383-
return std::shared_ptr< T >{
384-
new T[ size ], []( auto * ptr ) { delete[] ptr; } };
363+
return std::shared_ptr<T>{new T[size], [](auto *ptr) { delete[] ptr; }};
385364
#else
386365
return std::shared_ptr< T[] >{ new T[ size ] };
387366
#endif
388-
} );
389-
}
367+
});
390368
}
369+
} // namespace openPMD

0 commit comments

Comments
 (0)