InspectorProtocolTypes.h [plain text]
#pragma once
#include <wtf/Assertions.h>
#include <wtf/JSONValues.h>
namespace Inspector {
namespace Protocol {
template<typename T> struct BindingTraits;
template<typename T>
class OptOutput {
public:
OptOutput() : m_assigned(false) { }
void operator=(T value)
{
m_value = value;
m_assigned = true;
}
bool isAssigned() const { return m_assigned; }
T getValue()
{
ASSERT(isAssigned());
return m_value;
}
private:
T m_value;
bool m_assigned;
WTF_MAKE_NONCOPYABLE(OptOutput);
};
template<typename T>
struct BindingTraits {
typedef T BindingType;
static void push(JSON::Array&, BindingType&);
static JSON::Value::Type typeTag();
static RefPtr<T> runtimeCast(RefPtr<JSON::Object>&&);
#if !ASSERT_DISABLED
static void assertValueHasExpectedType(JSON::Value*);
#endif // !ASSERT_DISABLED
};
template<JSON::Value::Type TYPE>
struct PrimitiveBindingTraits {
#if !ASSERT_DISABLED
static void assertValueHasExpectedType(JSON::Value* value)
{
ASSERT_ARG(value, value && value->type() == TYPE);
}
#endif // !ASSERT_DISABLED
};
template<typename T>
struct BindingTraits<JSON::ArrayOf<T>> {
static RefPtr<JSON::ArrayOf<T>> runtimeCast(RefPtr<JSON::Value>&& value)
{
ASSERT_ARG(value, value);
RefPtr<JSON::Array> array;
bool castSucceeded = value->asArray(array);
ASSERT_UNUSED(castSucceeded, castSucceeded);
#if !ASSERT_DISABLED
assertValueHasExpectedType(array.get());
#endif // !ASSERT_DISABLED
COMPILE_ASSERT(sizeof(JSON::ArrayOf<T>) == sizeof(JSON::Array), type_cast_problem);
return static_cast<JSON::ArrayOf<T>*>(static_cast<JSON::ArrayBase*>(array.get()));
}
#if !ASSERT_DISABLED
static void assertValueHasExpectedType(JSON::Value* value)
{
ASSERT_ARG(value, value);
RefPtr<JSON::Array> array;
bool castSucceeded = value->asArray(array);
ASSERT_UNUSED(castSucceeded, castSucceeded);
for (unsigned i = 0; i < array->length(); i++)
BindingTraits<T>::assertValueHasExpectedType(array->get(i).get());
}
#endif // !ASSERT_DISABLED
};
template<>
struct BindingTraits<JSON::Value> {
#if !ASSERT_DISABLED
static void assertValueHasExpectedType(JSON::Value*)
{
}
#endif // !ASSERT_DISABLED
};
template<> struct BindingTraits<JSON::Array> : public PrimitiveBindingTraits<JSON::Value::Type::Array> { };
template<> struct BindingTraits<JSON::Object> : public PrimitiveBindingTraits<JSON::Value::Type::Object> { };
template<> struct BindingTraits<String> : public PrimitiveBindingTraits<JSON::Value::Type::String> { };
template<> struct BindingTraits<bool> : public PrimitiveBindingTraits<JSON::Value::Type::Boolean> { };
template<> struct BindingTraits<double> : public PrimitiveBindingTraits<JSON::Value::Type::Double> { };
template<> struct BindingTraits<int> : public PrimitiveBindingTraits<JSON::Value::Type::Integer> { };
}
using Protocol::BindingTraits;
}