Commit feaeffb8 authored by aligungr's avatar aligungr

L3 RRC/NAS developments

parent 027d89d6
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <array> #include <array>
#include <functional> #include <functional>
#include <optional> #include <optional>
#include <type_traits>
#include <vector> #include <vector>
#include <utils/common.hpp> #include <utils/common.hpp>
...@@ -16,6 +17,8 @@ ...@@ -16,6 +17,8 @@
namespace nas namespace nas
{ {
// TODO: Periodun sonuna geldiğinde erişilmezse, (autoClearIfNecessary çağrılmazsa) delete yapılmaz backup çalışmaz
/* /*
* - Items are unique, if already exists, deletes the previous one * - Items are unique, if already exists, deletes the previous one
* - List have fixed size, if capacity is full, oldest item is deleted * - List have fixed size, if capacity is full, oldest item is deleted
...@@ -142,6 +145,9 @@ class NasList ...@@ -142,6 +145,9 @@ class NasList
private: private:
void autoClearIfNecessary() void autoClearIfNecessary()
{ {
if (m_autoClearingPeriod <= 0)
return;
int64_t currentTime = ::utils::CurrentTimeMillis(); int64_t currentTime = ::utils::CurrentTimeMillis();
if (currentTime - m_lastAutoCleared >= m_autoClearingPeriod) if (currentTime - m_lastAutoCleared >= m_autoClearingPeriod)
{ {
...@@ -158,11 +164,11 @@ class NasList ...@@ -158,11 +164,11 @@ class NasList
void removeAt(size_t index) void removeAt(size_t index)
{ {
touch();
for (size_t i = index; i < m_size; ++i) for (size_t i = index; i < m_size; ++i)
m_data[i] = i + 1 < m_sizeLimit ? m_data[i + 1] : T{}; m_data[i] = i + 1 < m_sizeLimit ? m_data[i + 1] : T{};
m_size--; m_size--;
touch();
} }
void touch() void touch()
...@@ -172,4 +178,87 @@ class NasList ...@@ -172,4 +178,87 @@ class NasList
} }
}; };
template <typename T>
class NasSlot
{
public:
using backup_functor_type = std::function<void(const T &value)>;
private:
const int64_t m_autoClearingPeriod;
const std::optional<backup_functor_type> m_backupFunctor;
T m_value;
int64_t m_lastAutoCleared;
static_assert(!std::is_reference<T>::value);
public:
NasSlot(int64_t autoClearingPeriod, std::optional<backup_functor_type> backupFunctor)
: m_autoClearingPeriod{autoClearingPeriod}, m_backupFunctor{backupFunctor}, m_value{},
m_lastAutoCleared{::utils::CurrentTimeMillis()}
{
}
T get()
{
autoClearIfNecessary();
return m_value;
}
void set(const T &value)
{
autoClearIfNecessary();
m_value = value;
touch();
}
void set(T &&value)
{
autoClearIfNecessary();
m_value = std::move(value);
touch();
}
template <typename Functor>
void access(Functor fun)
{
autoClearIfNecessary();
fun((const T &)m_value);
}
template <typename Functor>
void mutate(Functor fun)
{
autoClearIfNecessary();
fun((T &)m_value);
touch();
}
private:
void autoClearIfNecessary()
{
if (m_autoClearingPeriod <= 0)
return;
int64_t currentTime = ::utils::CurrentTimeMillis();
if (currentTime - m_lastAutoCleared >= m_autoClearingPeriod)
{
m_lastAutoCleared = currentTime;
m_value = {};
}
}
void touch()
{
if (m_backupFunctor)
(*m_backupFunctor)(m_value);
}
};
} // namespace nas } // namespace nas
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment