mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-28 02:11:43 +00:00
[wpiutil] Split function_ref to a separate header
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
|
||||
#include "glass/hardware/LEDDisplay.h"
|
||||
|
||||
#include <wpi/SmallVector.h>
|
||||
|
||||
#include "glass/Context.h"
|
||||
#include "glass/support/ExtraGuiWidgets.h"
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <wpi/STLExtras.h>
|
||||
#include <wpi/function_ref.h>
|
||||
|
||||
#include "glass/Model.h"
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include <string_view>
|
||||
|
||||
#include <wpi/STLExtras.h>
|
||||
#include <wpi/function_ref.h>
|
||||
|
||||
#include "glass/Model.h"
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <wpi/STLExtras.h>
|
||||
#include <wpi/function_ref.h>
|
||||
|
||||
#include "glass/Model.h"
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include <string_view>
|
||||
|
||||
#include <wpi/STLExtras.h>
|
||||
#include <wpi/function_ref.h>
|
||||
|
||||
#include "glass/Model.h"
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include <string_view>
|
||||
|
||||
#include <wpi/STLExtras.h>
|
||||
#include <wpi/function_ref.h>
|
||||
|
||||
#include "glass/Model.h"
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <wpi/STLExtras.h>
|
||||
#include <wpi/function_ref.h>
|
||||
#include <wpi/span.h>
|
||||
|
||||
#include "glass/Model.h"
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include <string_view>
|
||||
|
||||
#include <wpi/STLExtras.h>
|
||||
#include <wpi/function_ref.h>
|
||||
|
||||
#include "glass/Model.h"
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include <string_view>
|
||||
|
||||
#include <wpi/STLExtras.h>
|
||||
#include <wpi/function_ref.h>
|
||||
|
||||
#include "glass/Model.h"
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include <string_view>
|
||||
|
||||
#include <wpi/STLExtras.h>
|
||||
#include <wpi/function_ref.h>
|
||||
|
||||
#include "glass/Model.h"
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include <string_view>
|
||||
|
||||
#include <wpi/STLExtras.h>
|
||||
#include <wpi/function_ref.h>
|
||||
|
||||
#include "glass/Model.h"
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
#include <frc/geometry/Rotation2d.h>
|
||||
#include <frc/geometry/Translation2d.h>
|
||||
#include <imgui.h>
|
||||
#include <wpi/STLExtras.h>
|
||||
#include <wpi/function_ref.h>
|
||||
#include <wpi/span.h>
|
||||
|
||||
#include "glass/Model.h"
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <frc/geometry/Rotation2d.h>
|
||||
#include <frc/geometry/Translation2d.h>
|
||||
#include <imgui.h>
|
||||
#include <wpi/STLExtras.h>
|
||||
#include <wpi/function_ref.h>
|
||||
|
||||
#include "glass/Model.h"
|
||||
#include "glass/View.h"
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
#include <vector>
|
||||
|
||||
#include <ntcore_cpp.h>
|
||||
#include <wpi/STLExtras.h>
|
||||
|
||||
#include "glass/DataSource.h"
|
||||
#include "glass/networktables/NetworkTablesHelper.h"
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
#include <vector>
|
||||
|
||||
#include <ntcore_cpp.h>
|
||||
#include <wpi/STLExtras.h>
|
||||
|
||||
#include "glass/DataSource.h"
|
||||
#include "glass/networktables/NetworkTablesHelper.h"
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
|
||||
#include <ntcore_cpp.h>
|
||||
#include <wpi/DenseMap.h>
|
||||
#include <wpi/STLExtras.h>
|
||||
|
||||
#include "glass/Model.h"
|
||||
#include "glass/View.h"
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#include <string_view>
|
||||
|
||||
#include <networktables/NetworkTable.h>
|
||||
#include <wpi/STLExtras.h>
|
||||
#include <wpi/function_ref.h>
|
||||
|
||||
namespace frc {
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#define WPIUTIL_WPI_STLEXTRAS_H
|
||||
|
||||
#include "wpi/SmallVector.h"
|
||||
#include "wpi/function_ref.h"
|
||||
#include "wpi/iterator.h"
|
||||
#include "wpi/iterator_range.h"
|
||||
#include "wpi/ErrorHandling.h"
|
||||
@@ -94,44 +95,6 @@ template <class Ty> struct greater_ptr {
|
||||
}
|
||||
};
|
||||
|
||||
/// An efficient, type-erasing, non-owning reference to a callable. This is
|
||||
/// intended for use as the type of a function parameter that is not used
|
||||
/// after the function in question returns.
|
||||
///
|
||||
/// This class does not own the callable, so it is not in general safe to store
|
||||
/// a function_ref.
|
||||
template<typename Fn> class function_ref;
|
||||
|
||||
template<typename Ret, typename ...Params>
|
||||
class function_ref<Ret(Params...)> {
|
||||
Ret (*callback)(intptr_t callable, Params ...params) = nullptr;
|
||||
intptr_t callable;
|
||||
|
||||
template<typename Callable>
|
||||
static Ret callback_fn(intptr_t callable, Params ...params) {
|
||||
return (*reinterpret_cast<Callable*>(callable))(
|
||||
std::forward<Params>(params)...);
|
||||
}
|
||||
|
||||
public:
|
||||
function_ref() = default;
|
||||
function_ref(std::nullptr_t) {}
|
||||
|
||||
template <typename Callable>
|
||||
function_ref(Callable &&callable,
|
||||
typename std::enable_if<
|
||||
!std::is_same<typename std::remove_reference<Callable>::type,
|
||||
function_ref>::value>::type * = nullptr)
|
||||
: callback(callback_fn<typename std::remove_reference<Callable>::type>),
|
||||
callable(reinterpret_cast<intptr_t>(&callable)) {}
|
||||
|
||||
Ret operator()(Params ...params) const {
|
||||
return callback(callable, std::forward<Params>(params)...);
|
||||
}
|
||||
|
||||
explicit operator bool() const { return callback; }
|
||||
};
|
||||
|
||||
// deleter - Very very very simple method that is used to invoke operator
|
||||
// delete on something. It is used like this:
|
||||
//
|
||||
|
||||
65
wpiutil/src/main/native/include/wpi/function_ref.h
Normal file
65
wpiutil/src/main/native/include/wpi/function_ref.h
Normal file
@@ -0,0 +1,65 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
//===- llvm/ADT/STLExtras.h - Useful STL related functions ------*- C++ -*-===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef WPIUTIL_WPI_FUNCTION_REF_H_
|
||||
#define WPIUTIL_WPI_FUNCTION_REF_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace wpi {
|
||||
|
||||
/// An efficient, type-erasing, non-owning reference to a callable. This is
|
||||
/// intended for use as the type of a function parameter that is not used
|
||||
/// after the function in question returns.
|
||||
///
|
||||
/// This class does not own the callable, so it is not in general safe to store
|
||||
/// a function_ref.
|
||||
template <typename Fn>
|
||||
class function_ref;
|
||||
|
||||
template <typename Ret, typename... Params>
|
||||
class function_ref<Ret(Params...)> {
|
||||
Ret (*callback)(intptr_t callable, Params... params) = nullptr;
|
||||
intptr_t callable;
|
||||
|
||||
template <typename Callable>
|
||||
static Ret callback_fn(intptr_t callable, Params... params) {
|
||||
return (*reinterpret_cast<Callable*>(callable))(
|
||||
std::forward<Params>(params)...);
|
||||
}
|
||||
|
||||
public:
|
||||
function_ref() = default;
|
||||
function_ref(std::nullptr_t) {}
|
||||
|
||||
template <typename Callable>
|
||||
function_ref(Callable&& callable,
|
||||
typename std::enable_if<
|
||||
!std::is_same<typename std::remove_reference<Callable>::type,
|
||||
function_ref>::value>::type* = nullptr)
|
||||
: callback(callback_fn<typename std::remove_reference<Callable>::type>),
|
||||
callable(reinterpret_cast<intptr_t>(&callable)) {}
|
||||
|
||||
Ret operator()(Params... params) const {
|
||||
return callback(callable, std::forward<Params>(params)...);
|
||||
}
|
||||
|
||||
explicit operator bool() const { return callback; }
|
||||
};
|
||||
|
||||
} // namespace wpi
|
||||
|
||||
#endif // WPIUTIL_WPI_FUNCTION_REF_H_
|
||||
Reference in New Issue
Block a user