PracticeDev/study_clang/Mimic/test_judge/include/CJudgeUtils.h

141 lines
4.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*************************************************************************
> 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 <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <string>
#include <stdint.h>
#include <pthread.h>
#include <mutex>
#include <memory>
#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 T>
class CSingleton
{
private:
static std::once_flag SingletonFlag;
static std::unique_ptr<T> 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 <class T> std::unique_ptr<T> CSingleton <T>::Instance = nullptr;
template <class T> std::once_flag CSingleton <T>::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<std::string>& 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<std::string> 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<std::string> m_sections;
std::map<std::string, std::map<std::string, std::string> > m_fields;
};
#endif