#ifndef PROTECTOR_UTILITY_H #define PROTECTOR_UTILITY_H #include #include #include #include #include #include #include #include #include #include #include class FatalError : public std::exception { public: FatalError(const char *__message, int __code) : message(__message), code(__code) {} const char *what() const noexcept override { return message.c_str(); } const int GetCode() const noexcept { return code; } private: std::string message; int code; }; class BlockingStringStream { public: BlockingStringStream() {} // Declaration of major std::stringstream interfaces template BlockingStringStream &operator<<(const T &val); template BlockingStringStream &operator>>(T &val); BlockingStringStream &getline(std::string &str, char delim = '\n'); std::stringstream internalStream; void readlock(); void unreadlock(); std::atomic is_writing = false; private: std::mutex mutex; std::mutex custom_mutex; std::condition_variable condition; }; // Implementation of operator<< template BlockingStringStream &BlockingStringStream::operator<<(const T &val) { { std::lock_guard lock(mutex); if (internalStream.peek() == EOF) { internalStream.clear(); internalStream.str(""); } internalStream << val; } condition.notify_all(); return *this; } // Implementation of operator>> template BlockingStringStream &BlockingStringStream::operator>>(T &val) { std::unique_lock lock(mutex); // Wait until data is available condition.wait(lock, [this] { return internalStream.peek() != EOF && !is_writing; }); internalStream >> val; return *this; } class ReadWriteLock { private: std::mutex mtx; std::condition_variable cv; std::atomic readers; std::atomic is_writing; public: ReadWriteLock(); void startRead(); void endRead(); void startWrite(); void endWrite(); }; class SessionClass { public: std::stack login_stack; std::string SessionToken; std::string OuthorizationKey; }; namespace BookStore_ZYM { extern std::mutex debug_Print_Mutex; } void debugPrint(); template void debugPrint(Args... args) { BookStore_ZYM::debug_Print_Mutex.lock(); ((std::cerr << args), ...); std::cerr << std::endl; BookStore_ZYM::debug_Print_Mutex.unlock(); } #endif // PROTECTOR_UTILITY_H