parent
							
								
									db00552d84
								
							
						
					
					
						commit
						74846d6deb
					
				
				 15 changed files with 74 additions and 186 deletions
			
			
		@ -1,14 +1,31 @@ | 
				
			||||
#pragma once | 
				
			||||
 | 
				
			||||
#include <cstdint> | 
				
			||||
#include <memory> | 
				
			||||
 | 
				
			||||
namespace Bk { | 
				
			||||
    using u1 = bool; | 
				
			||||
    using u8 = unsigned char; | 
				
			||||
    using u16 = unsigned short; | 
				
			||||
    using u32 = unsigned long; | 
				
			||||
    using u64 = unsigned long long; 
 | 
				
			||||
    using i1 = bool; | 
				
			||||
    using i8 = signed char; | 
				
			||||
    using i16 = signed short; | 
				
			||||
    using i32 = signed long; | 
				
			||||
    using i64 = signed long long; | 
				
			||||
    using u8 = uint8_t; | 
				
			||||
    using u16 = uint16_t; | 
				
			||||
    using u32 = uint32_t; | 
				
			||||
    using u64 = uint64_t; 
 | 
				
			||||
    using i8 = int8_t; | 
				
			||||
    using i16 = int16_t; | 
				
			||||
    using i32 = int32_t; | 
				
			||||
    using i64 = int64_t; | 
				
			||||
 | 
				
			||||
    template<typename T> | 
				
			||||
    using Scope = std::unique_ptr<T>; | 
				
			||||
    template<typename T, typename ... Args> | 
				
			||||
    constexpr Scope<T> CreateScope(Args&& ... args) | 
				
			||||
    { | 
				
			||||
        return std::make_unique<T>(std::forward<Args>(args)...); | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    template<typename T> | 
				
			||||
    using Ref = std::shared_ptr<T>; | 
				
			||||
    template<typename T, typename ... Args> | 
				
			||||
    constexpr Ref<T> CreateRef(Args&& ... args) | 
				
			||||
    { | 
				
			||||
        return std::make_shared<T>(std::forward<Args>(args)...); | 
				
			||||
    } | 
				
			||||
} | 
				
			||||
@ -1,30 +1,44 @@ | 
				
			||||
#pragma once | 
				
			||||
 | 
				
			||||
#include <bakatoolspch.h> | 
				
			||||
#include <bakatools/time/time_span.h> | 
				
			||||
#include <bakatools/time/time_point.h> | 
				
			||||
#include <thread> | 
				
			||||
#include <functional> | 
				
			||||
#include <chrono> | 
				
			||||
#include <ratio> | 
				
			||||
 | 
				
			||||
namespace Bk { | 
				
			||||
    template<typename P> | 
				
			||||
    class TaskDelayer 
 | 
				
			||||
    { | 
				
			||||
        public: | 
				
			||||
            TaskDelayer(int n_ts, 
 | 
				
			||||
            std::unique_ptr<std::function<void()>> action | 
				
			||||
            ): ts(n_ts) 
 | 
				
			||||
            TaskDelayer(int ts, bool repeat = false) | 
				
			||||
            : ts(ts), repeat(repeat) {} | 
				
			||||
            ~TaskDelayer() { stop(); } | 
				
			||||
 | 
				
			||||
            void start(std::unique_ptr<std::function<void()>> action) | 
				
			||||
            { | 
				
			||||
                if(running) stop(); | 
				
			||||
                running = true; | 
				
			||||
                worker = std::thread([this](std::unique_ptr<std::function<void()>> action) | 
				
			||||
                { | 
				
			||||
                    std::function<void()>& task = *action; | 
				
			||||
                    std::this_thread::sleep_for(ts.interval); | 
				
			||||
                    task(); | 
				
			||||
                    while(running && repeat) | 
				
			||||
                    { | 
				
			||||
                        std::this_thread::sleep_for(ts); | 
				
			||||
                        task(); | 
				
			||||
                    } | 
				
			||||
                }, std::move(action)); 
 | 
				
			||||
            } | 
				
			||||
 | 
				
			||||
            ~TaskDelayer() { worker.join(); } | 
				
			||||
            void stop() | 
				
			||||
            { | 
				
			||||
                if (!running) return; | 
				
			||||
                running = false; | 
				
			||||
                worker.join(); | 
				
			||||
            } | 
				
			||||
 | 
				
			||||
        private: | 
				
			||||
            bool running = false; | 
				
			||||
            bool repeat; | 
				
			||||
            std::thread worker; | 
				
			||||
            TimeSpan<P> ts; | 
				
			||||
            std::chrono::duration<int, std::milli> ts; | 
				
			||||
    }; | 
				
			||||
} | 
				
			||||
@ -1,43 +0,0 @@ | 
				
			||||
#pragma once | 
				
			||||
 | 
				
			||||
#include <bakatoolspch.h> | 
				
			||||
#include <bakatools/time/time_span.h> | 
				
			||||
#include <bakatools/time/time_point.h> | 
				
			||||
 | 
				
			||||
namespace Bk { | 
				
			||||
    template<typename P> | 
				
			||||
    class TaskTimer 
 | 
				
			||||
    { | 
				
			||||
        public: | 
				
			||||
            TaskTimer(TimeSpan<P> ts) | 
				
			||||
            : ts(ts) {} | 
				
			||||
            ~TaskTimer() { stop(); } | 
				
			||||
 | 
				
			||||
            void start(std::unique_ptr<std::function<void()>> action) | 
				
			||||
            { | 
				
			||||
                if(running) stop(); | 
				
			||||
                running = true; | 
				
			||||
                worker = std::thread([this](std::unique_ptr<std::function<void()>> action) | 
				
			||||
                { | 
				
			||||
                    std::function<void()>& task = *action; | 
				
			||||
                    while(running) | 
				
			||||
                    { | 
				
			||||
                        std::this_thread::sleep_for(ts.interval); | 
				
			||||
                        task(); | 
				
			||||
                    } | 
				
			||||
                }, std::move(action)); 
 | 
				
			||||
            } | 
				
			||||
 | 
				
			||||
            void stop() | 
				
			||||
            { | 
				
			||||
                if (!running) return; | 
				
			||||
                running = false; | 
				
			||||
                worker.join(); | 
				
			||||
            } | 
				
			||||
 | 
				
			||||
        private: | 
				
			||||
            bool running = false; | 
				
			||||
            std::thread worker; | 
				
			||||
            TimeSpan<P> ts; | 
				
			||||
    }; | 
				
			||||
} | 
				
			||||
@ -1,56 +0,0 @@ | 
				
			||||
#pragma once | 
				
			||||
 | 
				
			||||
#include <bakatoolspch.h> | 
				
			||||
#include "time_span.h" | 
				
			||||
 | 
				
			||||
namespace Bk 
 | 
				
			||||
{ | 
				
			||||
    using SysClock = std::chrono::system_clock; | 
				
			||||
    using SteadyClock = std::chrono::steady_clock; | 
				
			||||
    using FileClock = std::chrono::file_clock; | 
				
			||||
    using UtcClock = std::chrono::utc_clock; | 
				
			||||
    
 | 
				
			||||
    template<typename C, typename P> | 
				
			||||
    struct TimePoint | 
				
			||||
    { | 
				
			||||
        TimePoint() = default; | 
				
			||||
        TimePoint(std::chrono::time_point<C> point) | 
				
			||||
        { | 
				
			||||
            this->point = std::chrono::time_point_cast<std::chrono::duration<long int, P>>(point); | 
				
			||||
        } | 
				
			||||
 | 
				
			||||
        template<typename Pi> | 
				
			||||
        TimePoint<C, Pi> as_unit() | 
				
			||||
        { | 
				
			||||
            return TimePoint<C, Pi>(std::chrono::time_point_cast<std::chrono::duration<long int, Pi>>(point)); | 
				
			||||
        } | 
				
			||||
 | 
				
			||||
        template<typename Ci> | 
				
			||||
        TimePoint<Ci, P> as_clock() | 
				
			||||
        { | 
				
			||||
            return TimePoint(std::chrono::clock_cast<Ci>(point)); | 
				
			||||
        } | 
				
			||||
 | 
				
			||||
        TimeSpan<P> elasped() | 
				
			||||
        { | 
				
			||||
            return TimeSpan<P>(point.time_since_epoch()); | 
				
			||||
        } | 
				
			||||
 | 
				
			||||
        bool is_steady() | 
				
			||||
        { | 
				
			||||
            return C::is_steady; | 
				
			||||
        } | 
				
			||||
 | 
				
			||||
        TimeSpan<P> operator-(TimePoint<C, P>& tp) | 
				
			||||
        { | 
				
			||||
            return TimeSpan<P>(point - tp.point); | 
				
			||||
        } | 
				
			||||
 | 
				
			||||
        static TimePoint<C, P> now() 
 | 
				
			||||
        { | 
				
			||||
            return TimePoint<C, P>(C::now()); | 
				
			||||
        } | 
				
			||||
 | 
				
			||||
        std::chrono::time_point<C, std::chrono::duration<long int, P>> point; | 
				
			||||
    }; | 
				
			||||
} | 
				
			||||
@ -1,38 +0,0 @@ | 
				
			||||
#pragma once | 
				
			||||
 | 
				
			||||
#include <bakatoolspch.h> | 
				
			||||
 | 
				
			||||
namespace Bk 
 | 
				
			||||
{ | 
				
			||||
    using Nanosecond = std::ratio<1,1000000000>; | 
				
			||||
    using Microsecond = std::ratio<1,1000000>; | 
				
			||||
    using Millisecond = std::ratio<1,1000>; | 
				
			||||
    using Second = std::ratio<1,1>; | 
				
			||||
    using Minute = std::ratio<60, 1>; | 
				
			||||
    using Hour = std::ratio<3600, 1>; | 
				
			||||
    using Day = std::ratio<84600, 1>; | 
				
			||||
 | 
				
			||||
    template<typename P> | 
				
			||||
    struct TimeSpan 
 | 
				
			||||
    { | 
				
			||||
        TimeSpan(int interval = 0) 
 | 
				
			||||
        : interval(std::chrono::duration<long int, P>(interval)) {} | 
				
			||||
        TimeSpan(std::chrono::duration<long int, P> interval) | 
				
			||||
        : interval(interval) {} | 
				
			||||
 | 
				
			||||
        template<typename T> | 
				
			||||
        TimeSpan<T> as_unit() | 
				
			||||
        { | 
				
			||||
            return TimeSpan<T>(std::chrono::duration_cast<std::chrono::duration<long int, T>>(interval)); | 
				
			||||
        } | 
				
			||||
 | 
				
			||||
        int count() { return interval.count(); } | 
				
			||||
 | 
				
			||||
        TimeSpan<P> operator-(TimeSpan<P>& time_span) | 
				
			||||
        { | 
				
			||||
            return TimeSpan<P>(interval - time_span.interval); | 
				
			||||
        } | 
				
			||||
 | 
				
			||||
        std::chrono::duration<long int, P> interval; | 
				
			||||
    }; | 
				
			||||
} | 
				
			||||
					Loading…
					
					
				
		Reference in New Issue