2016-10-28 18:20:08 +00:00
|
|
|
// Copyright 2016 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#ifndef {{"_".join(config.protocol.namespace)}}_ValueConversions_h
|
|
|
|
#define {{"_".join(config.protocol.namespace)}}_ValueConversions_h
|
|
|
|
|
|
|
|
//#include "ErrorSupport.h"
|
|
|
|
//#include "Forward.h"
|
|
|
|
//#include "Values.h"
|
|
|
|
|
|
|
|
{% for namespace in config.protocol.namespace %}
|
|
|
|
namespace {{namespace}} {
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct ValueConversions {
|
2016-11-23 00:45:30 +00:00
|
|
|
static std::unique_ptr<T> fromValue(protocol::Value* value, ErrorSupport* errors)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
2016-11-23 00:45:30 +00:00
|
|
|
return T::fromValue(value, errors);
|
2016-10-28 18:20:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-23 00:45:30 +00:00
|
|
|
static std::unique_ptr<protocol::Value> toValue(T* value)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
2016-11-23 00:45:30 +00:00
|
|
|
return value->toValue();
|
2016-10-28 18:20:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-23 00:45:30 +00:00
|
|
|
static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<T>& value)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
2016-11-23 00:45:30 +00:00
|
|
|
return value->toValue();
|
2016-10-28 18:20:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct ValueConversions<bool> {
|
2016-11-23 00:45:30 +00:00
|
|
|
static bool fromValue(protocol::Value* value, ErrorSupport* errors)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
bool success = value ? value->asBoolean(&result) : false;
|
|
|
|
if (!success)
|
|
|
|
errors->addError("boolean value expected");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-11-23 00:45:30 +00:00
|
|
|
static std::unique_ptr<protocol::Value> toValue(bool value)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
|
|
|
return FundamentalValue::create(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct ValueConversions<int> {
|
2016-11-23 00:45:30 +00:00
|
|
|
static int fromValue(protocol::Value* value, ErrorSupport* errors)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
bool success = value ? value->asInteger(&result) : false;
|
|
|
|
if (!success)
|
|
|
|
errors->addError("integer value expected");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-11-23 00:45:30 +00:00
|
|
|
static std::unique_ptr<protocol::Value> toValue(int value)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
|
|
|
return FundamentalValue::create(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct ValueConversions<double> {
|
2016-11-23 00:45:30 +00:00
|
|
|
static double fromValue(protocol::Value* value, ErrorSupport* errors)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
|
|
|
double result = 0;
|
|
|
|
bool success = value ? value->asDouble(&result) : false;
|
|
|
|
if (!success)
|
|
|
|
errors->addError("double value expected");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-11-23 00:45:30 +00:00
|
|
|
static std::unique_ptr<protocol::Value> toValue(double value)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
|
|
|
return FundamentalValue::create(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct ValueConversions<String> {
|
2016-11-23 00:45:30 +00:00
|
|
|
static String fromValue(protocol::Value* value, ErrorSupport* errors)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
|
|
|
String result;
|
|
|
|
bool success = value ? value->asString(&result) : false;
|
|
|
|
if (!success)
|
|
|
|
errors->addError("string value expected");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-11-23 00:45:30 +00:00
|
|
|
static std::unique_ptr<protocol::Value> toValue(const String& value)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
|
|
|
return StringValue::create(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct ValueConversions<Value> {
|
2016-11-23 00:45:30 +00:00
|
|
|
static std::unique_ptr<Value> fromValue(protocol::Value* value, ErrorSupport* errors)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
|
|
|
bool success = !!value;
|
|
|
|
if (!success) {
|
|
|
|
errors->addError("value expected");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return value->clone();
|
|
|
|
}
|
|
|
|
|
2016-11-23 00:45:30 +00:00
|
|
|
static std::unique_ptr<protocol::Value> toValue(Value* value)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
|
|
|
return value->clone();
|
|
|
|
}
|
|
|
|
|
2016-11-23 00:45:30 +00:00
|
|
|
static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<Value>& value)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
|
|
|
return value->clone();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct ValueConversions<DictionaryValue> {
|
2016-11-23 00:45:30 +00:00
|
|
|
static std::unique_ptr<DictionaryValue> fromValue(protocol::Value* value, ErrorSupport* errors)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
|
|
|
bool success = value && value->type() == protocol::Value::TypeObject;
|
|
|
|
if (!success)
|
|
|
|
errors->addError("object expected");
|
|
|
|
return DictionaryValue::cast(value->clone());
|
|
|
|
}
|
|
|
|
|
2016-11-23 00:45:30 +00:00
|
|
|
static std::unique_ptr<protocol::Value> toValue(DictionaryValue* value)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
|
|
|
return value->clone();
|
|
|
|
}
|
|
|
|
|
2016-11-23 00:45:30 +00:00
|
|
|
static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<DictionaryValue>& value)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
|
|
|
return value->clone();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct ValueConversions<ListValue> {
|
2016-11-23 00:45:30 +00:00
|
|
|
static std::unique_ptr<ListValue> fromValue(protocol::Value* value, ErrorSupport* errors)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
|
|
|
bool success = value && value->type() == protocol::Value::TypeArray;
|
|
|
|
if (!success)
|
|
|
|
errors->addError("list expected");
|
|
|
|
return ListValue::cast(value->clone());
|
|
|
|
}
|
|
|
|
|
2016-11-23 00:45:30 +00:00
|
|
|
static std::unique_ptr<protocol::Value> toValue(ListValue* value)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
|
|
|
return value->clone();
|
|
|
|
}
|
|
|
|
|
2016-11-23 00:45:30 +00:00
|
|
|
static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<ListValue>& value)
|
2016-10-28 18:20:08 +00:00
|
|
|
{
|
|
|
|
return value->clone();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
{% for namespace in config.protocol.namespace %}
|
|
|
|
} // namespace {{namespace}}
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
#endif // !defined({{"_".join(config.protocol.namespace)}}_ValueConversions_h)
|