RpcRequest: add has* methods

This commit is contained in:
Stéphane Lepin 2019-11-15 19:08:19 +01:00
parent bdf1023b16
commit ee6e241144
2 changed files with 91 additions and 11 deletions

View File

@ -25,3 +25,81 @@ RpcRequest::RpcRequest(const QString& messageId, const QString& methodName, obs_
_parameters = obs_data_create();
obs_data_apply(_parameters, params);
}
const QString& RpcRequest::messageId() const
{
return _messageId;
}
const QString& RpcRequest::methodName() const
{
return _methodName;
}
const obs_data_t* RpcRequest::parameters() const
{
return _parameters;
}
bool RpcRequest::hasField(QString name, obs_data_type expectedFieldType, obs_data_number_type expectedNumberType)
{
if (!_parameters || name.isEmpty() || name.isNull()) {
return false;
}
OBSDataItemAutoRelease dataItem = obs_data_item_byname(_parameters, name.toUtf8());
if (!dataItem) {
return false;
}
if (expectedFieldType != OBS_DATA_NULL) {
obs_data_type fieldType = obs_data_item_gettype(dataItem);
if (fieldType != expectedFieldType) {
return false;
}
if (fieldType == OBS_DATA_NUMBER && expectedNumberType != OBS_DATA_NUM_INVALID) {
obs_data_number_type numberType = obs_data_item_numtype(dataItem);
if (numberType != expectedNumberType) {
return false;
}
}
}
return true;
}
bool RpcRequest::hasBool(QString fieldName)
{
return this->hasField(fieldName, OBS_DATA_BOOLEAN);
}
bool RpcRequest::hasString(QString fieldName)
{
return this->hasField(fieldName, OBS_DATA_STRING);
}
bool RpcRequest::hasNumber(QString fieldName, obs_data_number_type expectedNumberType)
{
return this->hasField(fieldName, OBS_DATA_NUMBER, expectedNumberType);
}
bool RpcRequest::hasInteger(QString fieldName)
{
return this->hasNumber(fieldName, OBS_DATA_NUM_INT);
}
bool RpcRequest::hasDouble(QString fieldName)
{
return this->hasNumber(fieldName, OBS_DATA_NUM_DOUBLE);
}
bool RpcRequest::hasArray(QString fieldName)
{
return this->hasField(fieldName, OBS_DATA_ARRAY);
}
bool RpcRequest::hasObject(QString fieldName)
{
return this->hasField(fieldName, OBS_DATA_OBJECT);
}

View File

@ -27,17 +27,19 @@ class RpcRequest
public:
explicit RpcRequest(const QString& messageId, const QString& methodName, obs_data_t* params);
const QString& messageId() const {
return _messageId;
}
const QString& methodName() const {
return _methodName;
}
const obs_data_t* parameters() const {
return _parameters;
}
const QString& messageId() const;
const QString& methodName() const;
const obs_data_t* parameters() const;
bool hasField(QString fieldName, obs_data_type expectedFieldType = OBS_DATA_NULL,
obs_data_number_type expectedNumberType = OBS_DATA_NUM_INVALID);
bool hasBool(QString fieldName);
bool hasString(QString fieldName);
bool hasNumber(QString fieldName, obs_data_number_type expectedNumberType = OBS_DATA_NUM_INVALID);
bool hasInteger(QString fieldName);
bool hasDouble(QString fieldName);
bool hasArray(QString fieldName);
bool hasObject(QString fieldName);
private:
const QString _messageId;