/************************************************************************* > File Name : CJudgeUtils.h > Author : FengChao > EMail : smile.fengchao@gmail.com > Created Time : Fri 17 Jul 2020 09:34:47 AM CST ************************************************************************/ #ifndef _JUDGE_COMMON_H_ #define _JUDGE_COMMON_H_ #include #include #include #include #include #include #include #include #include #include "CLog.h" class CNonCopyable { protected: CNonCopyable() {} ~CNonCopyable() {} private: CNonCopyable(const CNonCopyable &); const CNonCopyable& operator=(const CNonCopyable &); }; class CMutexLockGuard:CNonCopyable { public: explicit CMutexLockGuard(pthread_mutex_t& mutex) : m_mutex(mutex) { pthread_mutex_lock(&m_mutex); } ~CMutexLockGuard() { pthread_mutex_unlock(&m_mutex); } private: pthread_mutex_t& m_mutex; }; template class CSingleton { private: static std::once_flag SingletonFlag; static std::unique_ptr Instance; protected: CSingleton() = default; CSingleton(const T&) = delete; CSingleton& operator=(const T&) = delete; public: static T& GetInstance() { std::call_once(SingletonFlag, [=]() { Instance.reset(new T); }); return *(Instance.get()); } }; template std::unique_ptr CSingleton ::Instance = nullptr; template std::once_flag CSingleton ::SingletonFlag; class INIReader { public: explicit INIReader() {} ~INIReader() {} /// @brief 解析文件 /// @return 0 成功 /// @return <0 失败 /// @note 每次调用会清除上一次的解析结果 int32_t Parse(const std::string& filename); void Clear(); /// @brief 获取string类型字段的值,字段未配置时使用默认值 std::string Get(const std::string& section, const std::string& name, const std::string& default_value); // Get an integer (long) value from INI file, returning default_value // if not found or not a valid integer (decimal "1234", "-1234", // or hex "0x4d2"). int32_t GetInt32(const std::string& section, const std::string& name, int32_t default_value); uint32_t GetUInt32(const std::string& section, const std::string& name, uint32_t default_value); int64_t GetInt64(const std::string& section, const std::string& name, int64_t default_value); uint64_t GetUInt64(const std::string& section, const std::string& name, uint64_t default_value); // Get a real (floating point double) value from INI file, returning // default_value if not found or not a valid floating point value // according to strtod(). double GetReal(const std::string& section, const std::string& name, double default_value); // Get a boolean value from INI file, returning default_value // if not found or if not a valid true/false value. Valid true // values are "true", "yes", "on", "1", and valid false values are // "false", "no", "off", "0" (not case sensitive). bool GetBoolean(const std::string& section, const std::string& name, bool default_value); // Returns all the section names from the INI file, in alphabetical order, // but in the original casing const std::set& GetSections() const; // Returns all the field names from a section in the INI file, in // alphabetical order, but in the original casing. Returns an // empty set if the field name is unknown std::set GetFields(const std::string& section); /// @brief 返回最后错误字符串描述 /// @return 最后一次错误的信息 const char* GetLastError() const { return m_last_error; } private: int32_t ParseFile(FILE* file); private: char m_last_error[256]; std::set m_sections; std::map > m_fields; }; #endif