cpptick - format

This commit is contained in:
erki 2023-10-25 11:36:20 +03:00
parent ddb4931920
commit 0271b0d0de
7 changed files with 43 additions and 48 deletions

View File

@ -4,10 +4,10 @@
#pragma once #pragma once
#include <type_traits>
#include <array> #include <array>
#include <cstring> #include <cstring>
#include <memory> #include <memory>
#include <type_traits>
namespace cpptick namespace cpptick
{ {
@ -54,7 +54,7 @@ struct ArgStorage
pointer_buffer_head++; pointer_buffer_head++;
if (pointer_buffer_head != pointer_buffer.end()) if (pointer_buffer_head != pointer_buffer.end())
{ {
*pointer_buffer_head = (char*)memory_location + sizeof(T); *pointer_buffer_head = (char*) memory_location + sizeof(T);
space_remaining -= sizeof(T); space_remaining -= sizeof(T);
} }
} }
@ -74,14 +74,14 @@ struct ArgStorage
pointer_buffer_head++; pointer_buffer_head++;
if (pointer_buffer_head != pointer_buffer.end()) if (pointer_buffer_head != pointer_buffer.end())
{ {
*pointer_buffer_head = (char*)memory_location + sizeof(T); *pointer_buffer_head = (char*) memory_location + sizeof(T);
space_remaining -= sizeof(T); space_remaining -= sizeof(T);
} }
} }
template<TrivialClass T> template<TrivialClass T>
void cleanUp(const std::size_t) void cleanUp(const std::size_t)
{ } {}
template<CopyableClass T> template<CopyableClass T>
void cleanUp(const std::size_t index) void cleanUp(const std::size_t index)
@ -105,14 +105,14 @@ struct ArgStorage
template<SlotArgument T> template<SlotArgument T>
T& at(const std::size_t idx) T& at(const std::size_t idx)
{ {
return *((T*)pointer_buffer[idx]); return *((T*) pointer_buffer[idx]);
} }
template<SlotArgument T> template<SlotArgument T>
const T& at(const std::size_t idx) const const T& at(const std::size_t idx) const
{ {
return *((T*)pointer_buffer[idx]); return *((T*) pointer_buffer[idx]);
} }
}; };
} }// namespace cpptick

View File

@ -5,6 +5,6 @@
#pragma once #pragma once
#include "cpptick/argstore.hpp" #include "cpptick/argstore.hpp"
#include "cpptick/timer.hpp"
#include "cpptick/scheduler.hpp" #include "cpptick/scheduler.hpp"
#include "cpptick/slot.hpp" #include "cpptick/slot.hpp"
#include "cpptick/timer.hpp"

View File

@ -4,9 +4,9 @@
#pragma once #pragma once
#include <tuple>
#include <array>
#include <algorithm> #include <algorithm>
#include <array>
#include <tuple>
#include <utility_function.hpp> #include <utility_function.hpp>
#include <utility_ringbuffer.hpp> #include <utility_ringbuffer.hpp>
@ -18,7 +18,7 @@ namespace cpptick
struct BaseScheduler struct BaseScheduler
{ {
using StoredCall = std::pair<Utility::IFunction<void (ArgStorage&)>*, ArgStorage>; using StoredCall = std::pair<Utility::IFunction<void(ArgStorage&)>*, ArgStorage>;
Utility::Ringbuffer<StoredCall, 12> stored_calls; Utility::Ringbuffer<StoredCall, 12> stored_calls;
using StoredTimer = std::pair<std::uint32_t, Timer*>; using StoredTimer = std::pair<std::uint32_t, Timer*>;
@ -48,7 +48,7 @@ struct BaseScheduler
} }
} }
ArgStorage& storeCall(Utility::IFunction<void (ArgStorage&)>* call) ArgStorage& storeCall(Utility::IFunction<void(ArgStorage&)>* call)
{ {
// @todo: handle overflow... // @todo: handle overflow...
@ -81,7 +81,7 @@ struct Scheduler : BaseScheduler
if (empty_slot == stored_timers.end()) if (empty_slot == stored_timers.end())
return; return;
*empty_slot = { time_to_run, timer }; *empty_slot = {time_to_run, timer};
std::sort(stored_timers.begin(), stored_timers.end()); std::sort(stored_timers.begin(), stored_timers.end());
} }
@ -120,5 +120,4 @@ struct Scheduler : BaseScheduler
} }
}; };
} }// namespace cpptick

View File

@ -4,9 +4,9 @@
#pragma once #pragma once
#include <memory>
#include <array> #include <array>
#include <cstring> #include <cstring>
#include <memory>
#include <utility_function.hpp> #include <utility_function.hpp>
#include <utility_ringbuffer.hpp> #include <utility_ringbuffer.hpp>
@ -23,15 +23,14 @@ struct Slot;
template<typename R, SlotArgument... Args> template<typename R, SlotArgument... Args>
struct Slot<R(Args...)> struct Slot<R(Args...)>
{ {
std::array<Utility::IFunction<R (Args...)>*, 12> signals; std::array<Utility::IFunction<R(Args...)>*, 12> signals;
BaseScheduler* scheduler; BaseScheduler* scheduler;
Utility::FunctionOwned<Slot<R(Args...)>, void (ArgStorage&)> invoke_ptr; Utility::FunctionOwned<Slot<R(Args...)>, void(ArgStorage&)> invoke_ptr;
Slot() = delete; Slot() = delete;
explicit Slot(BaseScheduler* sched) explicit Slot(BaseScheduler* sched)
: scheduler(sched) : scheduler(sched), invoke_ptr(*this, &Slot<R(Args...)>::callUp)
, invoke_ptr(*this, &Slot<R(Args...)>::callUp)
{ {
signals.fill(nullptr); signals.fill(nullptr);
} }
@ -41,7 +40,7 @@ struct Slot<R(Args...)>
Slot& operator=(const Slot&) = delete; Slot& operator=(const Slot&) = delete;
Slot& operator=(Slot&&) = delete; Slot& operator=(Slot&&) = delete;
void connect(Utility::IFunction<R (Args...)>* signal) void connect(Utility::IFunction<R(Args...)>* signal)
{ {
for (auto*& callable : signals) for (auto*& callable : signals)
{ {
@ -53,9 +52,9 @@ struct Slot<R(Args...)>
} }
} }
Utility::IFunction<R (Args...)>* connect(R (*func)(Args...)) Utility::IFunction<R(Args...)>* connect(R (*func)(Args...))
{ {
auto* f = new Utility::Function<R (Args...)>(func); auto* f = new Utility::Function<R(Args...)>(func);
connect(f); connect(f);
@ -63,9 +62,9 @@ struct Slot<R(Args...)>
} }
template<typename Source> template<typename Source>
Utility::IFunction<R (Args...)>* connect(Source& src, R (Source::* func)(Args...)) Utility::IFunction<R(Args...)>* connect(Source& src, R (Source::*func)(Args...))
{ {
auto* f = new Utility::FunctionOwned<Source, R (Args...)>(src, func); auto* f = new Utility::FunctionOwned<Source, R(Args...)>(src, func);
connect(f); connect(f);
@ -73,12 +72,12 @@ struct Slot<R(Args...)>
} }
template<size_t... Is> template<size_t... Is>
void callUpFunction(Utility::IFunction<R (Args...)>* func, ArgStorage& args, std::index_sequence<Is...>) void callUpFunction(Utility::IFunction<R(Args...)>* func, ArgStorage& args, std::index_sequence<Is...>)
{ {
(*func)(std::forward<Args>(args.at<Args>(Is))...); (*func)(std::forward<Args>(args.at<Args>(Is))...);
} }
void callUpFunction(Utility::IFunction<R (Args...)>* func, ArgStorage& args) void callUpFunction(Utility::IFunction<R(Args...)>* func, ArgStorage& args)
{ {
callUpFunction(func, args, std::make_index_sequence<sizeof...(Args)>{}); callUpFunction(func, args, std::make_index_sequence<sizeof...(Args)>{});
} }
@ -103,4 +102,4 @@ struct Slot<R(Args...)>
} }
}; };
} }// namespace cpptick

View File

@ -25,10 +25,8 @@ struct Timer
}; };
Timer(BaseScheduler* scheduler, const std::uint32_t timeout_ms, const Type type) Timer(BaseScheduler* scheduler, const std::uint32_t timeout_ms, const Type type)
: scheduler_(scheduler) : scheduler_(scheduler), period_ms(timeout_ms), type(type)
, period_ms(timeout_ms) {}
, type(type)
{ }
Timer() = delete; Timer() = delete;
Timer(const Timer&) = delete; Timer(const Timer&) = delete;
@ -44,12 +42,12 @@ struct Timer
setSlot(&slot.invoke_ptr); setSlot(&slot.invoke_ptr);
} }
void setSlot(Utility::IFunction<void (ArgStorage&)>* slot) void setSlot(Utility::IFunction<void(ArgStorage&)>* slot)
{ {
slot_ = slot; slot_ = slot;
} }
Utility::IFunction<void (ArgStorage&)>* getSlot() Utility::IFunction<void(ArgStorage&)>* getSlot()
{ {
return slot_; return slot_;
} }
@ -61,9 +59,10 @@ struct Timer
std::uint32_t period_ms; std::uint32_t period_ms;
Type type; Type type;
private: private:
BaseScheduler* scheduler_; BaseScheduler* scheduler_;
Utility::IFunction<void (ArgStorage&)>* slot_ = nullptr; Utility::IFunction<void(ArgStorage&)>* slot_ = nullptr;
}; };
} }// namespace cpptick

View File

@ -14,4 +14,4 @@ void Timer::start()
scheduler_->storeTimer(this); scheduler_->storeTimer(this);
} }
} }// namespace cpptick

View File

@ -36,19 +36,18 @@ struct TestHal
return std::uint32_t( return std::uint32_t(
duration_cast<milliseconds>(system_clock::now().time_since_epoch()) duration_cast<milliseconds>(system_clock::now().time_since_epoch())
.count() .count());
);
} }
}; };
} }// namespace
TEST_CASE("Slot calls function properly with void args.", "[cpptick]") TEST_CASE("Slot calls function properly with void args.", "[cpptick]")
{ {
cpptick::Scheduler<TestHal> scheduler; cpptick::Scheduler<TestHal> scheduler;
cpptick::Slot<void ()> slot(&scheduler); cpptick::Slot<void()> slot(&scheduler);
auto* f = slot.connect(callbackByOne); auto* f = slot.connect(callbackByOne);
callback_count = 0; callback_count = 0;
@ -99,7 +98,7 @@ TEST_CASE("Slot calls function properly with args.", "[cpptick]")
{ {
cpptick::Scheduler<TestHal> scheduler; cpptick::Scheduler<TestHal> scheduler;
cpptick::Slot<void (int)> slot(&scheduler); cpptick::Slot<void(int)> slot(&scheduler);
auto* f = slot.connect(callbackByN); auto* f = slot.connect(callbackByN);
callback_count = 0; callback_count = 0;
@ -122,7 +121,7 @@ TEST_CASE("Timer will be invoked.", "[cpptick]")
{ {
cpptick::Scheduler<TestHal> scheduler; cpptick::Scheduler<TestHal> scheduler;
cpptick::Slot<void ()> slot(&scheduler); cpptick::Slot<void()> slot(&scheduler);
auto* f = slot.connect(callbackByOne); auto* f = slot.connect(callbackByOne);
callback_count = 0; callback_count = 0;
@ -179,7 +178,7 @@ TEST_CASE("Periodic timers will be invoked repeatedly.", "[cpptick]")
{ {
cpptick::Scheduler<TestHal> scheduler; cpptick::Scheduler<TestHal> scheduler;
cpptick::Slot<void ()> slot(&scheduler); cpptick::Slot<void()> slot(&scheduler);
auto* f = slot.connect(callbackByOne); auto* f = slot.connect(callbackByOne);
callback_count = 0; callback_count = 0;
@ -226,17 +225,16 @@ TEST_CASE("Sequential timers operate appropriately.", "[cpptick]")
{ {
cpptick::Scheduler<TestHal> scheduler; cpptick::Scheduler<TestHal> scheduler;
cpptick::Slot<void ()> slot_a(&scheduler); cpptick::Slot<void()> slot_a(&scheduler);
auto* f = slot_a.connect(callbackByOne); auto* f = slot_a.connect(callbackByOne);
cpptick::Timer timer_a(&scheduler, 100, cpptick::Timer::PERIODIC); cpptick::Timer timer_a(&scheduler, 100, cpptick::Timer::PERIODIC);
timer_a.setSlot(slot_a); timer_a.setSlot(slot_a);
auto callback_by_two = []() -> void auto callback_by_two = []() -> void {
{
callback_count += 2; callback_count += 2;
}; };
cpptick::Slot<void ()> slot_b(&scheduler); cpptick::Slot<void()> slot_b(&scheduler);
auto* ff = slot_b.connect(callback_by_two); auto* ff = slot_b.connect(callback_by_two);
cpptick::Timer timer_b(&scheduler, 50, cpptick::Timer::ONE_SHOT); cpptick::Timer timer_b(&scheduler, 50, cpptick::Timer::ONE_SHOT);