2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2020-01-21 18:16:11 +08:00
|
|
|
* Copyright (C) 2017-2020 Intel Corporation
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
2018-09-18 15:11:08 +08:00
|
|
|
* SPDX-License-Identifier: MIT
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
2020-02-13 21:03:54 +08:00
|
|
|
#include "get_info_status.h"
|
2019-02-27 18:39:32 +08:00
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
// Need for linux compatibility with memcpy_s
|
2020-02-24 01:46:50 +08:00
|
|
|
#include "helpers/string.h"
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2020-02-13 21:03:54 +08:00
|
|
|
inline GetInfoStatus getInfo(void *destParamValue, size_t destParamValueSize,
|
|
|
|
const void *srcParamValue, size_t srcParamValueSize) {
|
|
|
|
auto retVal = GetInfoStatus::INVALID_VALUE;
|
2017-12-21 07:45:38 +08:00
|
|
|
if (srcParamValue && srcParamValueSize) {
|
|
|
|
if (!destParamValue && !destParamValueSize) {
|
|
|
|
// Report ok if they're looking for size.
|
2020-02-13 21:03:54 +08:00
|
|
|
retVal = GetInfoStatus::SUCCESS;
|
2017-12-21 07:45:38 +08:00
|
|
|
} else if (destParamValue && destParamValueSize >= srcParamValueSize) {
|
|
|
|
// Report ok if we can copy safely
|
2020-02-13 21:03:54 +08:00
|
|
|
retVal = GetInfoStatus::SUCCESS;
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
memcpy_s(destParamValue, destParamValueSize, srcParamValue, srcParamValueSize);
|
|
|
|
} else if (!destParamValue) {
|
|
|
|
// Report ok if destParamValue == nullptr and destParamValueSize > 0
|
2020-02-13 21:03:54 +08:00
|
|
|
retVal = GetInfoStatus::SUCCESS;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct GetInfoHelper {
|
2020-02-13 21:03:54 +08:00
|
|
|
GetInfoHelper(void *dst, size_t dstSize, size_t *retSize, GetInfoStatus *retVal = nullptr)
|
2017-12-21 07:45:38 +08:00
|
|
|
: dst(dst), dstSize(dstSize), retSize(retSize), retVal(retVal) {
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename DataType>
|
2020-02-13 21:03:54 +08:00
|
|
|
GetInfoStatus set(const DataType &val) {
|
|
|
|
auto errCode = GetInfoStatus::SUCCESS;
|
2017-12-21 07:45:38 +08:00
|
|
|
if (retSize != nullptr) {
|
|
|
|
*retSize = sizeof(val);
|
|
|
|
}
|
|
|
|
if (dst != nullptr) {
|
|
|
|
if (dstSize >= sizeof(val)) {
|
|
|
|
*reinterpret_cast<DataType *>(dst) = val;
|
|
|
|
} else {
|
2020-02-13 21:03:54 +08:00
|
|
|
errCode = GetInfoStatus::INVALID_VALUE;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (retVal)
|
|
|
|
*retVal = errCode;
|
|
|
|
return errCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename DataType>
|
|
|
|
static void set(DataType *dst, DataType val) {
|
|
|
|
if (dst) {
|
|
|
|
*dst = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *dst;
|
|
|
|
size_t dstSize;
|
|
|
|
size_t *retSize;
|
2020-02-13 21:03:54 +08:00
|
|
|
GetInfoStatus *retVal;
|
2017-12-21 07:45:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ErrorCodeHelper {
|
2020-02-13 21:03:54 +08:00
|
|
|
ErrorCodeHelper(int *errcodeRet, int defaultCode)
|
2017-12-21 07:45:38 +08:00
|
|
|
: errcodeRet(errcodeRet) {
|
|
|
|
set(defaultCode);
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:03:54 +08:00
|
|
|
void set(int code) {
|
2017-12-21 07:45:38 +08:00
|
|
|
if (errcodeRet != nullptr) {
|
|
|
|
*errcodeRet = code;
|
|
|
|
}
|
|
|
|
localErrcode = code;
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:03:54 +08:00
|
|
|
int *errcodeRet;
|
|
|
|
int localErrcode;
|
2017-12-21 07:45:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T getValidParam(T param, T defaultVal = 1, T invalidVal = 0) {
|
|
|
|
if (param == invalidVal) {
|
|
|
|
return defaultVal;
|
|
|
|
}
|
|
|
|
return param;
|
|
|
|
}
|