SST 16.0.0
Structural Simulation Toolkit
get_array_size.h
1// Copyright 2009-2026 NTESS. Under the terms
2// of Contract DE-NA0003525 with NTESS, the U.S.
3// Government retains certain rights in this software.
4//
5// Copyright (c) 2009-2026, NTESS
6// All rights reserved.
7//
8// This file is part of the SST software package. For license
9// information, see the LICENSE file in the top level directory of the
10// distribution.
11
12#ifndef SST_CORE_SERIALIZATION_IMPL_GET_ARRAY_SIZE_H
13#define SST_CORE_SERIALIZATION_IMPL_GET_ARRAY_SIZE_H
14
15#ifndef SST_INCLUDING_SERIALIZER_H
16#warning \
17 "The header file sst/core/serialization/impl/get_array_size.h should not be directly included as it is not part of the stable public API. The file is included in sst/core/serialization/serializer.h"
18#endif
19
20#include <cstddef>
21#include <stdexcept>
22#include <type_traits>
23
24namespace SST::Core::Serialization {
25
26// Convert array size to size_t, flagging errors
27template <typename SIZE_T>
28std::enable_if_t<std::is_integral_v<SIZE_T>, size_t>
29get_array_size(SIZE_T size, const char* msg)
30{
31 bool range_error;
32 if constexpr ( std::is_unsigned_v<SIZE_T> )
33 range_error = size > SIZE_MAX;
34 else if constexpr ( sizeof(SIZE_T) > sizeof(size_t) )
35 range_error = size < 0 || size > static_cast<SIZE_T>(SIZE_MAX);
36 else
37 range_error = size < 0;
38 if ( range_error ) throw std::range_error(msg);
39 return static_cast<size_t>(size);
40}
41
42} // namespace SST::Core::Serialization
43
44#endif // SST_CORE_SERIALIZATION_IMPL_GET_ARRAY_SIZE_H