Diligent Engine  v.2.4.g
FormatString.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2021 Diligent Graphics LLC
3  * Copyright 2015-2019 Egor Yusov
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * In no event and under no legal theory, whether in tort (including negligence),
18  * contract, or otherwise, unless required by applicable law (such as deliberate
19  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
20  * liable for any damages, including any direct, indirect, special, incidental,
21  * or consequential damages of any character arising as a result of this License or
22  * out of the use or inability to use the software (including but not limited to damages
23  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
24  * all other commercial damages or losses), even if such Contributor has been advised
25  * of the possibility of such damages.
26  */
27 
28 #pragma once
29 
30 #include <sstream>
31 #include <iomanip>
32 
33 namespace Diligent
34 {
35 
36 template <typename SSType>
37 void FormatStrSS(SSType& ss)
38 {
39 }
40 
41 template <typename SSType, typename ArgType>
42 void FormatStrSS(SSType& ss, const ArgType& Arg)
43 {
44  ss << Arg;
45 }
46 
47 template <typename SSType, typename FirstArgType, typename... RestArgsType>
48 void FormatStrSS(SSType& ss, const FirstArgType& FirstArg, const RestArgsType&... RestArgs)
49 {
50  FormatStrSS(ss, FirstArg);
51  FormatStrSS(ss, RestArgs...); // recursive call using pack expansion syntax
52 }
53 
54 template <typename... RestArgsType>
55 std::string FormatString(const RestArgsType&... Args)
56 {
57  std::stringstream ss;
58  FormatStrSS(ss, Args...);
59  return ss.str();
60 }
61 
62 template <typename Type>
64 {
65  MemorySizeFormatter(Type _size, int _precision, Type _ref_size) :
66  size{_size},
67  precision{_precision},
68  ref_size{_ref_size}
69  {}
70  Type size = 0;
71  int precision = 0;
73 };
74 
75 template <typename Type>
76 MemorySizeFormatter<Type> FormatMemorySize(Type _size, int _precision = 0, Type _ref_size = 0)
77 {
78  return MemorySizeFormatter<Type>{_size, _precision, _ref_size};
79 }
80 
81 template <typename SSType, typename Type>
82 void FormatStrSS(SSType& ss, const MemorySizeFormatter<Type>& Arg)
83 {
84  auto ref_size = Arg.ref_size != 0 ? Arg.ref_size : Arg.size;
85  if (ref_size >= (1 << 30))
86  {
87  ss << std::fixed << std::setprecision(Arg.precision) << static_cast<double>(Arg.size) / double{1 << 30} << " GB";
88  }
89  else if (ref_size >= (1 << 20))
90  {
91  ss << std::fixed << std::setprecision(Arg.precision) << static_cast<double>(Arg.size) / double{1 << 20} << " MB";
92  }
93  else if (ref_size >= (1 << 10))
94  {
95  ss << std::fixed << std::setprecision(Arg.precision) << static_cast<double>(Arg.size) / double{1 << 10} << " KB";
96  }
97  else
98  {
99  ss << Arg.size << (((Arg.size & 0x01) == 0x01) ? " Byte" : " Bytes");
100  }
101 }
102 
103 } // namespace Diligent
Diligent::FormatStrSS
void FormatStrSS(SSType &ss)
Definition: FormatString.hpp:37
Diligent::MemorySizeFormatter::MemorySizeFormatter
MemorySizeFormatter(Type _size, int _precision, Type _ref_size)
Definition: FormatString.hpp:65
Diligent::FormatMemorySize
MemorySizeFormatter< Type > FormatMemorySize(Type _size, int _precision=0, Type _ref_size=0)
Definition: FormatString.hpp:76
Diligent::MemorySizeFormatter::ref_size
Type ref_size
Definition: FormatString.hpp:72
Diligent::MemorySizeFormatter::precision
int precision
Definition: FormatString.hpp:71
Type
const D3D12_PIPELINE_STATE_SUBOBJECT_TYPE Type
Definition: PipelineStateD3D12Impl.cpp:69
Diligent::MemorySizeFormatter::size
Type size
Definition: FormatString.hpp:70
Diligent::FormatString
std::string FormatString(const RestArgsType &... Args)
Definition: FormatString.hpp:55
Diligent::MemorySizeFormatter
Definition: FormatString.hpp:63
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37