cpptick - initial
This commit is contained in:
parent
0698081d7b
commit
72961f2750
@ -1,6 +1,6 @@
|
|||||||
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
|
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
|
||||||
|
|
||||||
set(SKULLC_VERSION 0.1.0)
|
set(SKULLC_VERSION 0.2.0)
|
||||||
|
|
||||||
project(skullc
|
project(skullc
|
||||||
VERSION ${SKULLC_VERSION}
|
VERSION ${SKULLC_VERSION}
|
||||||
@ -19,6 +19,7 @@ include(skullc-install)
|
|||||||
|
|
||||||
add_subdirectory(Peripherals)
|
add_subdirectory(Peripherals)
|
||||||
add_subdirectory(Utility)
|
add_subdirectory(Utility)
|
||||||
|
add_subdirectory(CppTick)
|
||||||
add_subdirectory(Messaging)
|
add_subdirectory(Messaging)
|
||||||
|
|
||||||
if(SKULLC_WITH_TESTS)
|
if(SKULLC_WITH_TESTS)
|
||||||
|
|||||||
23
CppTick/CMakeLists.txt
Normal file
23
CppTick/CMakeLists.txt
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
|
||||||
|
|
||||||
|
add_library(cpptick INTERFACE)
|
||||||
|
add_library(skullc::cpptick ALIAS cpptick)
|
||||||
|
|
||||||
|
target_include_directories(cpptick
|
||||||
|
INTERFACE
|
||||||
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/Inc>
|
||||||
|
$<INSTALL_INTERFACE:include>
|
||||||
|
)
|
||||||
|
|
||||||
|
set_target_properties(cpptick
|
||||||
|
PROPERTIES
|
||||||
|
CXX_STANDARD 20
|
||||||
|
CXX_STANDARD_REQUIRED TRUE
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(cpptick
|
||||||
|
INTERFACE
|
||||||
|
skullc::utility
|
||||||
|
)
|
||||||
|
|
||||||
|
skullc_install_packages(skullc cpptick ${SKULLC_VERSION})
|
||||||
235
CppTick/Inc/cpptick/cpptick.hpp
Normal file
235
CppTick/Inc/cpptick/cpptick.hpp
Normal file
@ -0,0 +1,235 @@
|
|||||||
|
//
|
||||||
|
// Created by erki on 12/09/23.
|
||||||
|
//
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
|
#include <memory>
|
||||||
|
#include <array>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
#include <utility_function.hpp>
|
||||||
|
#include <utility_ringbuffer.hpp>
|
||||||
|
|
||||||
|
namespace cpptick
|
||||||
|
{
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
concept TrivialClass = std::is_trivial_v<T> && std::is_trivially_destructible_v<T>;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
concept CopyableClass = !TrivialClass<T> && std::copy_constructible<T>;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
concept SlotArgument = TrivialClass<T> || CopyableClass<T>;
|
||||||
|
|
||||||
|
struct ArgStorage
|
||||||
|
{
|
||||||
|
std::array<char, sizeof(int) * 12> buffer;
|
||||||
|
std::array<char*, 12> pointer_buffer;
|
||||||
|
std::array<char*, 12>::iterator pointer_buffer_head;
|
||||||
|
std::size_t space_remaining;
|
||||||
|
|
||||||
|
ArgStorage() = default;
|
||||||
|
ArgStorage(const ArgStorage&) = default;
|
||||||
|
ArgStorage(ArgStorage&&) = default;
|
||||||
|
|
||||||
|
void reset()
|
||||||
|
{
|
||||||
|
space_remaining = buffer.size();
|
||||||
|
pointer_buffer_head = pointer_buffer.begin();
|
||||||
|
*pointer_buffer_head = buffer.data();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<TrivialClass T>
|
||||||
|
void pushArg(T&& arg)
|
||||||
|
{
|
||||||
|
if (pointer_buffer_head == pointer_buffer.end() || space_remaining == 0)
|
||||||
|
std::exit(1);
|
||||||
|
|
||||||
|
void* memory_location = *pointer_buffer_head;
|
||||||
|
|
||||||
|
if (std::align(alignof(T), sizeof(T), memory_location, space_remaining) == nullptr)
|
||||||
|
std::exit(2);
|
||||||
|
|
||||||
|
std::memcpy(memory_location, &arg, sizeof(T));
|
||||||
|
pointer_buffer_head++;
|
||||||
|
if (pointer_buffer_head != pointer_buffer.end())
|
||||||
|
{
|
||||||
|
*pointer_buffer_head = (char*)memory_location + sizeof(T);
|
||||||
|
space_remaining -= sizeof(T);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<CopyableClass T>
|
||||||
|
void pushArg(T&& arg)
|
||||||
|
{
|
||||||
|
if (pointer_buffer_head == pointer_buffer.end() || space_remaining == 0)
|
||||||
|
std::exit(1);
|
||||||
|
|
||||||
|
void* memory_location = *pointer_buffer_head;
|
||||||
|
|
||||||
|
if (std::align(alignof(T), sizeof(T), memory_location, space_remaining) == nullptr)
|
||||||
|
std::exit(2);
|
||||||
|
|
||||||
|
new (memory_location) T{arg};
|
||||||
|
pointer_buffer_head++;
|
||||||
|
if (pointer_buffer_head != pointer_buffer.end())
|
||||||
|
{
|
||||||
|
*pointer_buffer_head = (char*)memory_location + sizeof(T);
|
||||||
|
space_remaining -= sizeof(T);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<TrivialClass T>
|
||||||
|
void cleanUp(const std::size_t)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
template<CopyableClass T>
|
||||||
|
void cleanUp(const std::size_t index)
|
||||||
|
{
|
||||||
|
T* item = reinterpret_cast<T*>(pointer_buffer[index]);
|
||||||
|
item->~T();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args, size_t... Is>
|
||||||
|
void cleanUp(std::index_sequence<Is...>)
|
||||||
|
{
|
||||||
|
(cleanUp<Args>(Is), ...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void cleanUp()
|
||||||
|
{
|
||||||
|
cleanUp<Args...>(std::make_index_sequence<sizeof...(Args)>{});
|
||||||
|
}
|
||||||
|
|
||||||
|
template<SlotArgument T>
|
||||||
|
T& at(const std::size_t idx)
|
||||||
|
{
|
||||||
|
return *((T*)pointer_buffer[idx]);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<SlotArgument T>
|
||||||
|
const T& at(const std::size_t idx) const
|
||||||
|
{
|
||||||
|
return *((T*)pointer_buffer[idx]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Scheduler
|
||||||
|
{
|
||||||
|
using StoredCall = std::pair<Utility::IFunction<void (ArgStorage&)>*, ArgStorage>;
|
||||||
|
Utility::Ringbuffer<StoredCall, 12> stored_calls;
|
||||||
|
|
||||||
|
void tick()
|
||||||
|
{
|
||||||
|
if (!stored_calls.empty())
|
||||||
|
{
|
||||||
|
auto* f = stored_calls.begin()->first;
|
||||||
|
auto& args = stored_calls.begin()->second;
|
||||||
|
(*f)(args);
|
||||||
|
stored_calls.pop_front();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ArgStorage& storeCall(Utility::IFunction<void (ArgStorage&)>* call)
|
||||||
|
{
|
||||||
|
// @todo: handle overflow...
|
||||||
|
|
||||||
|
// still constructs double.
|
||||||
|
stored_calls.emplace_back(call, ArgStorage{});
|
||||||
|
auto& storage = stored_calls.back().second;
|
||||||
|
storage.reset();
|
||||||
|
return storage;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename>
|
||||||
|
struct Slot;
|
||||||
|
|
||||||
|
template<typename R, SlotArgument... Args>
|
||||||
|
struct Slot<R(Args...)>
|
||||||
|
{
|
||||||
|
std::array<Utility::IFunction<R (Args...)>*, 12> signals;
|
||||||
|
Scheduler* scheduler;
|
||||||
|
|
||||||
|
Utility::FunctionOwned<Slot<R(Args...)>, void (ArgStorage&)> invoke_ptr;
|
||||||
|
|
||||||
|
Slot() = delete;
|
||||||
|
explicit Slot(Scheduler* sched)
|
||||||
|
: scheduler(sched)
|
||||||
|
, invoke_ptr(*this, &Slot<R(Args...)>::callUp)
|
||||||
|
{
|
||||||
|
signals.fill(nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
Slot(const Slot&) = delete;
|
||||||
|
Slot(Slot&&) = delete;
|
||||||
|
Slot& operator=(const Slot&) = delete;
|
||||||
|
Slot& operator=(Slot&&) = delete;
|
||||||
|
|
||||||
|
void connect(Utility::IFunction<R (Args...)>* signal)
|
||||||
|
{
|
||||||
|
for (auto*& callable : signals)
|
||||||
|
{
|
||||||
|
if (callable == nullptr)
|
||||||
|
{
|
||||||
|
callable = signal;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Utility::IFunction<R (Args...)>* connect(R (*func)(Args...))
|
||||||
|
{
|
||||||
|
auto* f = new Utility::Function<R (Args...)>(func);
|
||||||
|
|
||||||
|
connect(f);
|
||||||
|
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Source>
|
||||||
|
Utility::IFunction<R (Args...)>* connect(Source& src, R (Source::* func)(Args...))
|
||||||
|
{
|
||||||
|
auto* f = new Utility::FunctionOwned<Source, R (Args...)>(src, func);
|
||||||
|
|
||||||
|
connect(f);
|
||||||
|
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<size_t... Is>
|
||||||
|
void callUpFunction(Utility::IFunction<R (Args...)>* func, ArgStorage& args, std::index_sequence<Is...>)
|
||||||
|
{
|
||||||
|
(*func)(std::forward<Args>(args.at<Args>(Is))...);
|
||||||
|
}
|
||||||
|
|
||||||
|
void callUpFunction(Utility::IFunction<R (Args...)>* func, ArgStorage& args)
|
||||||
|
{
|
||||||
|
callUpFunction(func, args, std::make_index_sequence<sizeof...(Args)>{});
|
||||||
|
}
|
||||||
|
|
||||||
|
void callUp(ArgStorage& args)
|
||||||
|
{
|
||||||
|
for (auto* f : signals)
|
||||||
|
{
|
||||||
|
if (f)
|
||||||
|
callUpFunction(f, args);
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
args.cleanUp<Args...>();
|
||||||
|
}
|
||||||
|
|
||||||
|
void invoke(Args... args)
|
||||||
|
{
|
||||||
|
ArgStorage& storage = scheduler->storeCall(&invoke_ptr);
|
||||||
|
(storage.pushArg(std::forward<Args>(args)), ...);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
2
CppTick/cpptick-config.cmake.in
Normal file
2
CppTick/cpptick-config.cmake.in
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
include(${CMAKE_CURRENT_LIST_DIR}/skullc-cpptick-config-version.cmake)
|
||||||
|
include(${CMAKE_CURRENT_LIST_DIR}/skullc-cpptick-targets.cmake)
|
||||||
@ -31,6 +31,7 @@ add_executable(tests
|
|||||||
enum_helpers.cpp
|
enum_helpers.cpp
|
||||||
bytes.cpp
|
bytes.cpp
|
||||||
filters.cpp
|
filters.cpp
|
||||||
|
cpptick.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(tests
|
target_link_libraries(tests
|
||||||
@ -39,11 +40,12 @@ target_link_libraries(tests
|
|||||||
skullc::messaging
|
skullc::messaging
|
||||||
skullc::peripherals
|
skullc::peripherals
|
||||||
Catch2::Catch2
|
Catch2::Catch2
|
||||||
|
skullc::cpptick
|
||||||
)
|
)
|
||||||
|
|
||||||
set_target_properties(tests
|
set_target_properties(tests
|
||||||
PROPERTIES
|
PROPERTIES
|
||||||
CXX_STANDARD 17
|
CXX_STANDARD 20
|
||||||
)
|
)
|
||||||
|
|
||||||
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/contrib)
|
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/contrib)
|
||||||
|
|||||||
98
Tests/cpptick.cpp
Normal file
98
Tests/cpptick.cpp
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
//
|
||||||
|
// Created by erki on 12/09/23.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <catch2/catch.hpp>
|
||||||
|
|
||||||
|
#include <cpptick/cpptick.hpp>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
int callback_count = 0;
|
||||||
|
|
||||||
|
void callbackByOne()
|
||||||
|
{
|
||||||
|
callback_count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void callbackByN(int to_add)
|
||||||
|
{
|
||||||
|
callback_count += to_add;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Slot calls function properly with void args.", "[cpptick]")
|
||||||
|
{
|
||||||
|
cpptick::Scheduler scheduler;
|
||||||
|
|
||||||
|
cpptick::Slot<void ()> slot(&scheduler);
|
||||||
|
auto* f = slot.connect(callbackByOne);
|
||||||
|
|
||||||
|
callback_count = 0;
|
||||||
|
REQUIRE(callback_count == 0);
|
||||||
|
|
||||||
|
SECTION("invoke() calls the slot appropriately.")
|
||||||
|
{
|
||||||
|
slot.invoke();
|
||||||
|
CHECK(callback_count == 0);
|
||||||
|
|
||||||
|
scheduler.tick();
|
||||||
|
CHECK(callback_count == 1);
|
||||||
|
|
||||||
|
SECTION("Second tick doesn't invoke the slot again.")
|
||||||
|
{
|
||||||
|
scheduler.tick();
|
||||||
|
CHECK(callback_count == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Second invoke() will call the slot again.")
|
||||||
|
{
|
||||||
|
slot.invoke();
|
||||||
|
scheduler.tick();
|
||||||
|
CHECK(callback_count == 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Multiple calls queue properly.")
|
||||||
|
{
|
||||||
|
const int count = 3;
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
slot.invoke();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
CHECK(callback_count == i);
|
||||||
|
scheduler.tick();
|
||||||
|
CHECK(callback_count == i + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
delete f;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Slot calls function properly with args.", "[cpptick]")
|
||||||
|
{
|
||||||
|
cpptick::Scheduler scheduler;
|
||||||
|
|
||||||
|
cpptick::Slot<void (int)> slot(&scheduler);
|
||||||
|
auto* f = slot.connect(callbackByN);
|
||||||
|
|
||||||
|
callback_count = 0;
|
||||||
|
REQUIRE(callback_count == 0);
|
||||||
|
|
||||||
|
SECTION("invoke() passes argument appropriately.")
|
||||||
|
{
|
||||||
|
const int to_add = 5;
|
||||||
|
slot.invoke(to_add);
|
||||||
|
CHECK(callback_count == 0);
|
||||||
|
|
||||||
|
scheduler.tick();
|
||||||
|
CHECK(callback_count == to_add);
|
||||||
|
}
|
||||||
|
|
||||||
|
delete f;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user