XNSim/XNCore/XNCore_global.h

292 lines
7.9 KiB
C++
Executable File

#pragma once
#if defined(XNCORE_LIBRARY)
# define XNCORE_EXPORT __attribute__((visibility("default")))
#else
# define XNCORE_EXPORT __attribute__((visibility("default")))
#endif
#define FORCEINLINE __attribute__((always_inline))
#ifdef __linux__
# include <pthread.h>
# include <sched.h>
# include <sys/timeb.h>
# include <sys/mman.h>
# include <sys/stat.h>
# include <errno.h>
# include <time.h>
# include <memory>
# include <errno.h>
# include <unistd.h>
# include <limits.h>
# include <functional>
# include <map>
# include <unordered_map>
# include <vector>
# include <set>
# include <any>
# include <string>
# include <thread>
# include <mutex>
# include <condition_variable>
# include <queue>
# include <tinyxml2.h>
# include <chrono>
# include <iomanip>
# include <sstream>
# include <dlfcn.h>
# include <filesystem>
# include <array>
# include <cstring>
# include <iostream>
# include <sqlite3.h>
# include <nlohmann/json.hpp>
#endif
#include <fastdds/dds/domain/DomainParticipant.hpp>
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/publisher/DataWriter.hpp>
#include <fastdds/dds/publisher/Publisher.hpp>
#include <fastdds/dds/subscriber/DataReader.hpp>
#include <fastdds/dds/subscriber/Subscriber.hpp>
#include <fastdds/dds/topic/TypeSupport.hpp>
#include <fastdds/dds/subscriber/DataReaderListener.hpp>
#include <fastcdr/xcdr/optional.hpp>
#define FAST_DDS_MACRO eprosima::fastdds::dds
using XN_JSON = nlohmann::json;
/**
* @brief 默认基频定义,单位 Hz
*/
#define BASE_RUN_FREQ 100.0
/**
* @brief 默认基础运行间隔定义 单位纳秒
*/
#define BASE_RUN_INTER (1.0E9 / BASE_RUN_FREQ)
//模型周期性回调函数类型别名
using XNCallBack = std::function<void()>;
//DDS回调函数类型别名
using XNDDSCallBack = std::function<void(void *)>;
//事件回调函数类型别名
using XNEventCallback = std::function<void(const std::any &)>;
/**
* @brief 纳秒睡眠相关时间结构体
* @details 用于线程睡眠时间控制
*/
struct PERIOD_INFO {
/**
* @brief 系统提供的时间记录结构体,精确到纳秒
*/
timespec next_period;
/**
* @brief 睡眠时长,单位纳秒
*/
long period_ns;
};
/**
* @brief 系统时间点类型别名
*/
using XNTimePoint = std::chrono::system_clock::time_point;
/**
* @brief 系统运行状态枚举类
*/
enum class RunStatus {
/**
* @brief 未开始
*/
NotStart = 0,
/**
* @brief 运行中
*/
Runing,
/**
* @brief 暂停
*/
Suspend,
/**
* @brief 中止
*/
Aborted,
/**
* @brief 结束
*/
Finished
};
/**
* @brief 仿真控制命令枚举类
*/
enum class SimControlCmd {
/**
* @brief 开始
*/
Start,
/**
* @brief 继续
*/
Continue,
/**
* @brief 暂停
*/
Suspend,
/**
* @brief 中止
*/
Abort
};
/**
* @brief 运行频率族枚举类
*/
enum class FreqLevel {
/**
* @brief 基础频率
*/
BaseFreq = 0, // 120/100/60 Hz
/**
* @brief 半频
*/
HalfFreq, // 60/50/30 Hz
/**
* @brief 四分之一频
*/
QuarterFreq, // 30/25/15 Hz
/**
* @brief 八分之一频
*/
EighthFreq, // 15/12.5/7.5 Hz
/**
* @brief 十六分之一频
*/
SixteenthFreq, // 7.5/6.25/3.75 Hz
/**
* @brief 三十二分之一频
*/
ThirtyTwothFreq, // 3.75/3.125/1.875 Hz
};
/**
* @brief 框架对象状态枚举
*/
enum class XNFrameObjectStatus {
/**
* @brief 未初始化
*/
NotReady = 0,
/**
* @brief 已初始化
*/
Initialized,
/**
* @brief 已准备好
*/
Ready,
/**
* @brief 未知
*/
Unknown
};
template <class ToType, class FromType>
FORCEINLINE ToType XNStaticCastHelper(const FromType &from)
{
return std::static_pointer_cast<typename ToType::element_type>(from);
}
template <class ToType, class FromType>
FORCEINLINE ToType XNCastHelper(const FromType &from)
{
return std::dynamic_pointer_cast<typename ToType::element_type>(from);
}
#define XN_CAST(from, to) XNStaticCastHelper<to>(from)
#define XN_THISPTR std::static_pointer_cast<ThisType>(shared_from_this())
#define XNCLASS_PTR_DECLARE(a) \
using a##Ptr = std::shared_ptr<class a>; \
using a##WPtr = std::weak_ptr<class a>; \
using a##UPtr = std::unique_ptr<class a>; \
using a##ConsPtr = std::shared_ptr<const class a>;
#define XNSTRUCT_PTR_DECLARE(a) \
using a##Ptr = std::shared_ptr<struct a>; \
using a##WPtr = std::weak_ptr<struct a>; \
using a##UPtr = std::unique_ptr<struct a>; \
using a##ConsPtr = std::shared_ptr<const struct a>;
#define XN_NOCOPYABLE(Class) \
public: \
using NoCopyable = Class; \
\
private: \
Class(const Class &) = delete; \
Class &operator=(const Class &) = delete;
#define XN_DECLARE_PRIVATE(Class) \
XN_NOCOPYABLE(Class) \
protected: \
friend class Class##Private; \
inline Class##Private *GetPP() const \
{ \
return reinterpret_cast<Class##Private *>(_Private_Ptr); \
} \
using PrivateType = Class##Private;
#define T_D() PrivateType *const d = GetPP()
struct XNNullClass {
};
#define XN_METATYPE_P(cls) \
public: \
using ThisType = cls; \
using SuperType = XNNullClass;
#define XN_METATYPE(cls, sup) \
public: \
using ThisType = cls; \
using SuperType = sup;
#define XN_UNUSED(x) (void)x
namespace XNSim
{
template <typename T>
constexpr typename std::underlying_type<T>::type enumValue(T e)
{
return static_cast<typename std::underlying_type<T>::type>(e);
}
/**
* @brief 将ISO格式的时间字符串转换为系统时间点
* @param timeStr ISO格式的时间字符串 (YYYY-MM-DDTHH:mm:ss)
* @return 系统时间点
*/
extern "C" XNTimePoint XNCORE_EXPORT parseISOTime(const std::string &timeStr);
extern "C" std::vector<std::string> XNCORE_EXPORT split(const std::string &str,
const std::string &delim);
extern "C" std::string XNCORE_EXPORT getFileNameWithoutExt(const std::string &path);
extern "C" int XNCORE_EXPORT safe_stoi(const std::string &str, int defaultValue = 0);
inline std::string getStringFromSqlite3(sqlite3_stmt *stmt, int column)
{
const char *text = reinterpret_cast<const char *>(sqlite3_column_text(stmt, column));
if (text == nullptr) {
return "";
}
return std::string(text);
}
} // namespace XNSim