This repository was archived by the owner on Jan 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Add a new Function object #61
Open
bremoran
wants to merge
14
commits into
ARMmbed:master
Choose a base branch
from
bremoran:functional
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6cb02e9
Initial commit of working functional
bremoran 095bf87
Basic version of bind working
bremoran bc0df16
Partial Bind (doesn’t work yet)
bremoran e1ddf38
First-argument partial-binding now working
bremoran 220d61b
Move capture into its own header
bremoran 2ffe636
Add definitions for duck-type compatibility
bremoran 15c8483
Updates to Function design
bremoran 27e5e06
Review updates:
bremoran e014f39
Add support APIs for C function escalation
bremoran ae0ad5e
Move library polyfill items to polyfill
bremoran 37e6d9f
Add is_same and conditional to library polyfill
bremoran 70df7d5
Fix armcc support
bremoran 86c8504
Use the alignment intrinsic instead of sizeof for sorting tuples
bremoran b3ba411
Fix the static init fiasco
bremoran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* mbed Microcontroller Library | ||
| * Copyright (c) 2006-2015 ARM Limited | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| #ifndef FUNCTIONAL_DETAIL_ALLOCATORS_HPP | ||
| #define FUNCTIONAL_DETAIL_ALLOCATORS_HPP | ||
|
|
||
| #include "interface.hpp" | ||
|
|
||
| #include "core-util/ExtendablePoolAllocator.h" | ||
| #include "ualloc/ualloc.h" | ||
|
|
||
| #include <cstdint> | ||
| namespace functional { | ||
| namespace detail { | ||
|
|
||
| class ContainerAllocator : public mbed::util::ExtendablePoolAllocator { | ||
| public: | ||
| template <typename FunctionType> | ||
| void free(FunctionInterface<FunctionType> * ptr) { | ||
| ptr->~FunctionInterface<FunctionType>(); | ||
| mbed::util::ExtendablePoolAllocator::free(ptr); | ||
| } | ||
| ContainerAllocator(std::size_t initial_elements, std::size_t new_pool_elements, std::size_t element_size) : | ||
| mbed::util::ExtendablePoolAllocator() | ||
| { | ||
| this->init(initial_elements, new_pool_elements, element_size, UAllocTraits_t{.flags = UALLOC_TRAITS_NEVER_FREE}, | ||
| MBED_UTIL_POOL_ALLOC_DEFAULT_ALIGN); | ||
| } | ||
| }; | ||
|
|
||
| template <std::size_t ALLOC_SIZE> | ||
| class ContainerAllocatorWrapper { | ||
| public: | ||
| static const std::size_t size = ALLOC_SIZE; | ||
| static ContainerAllocator & instance(); | ||
| }; | ||
|
|
||
| namespace alloc_size { | ||
| class BaseClass { | ||
| public: | ||
| virtual void base_function(){} | ||
| }; | ||
| class VirtualClass : public BaseClass { | ||
| virtual void base_function(){} | ||
| }; | ||
| class UnknownClass; | ||
|
|
||
| #ifndef YOTTA_CONFIG_CORE_UTIL_FUNCTIONAL_FUNCTOR_SIZE | ||
| /* | ||
| * This size was chosen to allow a Function to bind a DNS response. | ||
| * 4 bytes for a reference count | ||
| * 4 bytes for the vtable pointers | ||
| * 4 bytes for the base Function | ||
| * 128/8 bytes for an IPv6 address | ||
| * 4 bytes for a char* pointer. | ||
| * 4 bytes of padding to round out to 8-byte alignment. | ||
| */ | ||
| #define YOTTA_CONFIG_CORE_UTIL_FUNCTIONAL_FUNCTOR_SIZE \ | ||
| ((sizeof(std::uint32_t) + sizeof(VirtualClass) + sizeof(UnknownClass *) + 128/8 + sizeof(char *) + 7) & ~7) | ||
| #endif | ||
|
|
||
| const std::size_t staticfp = sizeof(std::uint32_t) + sizeof(VirtualClass) + sizeof(void(*)()); | ||
| const std::size_t memberfp = sizeof(std::uint32_t) + sizeof(VirtualClass) + sizeof(UnknownClass *) + sizeof(void (UnknownClass::*)()); | ||
| const std::size_t functorfp = YOTTA_CONFIG_CORE_UTIL_FUNCTIONAL_FUNCTOR_SIZE; | ||
| } | ||
|
|
||
| using StaticFPAllocator = ContainerAllocatorWrapper<alloc_size::staticfp>; | ||
| using MemberFPAllocator = ContainerAllocatorWrapper<alloc_size::memberfp>; | ||
| using FunctorFPAllocator = ContainerAllocatorWrapper<alloc_size::functorfp>; | ||
|
|
||
| } // detail | ||
| } // functional | ||
| #endif // FUNCTIONAL_DETAIL_ALLOCATORS_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| /* mbed Microcontroller Library | ||
| * Copyright (c) 2006-2015 ARM Limited | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| #ifndef FUNCTIONAL_DETAIL_CAPTURE_HPP | ||
| #define FUNCTIONAL_DETAIL_CAPTURE_HPP | ||
|
|
||
| #include "polyfill.hpp" | ||
|
|
||
| #include "interface.hpp" | ||
| #include "allocators.hpp" | ||
|
|
||
| #include "core-util/assert.h" | ||
|
|
||
| namespace functional { | ||
| namespace detail { | ||
|
|
||
|
|
||
| namespace index { | ||
| template<std::size_t ...> | ||
| struct sequence { }; | ||
|
|
||
| template<std::size_t N, std::size_t ...S> | ||
| struct generator : generator<N-1, N-1, S...> { }; | ||
|
|
||
| template<std::size_t ...S> | ||
| struct generator<0, S...> { | ||
| typedef sequence<S...> type; | ||
| }; | ||
| } // namespace index | ||
|
|
||
| template <typename FunctionType, ContainerAllocator & (*Allocator)(), typename... CapturedTypes> | ||
| class CaptureFirst; | ||
|
|
||
| template <typename ReturnType, typename... ArgTypes, ContainerAllocator & (*Allocator)(), typename... CapturedTypes> | ||
| class CaptureFirst <ReturnType(ArgTypes...), Allocator, CapturedTypes...> | ||
| : public FunctionInterface <ReturnType(ArgTypes...)> { | ||
| public: | ||
| using stype = polyfill::tuple<CapturedTypes...>; | ||
| CaptureFirst(const stype & t, Function<ReturnType(CapturedTypes..., ArgTypes...)>& f): | ||
| f(f), storage(t) | ||
| {} | ||
|
|
||
| virtual ReturnType operator () (ArgTypes&&... Args) { | ||
| return idxcall(typename index::generator<sizeof...(CapturedTypes)>::type(), polyfill::forward<ArgTypes>(Args)...); | ||
| } | ||
| template <size_t... S> | ||
| inline ReturnType idxcall(index::sequence<S...>, ArgTypes&&... Args) { | ||
| return f( | ||
| polyfill::forward<CapturedTypes>( | ||
| polyfill::get<S,CapturedTypes...>(storage) | ||
| )..., | ||
| polyfill::forward<ArgTypes>(Args)...); | ||
| } | ||
|
|
||
| virtual ContainerAllocator & get_allocator() { | ||
| return Allocator(); | ||
| } | ||
| protected: | ||
| /* | ||
| * Future Optimization Note: It is possible to reduce memory consumption and call | ||
| * overhead by making CaptureFirst inherit from each of the FunctionInterface | ||
| * types, rather than just from FunctionInterface. | ||
| * | ||
| * In this case, however, a smarter allocator would help | ||
| */ | ||
| Function<ReturnType(CapturedTypes..., ArgTypes...)> & f; | ||
| polyfill::tuple<CapturedTypes...> storage; | ||
| }; | ||
|
|
||
| template <typename FunctionType, ContainerAllocator & (*Allocator)(), typename... CapturedTypes> | ||
| class CaptureLast; | ||
|
|
||
| template <typename ReturnType, typename... ArgTypes, ContainerAllocator & (*Allocator)(), typename... CapturedTypes> | ||
| class CaptureLast <ReturnType(ArgTypes...), Allocator, CapturedTypes...> | ||
| : public FunctionInterface <ReturnType(ArgTypes...)> { | ||
| public: | ||
| CaptureLast(Function<ReturnType(ArgTypes..., CapturedTypes...)> & f, CapturedTypes&&... CapturedArgs) : | ||
| f(f), storage(CapturedArgs...) | ||
| {} | ||
|
|
||
| virtual ReturnType operator () (ArgTypes&&... Args) { | ||
| return idxcall(typename index::generator<sizeof...(CapturedTypes)>::type(), polyfill::forward<ArgTypes>(Args)...); | ||
| } | ||
| template <size_t... S> | ||
| inline ReturnType idxcall(index::sequence<S...>, ArgTypes&&... Args) { | ||
| return f(polyfill::forward<ArgTypes>(Args)..., polyfill::forward<CapturedTypes>(polyfill::get<S,CapturedTypes...>(storage))...); | ||
| } | ||
|
|
||
| virtual ContainerAllocator & get_allocator() { | ||
| return Allocator(); | ||
| } | ||
| protected: | ||
|
|
||
| Function<ReturnType(ArgTypes..., CapturedTypes...)> & f; | ||
| polyfill::tuple<CapturedTypes...> storage; | ||
| }; | ||
|
|
||
| template <typename FunctionType, typename... ToRemove> struct RemoveFirstArgs {}; | ||
|
|
||
| template <typename ReturnType, typename... ArgTypes> | ||
| struct RemoveFirstArgs <ReturnType(ArgTypes...)> { | ||
| typedef Function<ReturnType(ArgTypes...)> type; | ||
| }; | ||
|
|
||
| template <typename ReturnType, typename RemovedArg, typename... ArgTypes, typename ToRemove0, typename... ToRemove> | ||
| struct RemoveFirstArgs <ReturnType(RemovedArg, ArgTypes...), ToRemove0, ToRemove...> { | ||
| static_assert(polyfill::is_same<RemovedArg, ToRemove0>::value, "Type mismatch in argument removal"); | ||
| typedef typename RemoveFirstArgs<ReturnType(ArgTypes...), ToRemove...>::type type; | ||
| }; | ||
|
|
||
| template <typename FunctionType0, typename FunctionType1, typename... RemoveTypes> struct RemoveLastArgs; | ||
|
|
||
| template <typename ReturnType, typename... Types, typename... RemoveTypes> | ||
| struct RemoveLastArgs <ReturnType(Types...), ReturnType(), RemoveTypes...> { | ||
| using type = void; | ||
| }; | ||
|
|
||
| template <typename ReturnType, typename... Types0, typename Transfer1, typename... Types1, typename... RemoveTypes> | ||
| struct RemoveLastArgs <ReturnType(Types0...), ReturnType(Transfer1, Types1...), RemoveTypes...> { | ||
| using type = typename polyfill::conditional< | ||
| polyfill::is_same<polyfill::tuple<Types1...>,polyfill::tuple<RemoveTypes...> >::value, | ||
| Function<ReturnType(Types0..., Transfer1)>, | ||
| typename RemoveLastArgs<ReturnType(Types0..., Transfer1), ReturnType(Types1...), RemoveTypes...>::type | ||
| >::type; | ||
| }; | ||
|
|
||
| template <typename ReturnType, typename... ArgTypes, typename... ParentTypes, typename... CapturedTypes> | ||
| Function<ReturnType(ArgTypes...)> bind_last(Function<ReturnType(ArgTypes...)> &&, Function<ReturnType(ParentTypes...)>& f, CapturedTypes... CapturedArgs) { | ||
| using CaptureFP = CaptureLast<ReturnType(ArgTypes...), FunctorFPAllocator::instance, CapturedTypes...>; | ||
| static_assert(sizeof(CaptureFP) <= alloc_size::functorfp, "Size of bound arguments is too large" ); | ||
| CaptureFP * newf = reinterpret_cast<CaptureFP *>(detail::FunctorFPAllocator::instance().alloc()); | ||
| CORE_UTIL_ASSERT_MSG(newf, "Function container memory allocation failed"); | ||
| new(newf) CaptureFP(f,polyfill::forward<CapturedTypes>(CapturedArgs)...); | ||
| return Function<ReturnType(ArgTypes...)>(static_cast<FunctionInterface<ReturnType(ArgTypes...)>*>(newf)); | ||
| } | ||
|
|
||
|
|
||
| } // namespace detail | ||
| } // namespace functional | ||
| #endif // FUNCTIONAL_DETAIL_CAPTURE_HPP | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* mbed Microcontroller Library | ||
| * Copyright (c) 2006-2015 ARM Limited | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| #ifndef FUNCTIONAL_DETAIL_FUNCTOR_HPP | ||
| #define FUNCTIONAL_DETAIL_FUNCTOR_HPP | ||
|
|
||
| #include "interface.hpp" | ||
| #include "allocators.hpp" | ||
|
|
||
| namespace functional { | ||
| namespace detail { | ||
|
|
||
|
|
||
| template <typename F, typename FunctionType, ContainerAllocator & (*Allocator)()> | ||
| class FunctorContainer; | ||
|
|
||
| template <typename F, typename ReturnType, typename... ArgTypes, ContainerAllocator & (*Allocator)()> | ||
| class FunctorContainer <F, ReturnType(ArgTypes...), Allocator> : public FunctionInterface <ReturnType(ArgTypes...)> { | ||
| public: | ||
| FunctorContainer(const F & f) : f(f) {} | ||
| FunctorContainer(const FunctorContainer & f) : f(f.f) {} | ||
|
|
||
| virtual ReturnType operator () (ArgTypes&&... Args) { | ||
| return f(polyfill::forward<ArgTypes>(Args)...); | ||
| } | ||
| virtual ContainerAllocator & get_allocator() { | ||
| return Allocator(); | ||
| } | ||
| protected: | ||
| F f; | ||
| }; | ||
|
|
||
|
|
||
| } // namespace detail | ||
| } // namespace functional | ||
| #endif // FUNCTIONAL_DETAIL_FUNCTOR_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* mbed Microcontroller Library | ||
| * Copyright (c) 2006-2015 ARM Limited | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| #ifndef FUNCTIONAL_V2_DETAIL_INTERFACE_HPP | ||
| #define FUNCTIONAL_V2_DETAIL_INTERFACE_HPP | ||
|
|
||
| #include "core-util/atomic_ops.h" | ||
| namespace functional { | ||
| namespace detail { | ||
| template <typename FunctionType> | ||
| class FunctionInterface; | ||
| } // namespace detail | ||
| } // namespace functional | ||
|
|
||
| #include "allocators.hpp" | ||
|
|
||
| namespace functional { | ||
| namespace detail { | ||
| template <typename ReturnType, typename... ArgTypes> | ||
| class FunctionInterface <ReturnType(ArgTypes...)> { | ||
| public: | ||
| FunctionInterface() : refcnt(0) {} | ||
| virtual ReturnType operator () (ArgTypes&&... Args) = 0; | ||
| virtual ContainerAllocator & get_allocator() = 0; | ||
| inline uint32_t inc() | ||
| { | ||
| return mbed::util::atomic_incr(&refcnt, static_cast<uint32_t>(1)); | ||
| } | ||
| inline uint32_t dec() | ||
| { | ||
| return mbed::util::atomic_decr(&refcnt, static_cast<uint32_t>(1)); | ||
| } | ||
| protected: | ||
| uint32_t refcnt; | ||
| }; | ||
|
|
||
| } // namespace detail | ||
| } // namespace functional | ||
|
|
||
| #endif // FUNCTIONAL_V2_DETAIL_INTERFACE_HPP |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this is virtual ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It probably doesn't need to be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I didn't saw that CaptureFirst inherit from FunctionInterface, it makes sense if it is virtual.