189 lines
3.8 KiB
C++
189 lines
3.8 KiB
C++
// License: Modified MIT (NON-AI)
|
|
// See the LICENSE file in the root directory for license information.
|
|
// Copyright 2025 Timothy Prepscius
|
|
|
|
#include <tjp/core/log/Log.h>
|
|
#include <tjp/core/containers/Vector.hpp>
|
|
#include <tjp/core/containers/Map.hpp>
|
|
#include <tjp/core/algorithm/map_value.hpp>
|
|
#include <tjp/core/algorithm/map_value_erase.hpp>
|
|
#include <tjp/core/iterators/range.hpp>
|
|
#include <tjp/core/threads/Lock.hpp>
|
|
#include <tjp/core/time/Time+chrono.hpp>
|
|
|
|
namespace my_base_namespace {
|
|
namespace ext = tjp::core;
|
|
using namespace ext;
|
|
}
|
|
|
|
// or
|
|
|
|
namespace my_base_namespace {
|
|
namespace ext = tjp::core;
|
|
|
|
using ext::Mutex;
|
|
}
|
|
|
|
namespace my_base_namespace::sub_name_space {
|
|
|
|
void do_something() {}
|
|
|
|
template<typename T>
|
|
void do_something_with(T &&) {}
|
|
|
|
// ----------------------------------------
|
|
// example for locks
|
|
// ----------------------------------------
|
|
void example_lock_tjp()
|
|
{
|
|
Mutex mutex;
|
|
|
|
auto lock = lock_of(mutex);
|
|
do_something();
|
|
|
|
auto should_send_signal = true;
|
|
if (should_send_signal)
|
|
{
|
|
auto unlock = unlock_of(mutex);
|
|
do_something();
|
|
}
|
|
}
|
|
|
|
// using std
|
|
void example_lock_cpp()
|
|
{
|
|
std::mutex mutex;
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
do_something();
|
|
|
|
auto should_send_signal = true;
|
|
if (should_send_signal)
|
|
{
|
|
try
|
|
{
|
|
mutex.unlock();
|
|
do_something();
|
|
mutex.lock();
|
|
}
|
|
catch (...)
|
|
{
|
|
mutex.lock();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
// example for maps
|
|
// ----------------------------------------
|
|
void example_map_tjp()
|
|
{
|
|
Map<int, int> map;
|
|
if (auto value = map_value(map, 42))
|
|
do_something_with(*value);
|
|
|
|
if (auto value = map_value_erase(map, 42))
|
|
do_something_with(*value);
|
|
}
|
|
|
|
|
|
// using std
|
|
void example_map_cpp()
|
|
{
|
|
std::map<int, int> map;
|
|
auto i = map.find(42);
|
|
if (i != map.end())
|
|
{
|
|
do_something_with(*i);
|
|
}
|
|
|
|
auto j = map.find(42);
|
|
if (j != map.end())
|
|
{
|
|
auto v = std::move(j->second);
|
|
map.erase(j);
|
|
|
|
do_something_with(v);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
// example for range
|
|
// ----------------------------------------
|
|
|
|
void example_range_cpp()
|
|
{
|
|
for (auto k=0; k<42; ++k)
|
|
do_something_with(k);
|
|
}
|
|
|
|
void example_range_tjp()
|
|
{
|
|
for (auto i: range(42))
|
|
do_something_with(i);
|
|
}
|
|
|
|
// ----------------------------------------
|
|
// time
|
|
// ----------------------------------------
|
|
|
|
void example_time_duration_cpp()
|
|
{
|
|
auto t1 = std::chrono::system_clock::now();
|
|
do_something();
|
|
auto t2 = std::chrono::system_clock::now();
|
|
|
|
auto duration = t2 - t1;
|
|
auto duration_serializable_number = std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
|
|
do_something_with(duration_serializable_number);
|
|
|
|
auto duration_in_seconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
|
|
do_something_with(duration_in_seconds);
|
|
|
|
auto two_second_after_t1 = t1 + std::chrono::seconds(2);
|
|
do_something_with(two_second_after_t1);
|
|
|
|
auto meters = 32.;
|
|
auto seconds = std::chrono::seconds(2);
|
|
auto meters_per_second = meters / std::chrono::duration<float>(seconds).count();
|
|
do_something_with(meters_per_second);
|
|
|
|
meters = std::chrono::duration<float>(seconds).count() * meters_per_second;
|
|
do_something_with(meters);
|
|
}
|
|
|
|
void example_time_duration_tjp()
|
|
{
|
|
auto t1 = time::now();
|
|
do_something();
|
|
auto t2 = time::now();
|
|
|
|
auto duration = t2 - t1;
|
|
auto duration_serializable_number = duration.v;
|
|
do_something_with(duration_serializable_number);
|
|
|
|
auto duration_in_seconds = duration / time::Seconds;
|
|
do_something_with(duration_in_seconds);
|
|
|
|
auto two_second_after_t1 = t1 + time::Seconds(2);
|
|
auto two_second_after_t1_another = t1 + 2 * time::Seconds;
|
|
do_something_with(two_second_after_t1);
|
|
do_something_with(two_second_after_t1_another);
|
|
|
|
auto meters = 32.;
|
|
auto seconds = 2 * time::Seconds;
|
|
auto meters_per_second = meters / seconds;
|
|
do_something_with(meters_per_second);
|
|
|
|
meters = seconds * meters_per_second;
|
|
do_something_with(meters);
|
|
}
|
|
|
|
} // namespace
|