SuperDex Physics C++ API
Loading...
Searching...
No Matches
mochi_handle_inl.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <cstddef>
20#include <functional>
21#include <memory>
22
24
25namespace superdex {
26
27inline Handle::Handle(ValueType raw) : value(raw) {}
28
29inline bool Handle::IsValid() const {
30 return value != kInvalidHandle;
31}
32inline bool Handle::operator==(Handle const& rhs) const {
33 return value == rhs.value;
35inline bool Handle::operator!=(Handle const& rhs) const {
36 return value != rhs.value;
37}
38inline bool Handle::operator<(Handle const& rhs) const {
39 return value < rhs.value;
40}
41inline uint64_t Handle::GetHash() const {
42 return std::hash<ValueType>{}(value);
43}
44
45// Definition kept out of the DSL-mirrored mochi_handle.h (ShapeHandle is marked
46// [no_cpp_definition] in mochi_physics_handle.mochi_gen): it carries a private,
47// C++-only auto-cleanup token that the DSL cannot express.
48struct ShapeHandle : public Handle {
49 using Handle::Handle;
50
51 private:
52 friend class ContextImpl;
53 struct AutoCleanup {
54 virtual ~AutoCleanup() = default;
55 };
56 std::shared_ptr<AutoCleanup> _cleanup = {};
57};
58
59namespace prefab {
60struct ScenePrefab;
61} // namespace prefab
62
63// Definition kept out of the DSL-mirrored mochi_prefab.h (PrefabHandle is declared in
64// mochi_physics_handle.mochi_gen and marked [no_cpp_definition]): it wraps a std::shared_ptr, which
65// the DSL cannot express.
66//
67// Runtime handle to a loaded nested prefab (the resolved target of a @ref prefab::PrefabReference).
68// It is a strongly-typed std::shared_ptr<@ref prefab::ScenePrefab> and behaves exactly like one
69// (copy shares ownership, move transfers, deref/operator-> access the prefab). The pointed-to
70// prefab is runtime state: it is populated during loading (e.g. @ref prefab::LoadNestedPrefabs) and
71// is never serialized.
72struct PrefabHandle : public std::shared_ptr<prefab::ScenePrefab> {
73 // Bring in std::shared_ptr's assignment operators (otherwise hidden by the implicit copy/move
74 // assignment) so a PrefabHandle can be assigned from a shared_ptr or nullptr. Constructors are
75 // intentionally NOT inherited: doing so would make `nullptr` convertible to PrefabHandle and make
76 // `handle = nullptr` ambiguous. Default/copy/move construction (all we need) stay implicit.
77 using std::shared_ptr<prefab::ScenePrefab>::operator=;
78};
79
80} // namespace superdex
81
82// Support for superdex::Handle in std::unordered_set and std::unordered_map
83/// @cond
84namespace std {
85template <>
86struct hash<superdex::Handle> {
87 std::size_t operator()(superdex::Handle h) const {
88 return h.GetHash();
89 }
90};
91template <>
92struct hash<superdex::SceneHandle> : public hash<superdex::Handle> {};
93template <>
94struct hash<superdex::ActorHandle> : public hash<superdex::Handle> {};
95template <>
96struct hash<superdex::ShapeHandle> : public hash<superdex::Handle> {};
97template <>
98struct hash<superdex::CallbackHandle> : public hash<superdex::Handle> {};
99template <>
100struct hash<superdex::ConstraintHandle> : public hash<superdex::Handle> {};
101template <>
102struct hash<superdex::StateHandle> : public hash<superdex::Handle> {};
103template <>
104struct hash<superdex::QueryHandle> : public hash<superdex::Handle> {};
105} // namespace std
106/// @endcond
constexpr uint64_t kInvalidHandle
Default value for an invalid handle.
Generic 64-bit handle providing base functionality for strongly-typed handles.
uint64_t GetHash() const
Compute a hash of the handle for use in unordered containers.
bool operator<(Handle const &rhs) const
uint64_t ValueType
Raw 64-bit identifier type.
ValueType value
Raw 64-bit identifier.
Handle()=default
bool operator!=(Handle const &rhs) const
bool operator==(Handle const &rhs) const
bool IsValid() const
Check whether the handle is valid (i.e., its value is non-zero).
Runtime handle to a loaded nested prefab (the resolved target of a prefab::PrefabReference).
Handle to a shape (mesh, sphere, plane, etc.) within a Context.
Top-level prefab describing a complete or partial physics scene (possibly just one actor).