File indexing completed on 2026-12-04 10:45:27
0001
0002
0003
0004
0005
0006
0007 #pragma once
0008
0009 #include <cstddef>
0010 #include <functional>
0011 #include <type_traits>
0012
0013 #include "corecel/data/Ldg.hh"
0014
0015 #include "Assert.hh"
0016 #include "Macros.hh"
0017 #include "Types.hh"
0018
0019 #if !CELER_DEVICE_COMPILE
0020 # include <ostream>
0021 #endif
0022
0023 namespace celeritas
0024 {
0025 namespace detail
0026 {
0027
0028
0029 template<class I>
0030 inline constexpr I nullid_value{static_cast<I>(-1)};
0031
0032 #if !CELER_DEVICE_COMPILE
0033 template<class I>
0034 void stream_opaqueid_impl(std::ostream&, I, I);
0035 #endif
0036
0037
0038 }
0039
0040
0041 struct nullid_t
0042 {
0043 constexpr explicit nullid_t(int) {}
0044 };
0045
0046
0047 inline constexpr nullid_t nullid{0};
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123 template<class TagT, class IndexT = ::celeritas::size_type>
0124 class OpaqueId
0125 {
0126 static_assert(std::is_unsigned_v<IndexT> && !std::is_same_v<IndexT, bool>,
0127 "IndexT must be unsigned.");
0128
0129 static constexpr bool ndebug = !CELERITAS_DEBUG;
0130
0131 public:
0132
0133
0134 using tag_type = TagT;
0135 using value_type = IndexT;
0136 using index_type = IndexT;
0137 using difference_type = std::make_signed_t<IndexT>;
0138 using size_type = value_type;
0139
0140
0141 public:
0142
0143 CELER_CEF OpaqueId(nullid_t) : value_(null_) {}
0144
0145
0146 CELER_CEF OpaqueId() : OpaqueId(nullid) {}
0147
0148
0149 explicit CELER_CEF OpaqueId(value_type index) : value_(index) {}
0150
0151
0152 explicit CELER_CEF operator bool() const noexcept
0153 {
0154 return value_ != null_;
0155 }
0156
0157
0158 CELER_CEF value_type operator*() const noexcept { return value_; }
0159
0160
0161 CELER_CEF value_type value() const noexcept(ndebug)
0162 {
0163 CELER_EXPECT(*this);
0164 return value_;
0165 }
0166
0167
0168 CELER_CEF value_type const* data() const noexcept { return &value_; }
0169
0170
0171
0172
0173
0174 CELER_CEF OpaqueId& operator++() noexcept(ndebug)
0175 {
0176 CELER_EXPECT(value_ < (null_ - 1));
0177 value_ += 1;
0178 return *this;
0179 }
0180
0181 CELER_CEF OpaqueId operator++(int) noexcept(ndebug)
0182 {
0183 OpaqueId old{*this};
0184 ++*this;
0185 return old;
0186 }
0187
0188 CELER_CEF OpaqueId& operator--() noexcept(ndebug)
0189 {
0190 CELER_EXPECT(*this && value_ > 0);
0191 value_ -= 1;
0192 return *this;
0193 }
0194
0195 CELER_CEF OpaqueId operator--(int) noexcept(ndebug)
0196 {
0197 OpaqueId old{*this};
0198 --*this;
0199 return old;
0200 }
0201
0202
0203
0204
0205
0206
0207 CELER_FUNCTION friend difference_type operator-(OpaqueId self,
0208 OpaqueId other)
0209 {
0210 CELER_EXPECT(self);
0211 CELER_EXPECT(other);
0212 return static_cast<difference_type>(*self - *other);
0213 }
0214
0215
0216 template<class J>
0217 CELER_FUNCTION friend auto operator+(OpaqueId id, J offset)
0218 -> std::enable_if_t<std::is_integral_v<J>, OpaqueId>
0219 {
0220 CELER_EXPECT(id);
0221 CELER_EXPECT(OpaqueId::is_safe_offset(*id, offset));
0222
0223
0224 return OpaqueId{static_cast<IndexT>(*id + offset)};
0225 }
0226
0227
0228 template<class J>
0229 CELER_FUNCTION friend auto operator+(J offset, OpaqueId id)
0230 -> std::enable_if_t<std::is_integral_v<J>, OpaqueId>
0231 {
0232 return id + offset;
0233 }
0234
0235
0236 template<class J>
0237 CELER_FUNCTION friend auto operator-(OpaqueId id, J offset)
0238 -> std::enable_if_t<std::is_integral_v<J>, OpaqueId>
0239 {
0240 CELER_EXPECT(id);
0241 CELER_EXPECT(offset <= 0 || static_cast<IndexT>(offset) <= *id);
0242
0243 return OpaqueId{static_cast<IndexT>(*id - static_cast<IndexT>(offset))};
0244 }
0245
0246
0247
0248
0249
0250
0251
0252 CELER_CEF value_type get() const noexcept(ndebug) { return this->value(); }
0253
0254
0255 CELER_CEF value_type unchecked_get() const noexcept { return value_; }
0256
0257
0258
0259
0260
0261 #define CELER_DEFINE_OPAQUEID_CMP(TOKEN) \
0262 CELER_CEF friend bool operator TOKEN(OpaqueId lhs, OpaqueId rhs) noexcept \
0263 { \
0264 return *lhs TOKEN * rhs; \
0265 }
0266
0267
0268
0269 CELER_DEFINE_OPAQUEID_CMP(==)
0270 CELER_DEFINE_OPAQUEID_CMP(!=)
0271 CELER_DEFINE_OPAQUEID_CMP(<)
0272 CELER_DEFINE_OPAQUEID_CMP(>)
0273 CELER_DEFINE_OPAQUEID_CMP(<=)
0274 CELER_DEFINE_OPAQUEID_CMP(>=)
0275
0276
0277
0278
0279 CELER_CEF friend bool operator==(OpaqueId id, nullid_t) noexcept
0280 {
0281 return !id;
0282 }
0283 CELER_CEF friend bool operator==(nullid_t, OpaqueId id) noexcept
0284 {
0285 return !id;
0286 }
0287 CELER_CEF friend bool operator!=(OpaqueId id, nullid_t) noexcept
0288 {
0289 return static_cast<bool>(id);
0290 }
0291 CELER_CEF friend bool operator!=(nullid_t, OpaqueId id) noexcept
0292 {
0293 return static_cast<bool>(id);
0294 }
0295
0296
0297
0298 CELER_CONSTEXPR_FUNCTION friend OpaqueId ldg(OpaqueId const* id)
0299 {
0300 using ::celeritas::ldg;
0301 return OpaqueId{ldg(&id->value_)};
0302 }
0303
0304 #undef CELER_DEFINE_OPAQUEID_CMP
0305 #define CELER_DEFINE_OPAQUEID_CMP(TOKEN) \
0306 template<class J> \
0307 CELER_CEF friend auto operator TOKEN(OpaqueId lhs, J rhs) noexcept \
0308 -> std::enable_if_t<std::is_unsigned_v<J>, bool> \
0309 { \
0310 return lhs && (static_cast<J>(*lhs) TOKEN rhs); \
0311 }
0312
0313
0314
0315
0316 CELER_DEFINE_OPAQUEID_CMP(<)
0317 CELER_DEFINE_OPAQUEID_CMP(<=)
0318
0319
0320 #undef CELER_DEFINE_OPAQUEID_CMP
0321
0322 #if !CELER_DEVICE_COMPILE
0323
0324 CELER_FORCEINLINE friend std::ostream& operator<<(std::ostream& os,
0325 OpaqueId v)
0326 {
0327 detail::stream_opaqueid_impl(os, v.value_, v.null_);
0328 return os;
0329 }
0330 #endif
0331
0332 private:
0333
0334
0335 value_type value_;
0336
0337
0338 static constexpr value_type null_ = detail::nullid_value<value_type>;
0339
0340
0341
0342 template<class J>
0343 static CELER_CEF bool is_safe_offset(IndexT value, J offset)
0344 {
0345 if constexpr (std::is_unsigned_v<J>)
0346 {
0347 return true;
0348 }
0349 else
0350 {
0351 if (offset >= 0)
0352 {
0353
0354 return true;
0355 }
0356 return static_cast<IndexT>(J{0} - offset) <= value;
0357 }
0358 }
0359 };
0360
0361
0362 template<class I, class V>
0363 struct MakeSize<OpaqueId<I, V>>
0364 {
0365 using type = V;
0366 };
0367
0368
0369 template<class I, class V>
0370 struct IsAutoLdg<OpaqueId<I, V>> : std::true_type
0371 {
0372 };
0373
0374
0375
0376
0377
0378
0379 namespace detail
0380 {
0381
0382
0383 template<class T, class J>
0384 inline CELER_FUNCTION T id_cast_impl(J value) noexcept(!CELERITAS_DEBUG)
0385 {
0386 constexpr auto null_val = detail::nullid_value<T>;
0387
0388 if constexpr (std::is_signed_v<J>)
0389 {
0390 CELER_EXPECT(value >= 0);
0391 }
0392
0393 if constexpr (!std::is_same_v<T, J>)
0394 {
0395
0396 using C = std::common_type_t<T, std::make_unsigned_t<J>>;
0397 if constexpr (std::is_signed_v<C>)
0398 {
0399 CELER_EXPECT(static_cast<C>(value) >= 0);
0400 }
0401 CELER_EXPECT(static_cast<C>(value) < static_cast<C>(null_val));
0402 }
0403 else
0404 {
0405
0406 CELER_EXPECT(static_cast<T>(value) != null_val);
0407 }
0408
0409 return static_cast<T>(value);
0410 }
0411
0412
0413
0414 template<class T>
0415 struct IsOpaqueId : std::false_type
0416 {
0417 };
0418
0419 template<class I, class V>
0420 struct IsOpaqueId<OpaqueId<I, V>> : std::true_type
0421 {
0422 };
0423
0424 #if !CELER_DEVICE_COMPILE
0425
0426 template<class V>
0427 inline void stream_opaqueid_impl(std::ostream& os, V v, V nullint)
0428 {
0429 os << '{';
0430 if (v != nullint)
0431 {
0432 os << v;
0433 }
0434 os << '}';
0435 }
0436
0437
0438 template<>
0439 CELER_FORCEINLINE void stream_opaqueid_impl(
0440 std::ostream& os, unsigned char v, unsigned char nullid)
0441 {
0442 return stream_opaqueid_impl(
0443 os, static_cast<unsigned int>(v), static_cast<unsigned int>(nullid));
0444 }
0445 #endif
0446
0447
0448 }
0449
0450
0451
0452
0453
0454
0455 template<class T>
0456 inline constexpr bool is_opaque_id_v
0457 = detail::IsOpaqueId<std::remove_cv_t<T>>::value;
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469 template<class IdT, class J>
0470 inline CELER_FUNCTION auto id_cast(J value) noexcept(!CELERITAS_DEBUG)
0471 -> std::enable_if_t<is_opaque_id_v<IdT> && std::is_integral_v<J>, IdT>
0472 {
0473 return IdT{detail::id_cast_impl<typename IdT::value_type, J>(value)};
0474 }
0475
0476
0477 }
0478
0479
0480 namespace std
0481 {
0482
0483 template<class I, class T>
0484 struct hash<celeritas::OpaqueId<I, T>>
0485 {
0486 std::size_t operator()(celeritas::OpaqueId<I, T> const& id) const noexcept
0487 {
0488 return std::hash<T>()(*id);
0489 }
0490 };
0491 }
0492