239 lines
4.5 KiB
C
239 lines
4.5 KiB
C
![]() |
#ifndef MSG_H
|
||
|
#define MSG_H
|
||
|
|
||
|
#include <opencv2/opencv.hpp>
|
||
|
|
||
|
#define MAX_STRING_LENGTH 128
|
||
|
//最多64个参数
|
||
|
#define MAX_PARAMS_LENGTH 64
|
||
|
|
||
|
// msg1
|
||
|
enum ParmaType
|
||
|
{
|
||
|
PARMATYPE_INPUT_IMAGE = 0,
|
||
|
PARMATYPE_OPTION,
|
||
|
PARMATYPE_OUTPUT_DATA,
|
||
|
PARMATYPE_TEMP_DATA,
|
||
|
};
|
||
|
|
||
|
enum DataType
|
||
|
{
|
||
|
DATATYPE_NONE = 0,
|
||
|
DATATYPE_INT,
|
||
|
DATATYPE_INT_ARRAY,
|
||
|
DATATYPE_CHAR,
|
||
|
DATATYPE_CHAR_ARRAY,
|
||
|
DATATYPE_FLOAT,
|
||
|
DATATYPE_FLOAT_ARRAY,
|
||
|
DATATYPE_BOOL,
|
||
|
DATATYPE_BOOL_ARRAY,
|
||
|
DATATYPE_ENUM,
|
||
|
};
|
||
|
|
||
|
struct Param
|
||
|
{
|
||
|
public:
|
||
|
char name[32] = "";
|
||
|
//void* data = 0;
|
||
|
long data_len = 0;
|
||
|
int width = 0;
|
||
|
int height = 0;
|
||
|
int flag = 0;
|
||
|
int run_times = 0;
|
||
|
bool need_delete = false;
|
||
|
long used_time = 0;
|
||
|
void (*delete_msg_func_ptr)(Param*) = 0;
|
||
|
std::shared_ptr<char> spData;
|
||
|
ParmaType param_type = PARMATYPE_OPTION;
|
||
|
DataType data_type = DATATYPE_NONE;
|
||
|
};
|
||
|
// msg1
|
||
|
|
||
|
|
||
|
template<class T>
|
||
|
struct LibapiDeleter
|
||
|
{
|
||
|
inline void operator()(void* pData)
|
||
|
{
|
||
|
delete static_cast<T*>(pData);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
template<class T>
|
||
|
struct LibapiDeleter<T[]>
|
||
|
{
|
||
|
inline void operator()(void* pData)
|
||
|
{
|
||
|
delete[] static_cast<T*>(pData);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
struct LibapiMsg
|
||
|
{
|
||
|
// msg1
|
||
|
public:
|
||
|
char creator_type[32] = "";
|
||
|
int creator_id = 0;
|
||
|
char version[32] = "";
|
||
|
long used_time = 0;
|
||
|
int cmd_id = 0;
|
||
|
Param params[MAX_PARAMS_LENGTH];
|
||
|
|
||
|
public:
|
||
|
|
||
|
virtual void set_used_time(long time)
|
||
|
{
|
||
|
this->used_time = time;
|
||
|
}
|
||
|
|
||
|
virtual void delete_msg()
|
||
|
{
|
||
|
LibapiMsg::delete_msg(this);
|
||
|
}
|
||
|
|
||
|
static void delete_msg(LibapiMsg* pMsg)
|
||
|
{
|
||
|
if (pMsg == NULL)
|
||
|
{
|
||
|
std::cout << "pMsg is null" << std::endl;
|
||
|
assert(0);
|
||
|
}
|
||
|
|
||
|
LibapiMsg* _pMsg = (LibapiMsg*)pMsg;
|
||
|
for (int i = 0; i < MAX_PARAMS_LENGTH; i++)
|
||
|
{
|
||
|
Param* _param = &_pMsg->params[i];
|
||
|
if (_param->need_delete)
|
||
|
{
|
||
|
_param->need_delete = false;
|
||
|
_param->spData = nullptr;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
delete pMsg;
|
||
|
pMsg = NULL;
|
||
|
}
|
||
|
|
||
|
static int copy_image_to_msg(void* pMsg, int pos, int param_type,
|
||
|
void* image_data, int width, int height, int flag)
|
||
|
{
|
||
|
if (pMsg == NULL)
|
||
|
{
|
||
|
std::cout << "pMsg == NULL" << std::endl;
|
||
|
assert(0);
|
||
|
}
|
||
|
|
||
|
if (pos > MAX_PARAMS_LENGTH)
|
||
|
{
|
||
|
std::cout << "pos > MAX_PARAMS_LENGTH" << std::endl;
|
||
|
assert(0);
|
||
|
}
|
||
|
|
||
|
LibapiMsg* _pMsg = (LibapiMsg*)pMsg;
|
||
|
Param* _param = &_pMsg->params[pos];
|
||
|
int image_data_len = width * height * flag;
|
||
|
|
||
|
if (_param->spData != NULL)
|
||
|
{
|
||
|
std::cout << "_param->spData != NULL" << std::endl;
|
||
|
assert(0);
|
||
|
|
||
|
}
|
||
|
|
||
|
char* pCharData = new char[image_data_len];
|
||
|
std::shared_ptr<char> spCharData(reinterpret_cast<char*>(pCharData), LibapiDeleter<char[]>());
|
||
|
memcpy(pCharData, image_data, image_data_len);
|
||
|
memcpy(_param->name, "IMAGE", sizeof("IMAGE"));
|
||
|
_param->spData = spCharData;
|
||
|
_param->width = width;
|
||
|
_param->height = height;
|
||
|
_param->flag = flag;
|
||
|
_param->need_delete = true;
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static cv::Mat get_image_from_msg(void* pMsg, int pos)
|
||
|
{
|
||
|
LibapiMsg* _pMsg = (LibapiMsg*)pMsg;
|
||
|
|
||
|
return cv::Mat(
|
||
|
_pMsg->params[pos].height,
|
||
|
_pMsg->params[pos].width,
|
||
|
CV_8UC3,
|
||
|
_pMsg->params[pos].spData.get());
|
||
|
}
|
||
|
|
||
|
template<class T>
|
||
|
static int set_spointer_to_msg(LibapiMsg* pMsg, int pos, void* p)
|
||
|
{
|
||
|
if (p != nullptr)
|
||
|
{
|
||
|
std::shared_ptr<char> spMatData(reinterpret_cast<char*>(p), LibapiDeleter<T>());
|
||
|
pMsg->params[pos].spData = spMatData;
|
||
|
pMsg->params[pos].need_delete = true;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
pMsg->params[pos].spData = nullptr;
|
||
|
pMsg->params[pos].need_delete = false;
|
||
|
}
|
||
|
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
template<class T>
|
||
|
static T* get_spointer_from_msg(LibapiMsg* pMsg, int pos)
|
||
|
{
|
||
|
if (pMsg->params[pos].spData == nullptr)
|
||
|
return NULL;
|
||
|
|
||
|
return (T*)pMsg->params[pos].spData.get();
|
||
|
}
|
||
|
|
||
|
// msg1
|
||
|
public:
|
||
|
//virtual void set_used_time(long time) = 0;
|
||
|
//virtual void delete_msg() = 0;
|
||
|
int add_ref()
|
||
|
{
|
||
|
std::unique_lock<std::mutex> lock(m_ref_mutex);
|
||
|
this->mRef++;
|
||
|
return mRef;
|
||
|
};
|
||
|
|
||
|
int sub_ref()
|
||
|
{
|
||
|
std::unique_lock<std::mutex> lock(m_ref_mutex);
|
||
|
this->mRef--;
|
||
|
return this->mRef;
|
||
|
};
|
||
|
|
||
|
int get_ref()
|
||
|
{
|
||
|
std::unique_lock<std::mutex> lock(m_ref_mutex);
|
||
|
return this->mRef;
|
||
|
};
|
||
|
|
||
|
bool select_one_process(bool need)
|
||
|
{
|
||
|
std::unique_lock<std::mutex> lock(m_ref_mutex_process);
|
||
|
this->mNeedOneProcess = need;
|
||
|
return need;
|
||
|
}
|
||
|
|
||
|
bool is_one_process(bool need = true)
|
||
|
{
|
||
|
std::unique_lock<std::mutex> lock(m_ref_mutex_process);
|
||
|
return this->mNeedOneProcess;
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
int mRef = 0;
|
||
|
std::mutex m_ref_mutex;
|
||
|
std::mutex m_ref_mutex_process;
|
||
|
bool mNeedOneProcess = false;
|
||
|
};
|
||
|
|
||
|
#endif
|