Skip to content
This repository was archived by the owner on Jan 15, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions core-util/v2/detail/allocators.hpp
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
152 changes: 152 additions & 0 deletions core-util/v2/detail/capture.hpp
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this is virtual ?

Copy link
Contributor Author

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.

Copy link
Member

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.

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
48 changes: 48 additions & 0 deletions core-util/v2/detail/functor.hpp
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
52 changes: 52 additions & 0 deletions core-util/v2/detail/interface.hpp
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
Loading