ICF 3.1.1.10
Technical documentation of ICF Libraries
istd.h
Go to the documentation of this file.
1/********************************************************************************
2** This file is part of the ICF Framework. Copyright (C) Witold Gantzke & Kirill Lepskiy
3** ICF Framework may be used under the terms of the LGPL License v. 2.1 by the Free Software Foundation.
4********************************************************************************/
5
6#pragma once
7
8
9// STL includes
10#include <limits>
11#include <memory>
12
13// Qt includes
14#include <QtCore/QtGlobal>
15#include <QtCore/QByteArray>
16#include <QtCore/QString>
17#include <QtCore/QStringList>
18
19
24namespace istd
25{
26} // namespace istd
27
28
29template <class T, class B>
30std::unique_ptr<T> CastOrRemove(B* origPtr)
31{
32 T* castedPtr = dynamic_cast<T*>(origPtr);
33 if (castedPtr != nullptr) {
34 return std::unique_ptr<T>(castedPtr);
35 }
36
37 delete origPtr;
38
39 return nullptr;
40}
41
42
43template <class T, class SourcePtrType>
44std::unique_ptr<T> CastOrRemove(std::unique_ptr< SourcePtrType> origPtr)
45{
46 T* castedPtr = dynamic_cast<T*>(origPtr.get());
47 if (castedPtr != nullptr) {
48 origPtr.release();
49 return std::unique_ptr<T>(castedPtr);
50 }
51
52 return nullptr;
53}
54
55
56static constexpr double I_BIG_EPSILON = 1.0e-8;
57
61#define I_EPSILON std::numeric_limits<double>::epsilon()
62
63
67#define I_HUGE_NUMBER 10e20
68
69
73#define I_INFINITY std::numeric_limits<double>::infinity()
74
75
76#ifndef QT_NO_DEBUG
77
78
79#define I_IF_DEBUG(instructions) instructions
80#define I_IF_NOT_DEBUG(instructions)
81
82
83#else // !QT_NO_DEBUG
84
85
86#define I_IF_DEBUG(instructions)
87#define I_IF_NOT_DEBUG(instructions) instructions
88
89
90#endif // !QT_NO_DEBUG
91
92
93
97#define I_ENUM_GET_VALUES(Enum, ...)\
98 static QList<int> Enum##GetValues(){\
99 QList<int> values;\
100 int vars[] = {0, __VA_ARGS__};\
101 int count = (sizeof(vars) / sizeof(int));\
102 for(int i = 0; i < count; ++i){\
103 if (i > 0){\
104 values << vars[i];\
105 }\
106 }\
107 return values;\
108 }
109
113#define I_ENUM_GET_STRINGS(Enum, ...)\
114 static QStringList Enum##GetStrings(){\
115 QString enumValuesString = #__VA_ARGS__;\
116 QStringList values = enumValuesString.split(",");\
117 for (int i = 0; i < values.count(); ++i){\
118 QString rawValue = values[i].simplified();\
119 QStringList splitNames = rawValue.split("_");\
120 if (splitNames.isEmpty()){\
121 values[i] = rawValue;\
122 }\
123 else{\
124 QString formattedValue = rawValue;\
125 for (int partIndex = 1; partIndex < splitNames.count(); ++partIndex){\
126 QString partValue = splitNames[partIndex].toLower();\
127 if (partIndex == 1){\
128 formattedValue = partValue;\
129 }\
130 else{\
131 formattedValue += partValue.at(0).toUpper() + partValue.mid(1);\
132 }\
133 }\
134 values[i] = formattedValue;\
135 }\
136 }\
137 return values;\
138 }
139
143#define I_DECLARE_ENUM(Enum, ...)\
144 I_ENUM_GET_VALUES(Enum, __VA_ARGS__)\
145 I_ENUM_GET_STRINGS(Enum, __VA_ARGS__)\
146 static QByteArray ToString(Enum enumValue){\
147 static QByteArray emptyString;\
148 QStringList values = Enum##GetStrings();\
149 QList<int> enumValues = Enum##GetValues();\
150 Q_ASSERT(enumValues.count() == values.count());\
151 for (int i = 0; i < enumValues.count(); ++i){\
152 if (enumValues[i] == enumValue){\
153 return values[i].toUtf8();\
154 }\
155 }\
156 return emptyString;\
157 }\
158 static bool FromString(const QByteArray& enumString, Enum& enumValue){\
159 QStringList values = Enum##GetStrings();\
160 QList<int> enumValues = Enum##GetValues();\
161 Q_ASSERT(enumValues.count() == values.count());\
162 for (int i = 0; i < enumValues.count(); ++i){\
163 if (values[i].toUtf8() == enumString){\
164 enumValue = Enum(enumValues[i]);\
165 return true;\
166 }\
167 }\
168 return false;\
169 }
170
171#define I_DECLARE_FLAGS(Enum, ...)\
172 I_ENUM_GET_VALUES(Enum, __VA_ARGS__)\
173 I_ENUM_GET_STRINGS(Enum, __VA_ARGS__)\
174 static QByteArray Enum##ToString(int flags){\
175 QByteArray retVal;\
176 QStringList values = Enum##GetStrings();\
177 QList<int> enumValues = Enum##GetValues();\
178 Q_ASSERT(enumValues.count() == values.count());\
179 for (int i = 0; i < enumValues.count(); ++i){\
180 if (enumValues[i] & flags){\
181 if (!retVal.isEmpty()){\
182 retVal += "|";\
183 }\
184 retVal += values[i].toUtf8();\
185 }\
186 }\
187 return retVal;\
188 }\
189 static bool Enum##FromString(const QByteArray& enumString, int& flags){\
190 QStringList values = Enum##GetStrings();\
191 QList<int> enumValues = Enum##GetValues();\
192 flags = 0;\
193 Q_ASSERT(enumValues.count() == values.count());\
194 QList<QByteArray> separatedValues = enumString.split('|');\
195 for (int valueIndex = 0; valueIndex < separatedValues.count(); ++valueIndex){\
196 QByteArray value = separatedValues[valueIndex];\
197 for (int i = 0; i < enumValues.count(); ++i){\
198 if (values[i].toUtf8() == value){\
199 flags |= enumValues[i];\
200 }\
201 }\
202 }\
203 return true;\
204 }
205
206
207#ifdef ICF_USE_BINARY_RESOURCES
208#include<QtCore/QResource>
209#define I_INIT_RESOURCE(resourceId)\
210 QResource::registerResource("resources/" #resourceId ".rcc")
211#else
212#define I_INIT_RESOURCE(resourceId)\
213 Q_INIT_RESOURCE(resourceId)
214#endif
std::unique_ptr< T > CastOrRemove(B *origPtr)
Definition istd.h:30
Standard library.
Definition IComponent.h:17

© Witold Gantzke and Kirill Lepskiy