WPILibC++ 2024.1.1-beta-4
SendableHelper.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
8
9namespace wpi {
10
11/**
12 * A helper class for use with objects that add themselves to SendableRegistry.
13 * It takes care of properly calling Move() and Remove() on move and
14 * destruction. No action is taken if the object is copied.
15 * Use public inheritance with CRTP when using this class.
16 * @tparam CRTP derived class
17 */
18template <typename Derived>
20 public:
21 SendableHelper(const SendableHelper& rhs) = default;
22 SendableHelper& operator=(const SendableHelper& rhs) = default;
23
24#if !defined(_MSC_VER) && (defined(__llvm__) || __GNUC__ > 7)
25 // See https://bugzilla.mozilla.org/show_bug.cgi?id=1442819
26 __attribute__((no_sanitize("vptr")))
27#endif
29 // it is safe to call Move() multiple times with the same rhs
30 SendableRegistry::Move(static_cast<Derived*>(this),
31 static_cast<Derived*>(&rhs));
32 }
33
34#if !defined(_MSC_VER) && (defined(__llvm__) || __GNUC__ > 7)
35 // See https://bugzilla.mozilla.org/show_bug.cgi?id=1442819
36 __attribute__((no_sanitize("vptr")))
37#endif
40 // it is safe to call Move() multiple times with the same rhs
41 SendableRegistry::Move(static_cast<Derived*>(this),
42 static_cast<Derived*>(&rhs));
43 return *this;
44 }
45
46 protected:
47 SendableHelper() = default;
48
50 // it is safe to call Remove() multiple times with the same object
51 SendableRegistry::Remove(static_cast<Derived*>(this));
52 }
53};
54
55} // namespace wpi
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:19
~SendableHelper()
Definition: SendableHelper.h:49
SendableHelper & operator=(SendableHelper &&rhs)
Definition: SendableHelper.h:39
SendableHelper(const SendableHelper &rhs)=default
SendableHelper(SendableHelper &&rhs)
Definition: SendableHelper.h:28
SendableHelper()=default
SendableHelper & operator=(const SendableHelper &rhs)=default
static void Move(Sendable *to, Sendable *from)
Moves an object in the registry (for use in move constructors/assignments).
static bool Remove(Sendable *sendable)
Removes an object from the registry.
Definition: ntcore_cpp.h:26