WPILibC++ 2025.3.1
Loading...
Searching...
No Matches
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
7#include <type_traits>
8
10
11namespace wpi {
12
13/**
14 * A helper class for use with objects that add themselves to SendableRegistry.
15 * It takes care of properly calling Move() and Remove() on move and
16 * destruction. No action is taken if the object is copied.
17 * Use public inheritance with CRTP when using this class.
18 * @tparam CRTP derived class
19 */
20template <typename Derived>
22 public:
23 constexpr SendableHelper(const SendableHelper& rhs) = default;
24 constexpr SendableHelper& operator=(const SendableHelper& rhs) = default;
25
26#if !defined(_MSC_VER) && (defined(__llvm__) || __GNUC__ > 7)
27 // See https://bugzilla.mozilla.org/show_bug.cgi?id=1442819
28 __attribute__((no_sanitize("vptr")))
29#endif
30 constexpr SendableHelper(SendableHelper&& rhs) {
31 if (!std::is_constant_evaluated()) {
32 // it is safe to call Move() multiple times with the same rhs
33 SendableRegistry::Move(static_cast<Derived*>(this),
34 static_cast<Derived*>(&rhs));
35 }
36 }
37
38#if !defined(_MSC_VER) && (defined(__llvm__) || __GNUC__ > 7)
39 // See https://bugzilla.mozilla.org/show_bug.cgi?id=1442819
40 __attribute__((no_sanitize("vptr")))
41#endif
42 constexpr SendableHelper&
44 if (!std::is_constant_evaluated()) {
45 // it is safe to call Move() multiple times with the same rhs
46 SendableRegistry::Move(static_cast<Derived*>(this),
47 static_cast<Derived*>(&rhs));
48 }
49 return *this;
50 }
51
52 protected:
53 constexpr SendableHelper() = default;
54
55 constexpr ~SendableHelper() {
56 if (!std::is_constant_evaluated()) {
57 // it is safe to call Remove() multiple times with the same object
58 SendableRegistry::Remove(static_cast<Derived*>(this));
59 }
60 }
61};
62
63} // namespace wpi
A helper class for use with objects that add themselves to SendableRegistry.
Definition SendableHelper.h:21
constexpr SendableHelper & operator=(SendableHelper &&rhs)
Definition SendableHelper.h:43
constexpr SendableHelper & operator=(const SendableHelper &rhs)=default
constexpr SendableHelper()=default
constexpr SendableHelper(SendableHelper &&rhs)
Definition SendableHelper.h:30
constexpr ~SendableHelper()
Definition SendableHelper.h:55
constexpr SendableHelper(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.
Foonathan namespace.
Definition ntcore_cpp.h:26