Uniot Core
0.8.1
Loading...
Searching...
No Matches
Common.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2024 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include <memory>
22#include <utility>
23
29
42template <int a, int b, int c, int d>
43struct FourCC {
44 static const uint32_t Value = (((((d << 8) | c) << 8) | b) << 8) | a;
45};
46
56template <typename... Args>
57inline void UNUSED(Args &&...args) {
58 (void)(sizeof...(args));
59}
60
72inline uint32_t CRC32(const void *data, size_t length, uint32_t crc = 0) {
73 const uint8_t *ldata = (const uint8_t *)data;
74
75 crc = ~crc;
76 while (length--) {
77 crc ^= *ldata++;
78 for (uint8_t k = 0; k < 8; k++)
79 crc = crc & 1 ? (crc >> 1) ^ 0x82f63b78 : crc >> 1; // CRC-32C (iSCSI) polynomial in reversed bit order.
80 }
81 return ~crc;
82}
83
88#define COUNT_OF(arr) (sizeof(arr) / sizeof(arr[0]))
89
97#define ARRAY_ELEMENT_SAFE(arr, index) ((arr)[(((index) < COUNT_OF(arr)) ? (index) : (COUNT_OF(arr) - 1))])
98
107#define FOURCC(name) FourCC<ARRAY_ELEMENT_SAFE(#name, 0), ARRAY_ELEMENT_SAFE(#name, 1), ARRAY_ELEMENT_SAFE(#name, 2), ARRAY_ELEMENT_SAFE(#name, 3)>::Value
108
119#define ALIAS_IMPLICIT_FUNCTION(high, low) \
120 template <typename... Args> \
121 inline auto high(Args &&...args) -> decltype(low(std::forward<Args>(args)...)) { \
122 return low(std::forward<Args>(args)...); \
123 }
124
134#define ALIAS_EXPLICIT_FUNCTION(high, low) \
135 template <typename T, typename... Args> \
136 inline auto high(Args &&...args) -> decltype(low<T>(std::forward<Args>(args)...)) { \
137 return low<T>(std::forward<Args>(args)...); \
138 }
139
140namespace uniot {
145
151template <typename T>
152using UniquePointer = std::unique_ptr<T>;
153
159template <typename T>
160using SharedPointer = std::shared_ptr<T>;
161
168template <typename T_First, typename T_Second>
169using Pair = std::pair<T_First, T_Second>;
170
175
176
180
185
187} // namespace uniot
auto MakePair(Args &&...args) -> decltype(std::make_pair(std::forward< Args >(args)...))
Creates a pair instance, alias for std::make_pair.
Definition Common.h:184
std::unique_ptr< T > UniquePointer
Type alias for std::unique_ptr with cleaner syntax.
Definition Common.h:152
#define ALIAS_IMPLICIT_FUNCTION(high, low)
Creates an alias for a function that forwards all arguments implicitly.
Definition Common.h:119
std::shared_ptr< T > SharedPointer
Type alias for std::shared_ptr with cleaner syntax.
Definition Common.h:160
void UNUSED(Args &&...args)
Utility function to mark function parameters as intentionally unused.
Definition Common.h:57
std::pair< T_First, T_Second > Pair
Type alias for std::pair with cleaner syntax.
Definition Common.h:169
auto MakeUnique(Args &&...args) -> decltype(std::make_unique< T >(std::forward< Args >(args)...))
Creates a unique pointer instance, alias for std::make_unique.
Definition Common.h:179
#define ALIAS_EXPLICIT_FUNCTION(high, low)
Creates an alias for a template function that forwards all arguments explicitly.
Definition Common.h:134
uint32_t CRC32(const void *data, size_t length, uint32_t crc=0)
Calculates CRC32-C (Castagnoli) checksum for data integrity verification.
Definition Common.h:72
auto MakeShared(Args &&...args) -> decltype(std::make_shared< T >(std::forward< Args >(args)...))
Creates a shared pointer instance, alias for std::make_shared.
Definition Common.h:174
Contains all classes and functions related to the Uniot Core.
Creates a four-character code (FourCC) value from template parameters.
Definition Common.h:43
static const uint32_t Value
Definition Common.h:44