STIR  6.3.0
format.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2025, University College London
3  This file is part of STIR.
4 
5  SPDX-License-Identifier: Apache-2.0
6 
7  See STIR/LICENSE.txt for details
8 */
9 
10 #ifndef __stir_FORMAT__
11 #define __stir_FORMAT__
12 
22 #include <string>
23 #include <utility> // for std::forward
24 #include "stir/common.h"
25 
26 #if __cplusplus >= 202002L // C++-20
27 # include <version> // need this for __cpp_lib_format
28 #endif
29 
30 #if defined(__cpp_lib_format) && (__cpp_lib_format >= 201907L)
31 # include <format>
32 namespace internal_format = std; // using std::format;
33 #else
34 # define FMT_HEADER_ONLY 1
35 # if defined(_MSC_VER)
36 // MS VC compiler needs the /utf-8 compiler switch to be able to handle fmt with Unicode support
37 // Instead, we just disable it.
38 # define FMT_UNICODE 0
39 # endif
40 # include "fmt/format.h"
41 namespace internal_format = fmt; // using fmt::format;
42 #endif
43 
44 START_NAMESPACE_STIR
45 
46 #if defined(__cpp_lib_format) && (__cpp_lib_format >= 201907L)
47 using std::format;
48 #else
49 
50 template <typename... Args>
51 std::string
52 format(fmt::format_string<Args...> fmt, Args&&... args)
53 {
54  return fmt::format(fmt, std::forward<Args>(args)...);
55 }
56 #endif
57 
58 // this is an alternative function definition for instances where the format string is not known at compile time
59 // likely will be in c++26
60 template <typename... Args>
61 std::string
62 runtime_format(internal_format::string_view fmt, Args&&... args)
63 {
64 #if defined(__cpp_lib_format) && (__cpp_lib_format >= 201907L)
65  return internal_format::vformat(fmt, std::make_format_args(args...));
66 #else
67  return internal_format::format(fmt::runtime(fmt), std::forward<Args>(args)...);
68 #endif
69 }
70 
71 END_NAMESPACE_STIR
72 
73 #endif // __stir_FORMAT__
STL namespace.
basic configuration include file