Skip to content

Commit

Permalink
third_party/fuchsia: Copybara import of the fit library
Browse files Browse the repository at this point in the history
  - f9f809cc1015ee6bc529728aecac09b0a20d17fa [fit] static_assert for bad function instantiation
  - fa85e608bba04b8e7e316f8159b29fa0b735f6ff [fit] Fix internal class template parameters

Manually fix negative compilation test failures for pw::Function.

GitOrigin-RevId: f9f809cc1015ee6bc529728aecac09b0a20d17fa
Change-Id: Ie23320ca819c41546071df13e99bb626beed8537
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/107660
Commit-Queue: Auto-Submit <[email protected]>
Pigweed-Auto-Submit: Wyatt Hepler <[email protected]>
Reviewed-by: Erik Gilling <[email protected]>
  • Loading branch information
Fuchsia Authors authored and CQ Bot Account committed Aug 20, 2022
1 parent 620bb53 commit 2841e8e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
8 changes: 4 additions & 4 deletions pw_function/function_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ namespace pw {
namespace {

#if PW_NC_TEST(CannotInstantiateWithNonFunction)
PW_NC_EXPECT("incomplete type|undefined template");
PW_NC_EXPECT("must be instantiated with a function type");

[[maybe_unused]] Function<int> function_pointer;

#elif PW_NC_TEST(CannotInstantiateWithFunctionPointer1)
PW_NC_EXPECT("incomplete type|undefined template");
PW_NC_EXPECT("must be instantiated with a function type");

[[maybe_unused]] Function<void (*)()> function_pointer;

#elif PW_NC_TEST(CannotInstantiateWithFunctionPointer2)
PW_NC_EXPECT("incomplete type|undefined template");
PW_NC_EXPECT("must be instantiated with a function type");

[[maybe_unused]] void SomeFunction(int);

[[maybe_unused]] Function<decltype(&SomeFunction)> function_pointer;

#elif PW_NC_TEST(CannotInstantiateWithFunctionReference)
PW_NC_EXPECT("incomplete type|undefined template");
PW_NC_EXPECT("must be instantiated with a function type");

[[maybe_unused]] Function<void (&)()> function_pointer;

Expand Down
62 changes: 36 additions & 26 deletions third_party/fuchsia/repo/sdk/lib/fit/include/lib/fit/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@
#ifndef LIB_FIT_INCLUDE_LIB_FIT_FUNCTION_H_
#define LIB_FIT_INCLUDE_LIB_FIT_FUNCTION_H_

#include <type_traits>

#include "function_internal.h"
#include "traits.h"
#include "utility_internal.h"

namespace fit {

template <size_t inline_target_size, bool require_inline, typename Result, typename... Args>
class function_impl;
template <size_t inline_target_size, bool require_inline, typename Callable>
class function_impl {
static_assert(std::is_function<Callable>::value,
"fit::function must be instantiated with a function type, such as void() or "
"int(char*, bool)");
};

template <size_t inline_target_size, bool require_inline, typename Result, typename... Args>
class callback_impl;
template <size_t inline_target_size, bool require_inline, typename Callable>
class callback_impl {
static_assert(std::is_function<Callable>::value,
"fit::callback must be instantiated with a function type, such as void() or "
"int(char*, bool)");
};

// The default size allowance for storing a target inline within a function
// object, in bytes. This default allows for inline storage of targets
Expand Down Expand Up @@ -302,30 +312,30 @@ class function_impl<inline_target_size, require_inline, Result(Args...)> final
}
};

template <size_t inline_target_size, bool require_inline, typename Result, typename... Args>
void swap(function_impl<inline_target_size, require_inline, Result, Args...>& a,
function_impl<inline_target_size, require_inline, Result, Args...>& b) {
template <size_t inline_target_size, bool require_inline, typename Callable>
void swap(function_impl<inline_target_size, require_inline, Callable>& a,
function_impl<inline_target_size, require_inline, Callable>& b) {
a.swap(b);
}

template <size_t inline_target_size, bool require_inline, typename Result, typename... Args>
bool operator==(const function_impl<inline_target_size, require_inline, Result, Args...>& f,
template <size_t inline_target_size, bool require_inline, typename Callable>
bool operator==(const function_impl<inline_target_size, require_inline, Callable>& f,
decltype(nullptr)) {
return !f;
}
template <size_t inline_target_size, bool require_inline, typename Result, typename... Args>
template <size_t inline_target_size, bool require_inline, typename Callable>
bool operator==(decltype(nullptr),
const function_impl<inline_target_size, require_inline, Result, Args...>& f) {
const function_impl<inline_target_size, require_inline, Callable>& f) {
return !f;
}
template <size_t inline_target_size, bool require_inline, typename Result, typename... Args>
bool operator!=(const function_impl<inline_target_size, require_inline, Result, Args...>& f,
template <size_t inline_target_size, bool require_inline, typename Callable>
bool operator!=(const function_impl<inline_target_size, require_inline, Callable>& f,
decltype(nullptr)) {
return !!f;
}
template <size_t inline_target_size, bool require_inline, typename Result, typename... Args>
template <size_t inline_target_size, bool require_inline, typename Callable>
bool operator!=(decltype(nullptr),
const function_impl<inline_target_size, require_inline, Result, Args...>& f) {
const function_impl<inline_target_size, require_inline, Callable>& f) {
return !!f;
}

Expand Down Expand Up @@ -458,30 +468,30 @@ class callback_impl<inline_target_size, require_inline, Result(Args...)> final
}
};

template <size_t inline_target_size, bool require_inline, typename Result, typename... Args>
void swap(callback_impl<inline_target_size, require_inline, Result, Args...>& a,
callback_impl<inline_target_size, require_inline, Result, Args...>& b) {
template <size_t inline_target_size, bool require_inline, typename Callable>
void swap(callback_impl<inline_target_size, require_inline, Callable>& a,
callback_impl<inline_target_size, require_inline, Callable>& b) {
a.swap(b);
}

template <size_t inline_target_size, bool require_inline, typename Result, typename... Args>
bool operator==(const callback_impl<inline_target_size, require_inline, Result, Args...>& f,
template <size_t inline_target_size, bool require_inline, typename Callable>
bool operator==(const callback_impl<inline_target_size, require_inline, Callable>& f,
decltype(nullptr)) {
return !f;
}
template <size_t inline_target_size, bool require_inline, typename Result, typename... Args>
template <size_t inline_target_size, bool require_inline, typename Callable>
bool operator==(decltype(nullptr),
const callback_impl<inline_target_size, require_inline, Result, Args...>& f) {
const callback_impl<inline_target_size, require_inline, Callable>& f) {
return !f;
}
template <size_t inline_target_size, bool require_inline, typename Result, typename... Args>
bool operator!=(const callback_impl<inline_target_size, require_inline, Result, Args...>& f,
template <size_t inline_target_size, bool require_inline, typename Callable>
bool operator!=(const callback_impl<inline_target_size, require_inline, Callable>& f,
decltype(nullptr)) {
return !!f;
}
template <size_t inline_target_size, bool require_inline, typename Result, typename... Args>
template <size_t inline_target_size, bool require_inline, typename Callable>
bool operator!=(decltype(nullptr),
const callback_impl<inline_target_size, require_inline, Result, Args...>& f) {
const callback_impl<inline_target_size, require_inline, Callable>& f) {
return !!f;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ constexpr target_ops<Result, Args...> target<SharedFunction,
/*is_shared=*/true, Result, Args...>::ops = {
&target::target_type_id, &target::get, &target::invoke, &target::move, &target::destroy};

template <size_t inline_target_size, bool require_inline, typename Result, typename... Args>
template <size_t inline_target_size, bool require_inline, typename Callable>
class function_base;

// Function implementation details.
Expand Down

0 comments on commit 2841e8e

Please sign in to comment.