JPL Spatial
Sound spatialization and propagation library
Loading...
Searching...
No Matches
IDType.h
Go to the documentation of this file.
1//
2// ██╗██████╗ ██╗ ██╗██████╗ ███████╗
3// ██║██╔══██╗ ██║ ██║██╔══██╗██╔════╝ ** JPLSpatial **
4// ██║██████╔╝ ██║ ██║██████╔╝███████╗
5// ██ ██║██╔═══╝ ██║ ██║██╔══██╗╚════██║ https://github.com/Jaytheway/JPLSpatial
6// ╚█████╔╝██║ ███████╗██║██████╔╝███████║
7// ╚════╝ ╚═╝ ╚══════╝╚═╝╚═════╝ ╚══════╝
8//
9// Copyright 2024 Jaroslav Pevno, JPLSpatial is offered under the terms of the ISC license:
10//
11// Permission to use, copy, modify, and/or distribute this software for any purpose with or
12// without fee is hereby granted, provided that the above copyright notice and this permission
13// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
14// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
16// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
17// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20#pragma once
21
22#include <concepts>
23#include <atomic>
24
25namespace JPL
26{
27 //==========================================================================
36 template<typename Tag, std::integral InternalType = uint32_t, bool Atomic = false>
37 struct [[nodiscard]] IDType
38 {
39 using TagType = Tag;
40 using CounterType = std::conditional_t<Atomic, std::atomic<InternalType>, InternalType>;
41
42 constexpr IDType() = default;
43
44 [[nodiscard]] static constexpr IDType New() noexcept { return IDType(++sLastValue); }
45
46 [[nodiscard]] constexpr bool IsValid() const noexcept { return mValue != std::numeric_limits<InternalType>::max(); }
47 [[nodiscard]] constexpr operator bool() const noexcept { return IsValid(); }
48
49 [[nodiscard]] constexpr bool operator==(const IDType other) const noexcept { return mValue == other.mValue; }
50 [[nodiscard]] constexpr bool operator!=(const IDType other) const noexcept { return mValue != other.mValue; }
51
52 private:
53 constexpr explicit IDType(InternalType value) : mValue(value) {}
54
55 friend struct std::hash<JPL::IDType<Tag, InternalType>>;
56
57 InternalType mValue = std::numeric_limits<InternalType>::max();
58 inline static CounterType sLastValue{ InternalType(0) };
59 };
60} // namespace JPL
61
62
63//==============================================================================
64namespace std
65{
66 template<typename Tag, std::integral InternalType>
67 struct [[nodiscard]] hash<JPL::IDType<Tag, InternalType>>
68 {
69 constexpr std::size_t operator()(const JPL::IDType<Tag, InternalType>& id) const
70 {
71 return id.mValue;
72 }
73 };
74} // namespace std
Definition AcousticMaterial.h:36
JPL_INLINE simd max(const simd &a, const simd &b) noexcept
Element-wise max.
Definition SIMD.h:1799
JPL_INLINE simd min(const simd &a, const simd &b) noexcept
Element-wise min.
Definition SIMD.h:1813
Definition ChannelMap.h:268
Definition IDType.h:38
constexpr bool operator!=(const IDType other) const noexcept
Definition IDType.h:50
constexpr bool operator==(const IDType other) const noexcept
Definition IDType.h:49
Tag TagType
Definition IDType.h:39
static constexpr IDType New() noexcept
Definition IDType.h:44
constexpr bool IsValid() const noexcept
Definition IDType.h:46
std::conditional_t< Atomic, std::atomic< InternalType >, InternalType > CounterType
Definition IDType.h:40
constexpr IDType()=default