"st.h"

"st.h"

Cooltonbek

#pragma once
#ifndef ST_H_INCLUDING
#define ST_H_INCLUDING

// standard tools file ( st )

#include <algorithm>
#include <string>
#include <chrono>
#include <mutex>
#include <shared_mutex>
#include <atomic>
#include <thread>
#include <optional>

using namespace std::literals;

using sbyte = signed char;
using ubyte = unsigned char;
using ushort = unsigned short;
using uint = unsigned int;
using slong = signed long long;
using ulong = unsigned long long;
using str = std::string;

using opt_char = std::optional<char>;
using opt_sbyte = std::optional<sbyte>;
using opt_ubyte = std::optional<ubyte>;
using opt_short = std::optional<short>;
using opt_ushort = std::optional<ushort>;
using opt_int = std::optional<int>;
using opt_uint = std::optional<uint>;
using opt_slong = std::optional<slong>;
using opt_ulong = std::optional<ulong>;
using opt_str = std::optional<str>;

using vault = std::mutex;
using door = std::recursive_mutex;
using shared = std::shared_mutex;

#define null (std::nullopt)
#define is_null(val) ((val) == null)
#define no_null(val) ((val) != null)

#define to_char(val) (reinterpret_cast<char*>(val))
#define to_wchar(val) (reinterpret_cast<wchar_t*>(val))

namespace st
{

constexpr ubyte MAX_LONG_LEN = 19;
constexpr ubyte MAX_FLOAT_LEN = 30;

enum class result : ubyte
{ Failed = 0
, Success = 1
, Wait = 2
, Undefined = 3
, Exception = 0x7f
};

enum class action : ubyte
{ None = 0
, Ready = 1
, Should = 2
, Must = 3
, Waiting = 4
, Trying = 5
, Doing = 6
, Done = 7
, Failed = 8
};

using clock = std::chrono::time_point<std::chrono::system_clock>;

inline clock now(){ return std::chrono::system_clock::now(); }

inline double passed_sec( const clock &pdStart )
{ return std::chrono::duration<double>( now() - pdStart ).count(); }

template<typename T>
inline void sleep( const T &pdVal )
{ std::this_thread::sleep_for( pdVal ); }

// inevitable definitions for easy use
#define LOCK(plLockable) std::lock_guard<decltype(plLockable)> \
tlLockingIt(plLockable)
#define LOCKIT LOCK(*this) // inevitable definition for easy use

class room
{
  public:

   room() {}

   ~room()
   { while( nStackDepth ){ unlock(); } }

   void lock() const
  { lDoor.lock(); nStackDepth++; }

   bool try_lock() const
   { return lDoor.try_lock() ? ( ++nStackDepth ) : false; }

   void unlock() const
   { if( nStackDepth ){ lDoor.unlock(); nStackDepth--; } }

   // deleted methods
   room( const room &poSour ) = delete;
   virtual room &operator=( const room &poSour ) = delete;

  private:

   mutable std::atomic<ulong> nStackDepth{0};
   mutable door lDoor;

} // namespace st

#endif // ST_H_INCLUDING


Report Page