libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
Loading...
Searching...
No Matches
Part.h
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <memory>
5#include <stdint.h>
6#ifdef COMPONENT_THREAD_SAFE
7#include <mutex>
8#endif
9
10namespace Ship {
11
12class Context;
13
15#define INVALID_PART_ID UINT64_MAX
16
27class Part {
28 public:
32 explicit Part(std::shared_ptr<Context> context);
33 virtual ~Part() = default;
34
36 uint64_t GetId() const;
37
43 bool operator==(const Part& other) const;
44
53 std::shared_ptr<Context> GetContext() const;
54
63 void SetContext(std::shared_ptr<Context> ctx);
64
65 protected:
74 virtual void OnAdded(bool forced);
75
84 virtual void OnRemoved(bool forced);
85
86 template <typename, typename> friend class PartList;
87
88 private:
89 static std::atomic<uint64_t> sNextPartId;
90 uint64_t mId;
91 std::weak_ptr<Context> mContext;
92#ifdef COMPONENT_THREAD_SAFE
93 // Guards mContext, which can be written by SetContext (during context
94 // propagation on Add) while other threads read it via GetContext().
95 mutable std::mutex mContextMutex;
96#endif
97};
98
99} // namespace Ship
A thread-safe ordered list of Parts.
Definition PartList.h:114
Base class for all identifiable objects in the component system.
Definition Part.h:27
std::shared_ptr< Context > GetContext() const
Returns the Context this Part belongs to, or nullptr if unset.
bool operator==(const Part &other) const
Compares two Parts for equality by their unique IDs.
virtual void OnAdded(bool forced)
Called after this Part has been added to a PartList.
Part()
Constructs a Part and assigns it a unique ID.
uint64_t GetId() const
Returns the unique identifier for this Part.
virtual ~Part()=default
Part(std::shared_ptr< Context > context)
Constructs a Part with an explicit Context reference and unique ID.
virtual void OnRemoved(bool forced)
Called after this Part has been removed from a PartList.
void SetContext(std::shared_ptr< Context > ctx)
Sets the Context this Part belongs to.
Core namespace for the libultraship engine framework.
Definition gfx_direct3d_common.h:14