Commit 065e3983 authored by Hitoshi Yoshida's avatar Hitoshi Yoshida Committed by Commit Bot

CodeGen: Make generated Enumeration classes more useful

Makes generated code of IDL Enumeration more useful in following ways;
- Drops declaration of friend operators == and !=.  Those operators
  become able to work.
- Adds == and != operators to compare with other types, e.g. String,
  AtomicString, and char[], in the base class as migration adapters.
- Adds ToString() method to allow CHECK() output the value.


Bug: 839389
Change-Id: I05dbeb2a2f4eae51b67b14ef676941eab93c6862
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2134002Reviewed-by: default avatarYuki Shiino <yukishiino@chromium.org>
Commit-Queue: Hitoshi Yoshida <peria@chromium.org>
Cr-Commit-Position: refs/heads/master@{#756610}
parent e4d57011
...@@ -166,25 +166,6 @@ def make_equality_operators(cg_context): ...@@ -166,25 +166,6 @@ def make_equality_operators(cg_context):
decls = ListNode([func1_def, EmptyNode(), func2_def]) decls = ListNode([func1_def, EmptyNode(), func2_def])
# Migration adapter
func3_def = CxxFuncDefNode(
name="operator==",
arg_decls=["const ${class_name}& lhs", "const String& rhs"],
return_type="bool",
inline=True)
func3_def.set_base_template_vars(cg_context.template_bindings())
func3_def.body.append(TextNode("return lhs.AsString() == rhs;"))
func4_def = CxxFuncDefNode(
name="operator==",
arg_decls=["const String& lhs", "const ${class_name}& rhs"],
return_type="bool",
inline=True)
func4_def.set_base_template_vars(cg_context.template_bindings())
func4_def.body.append(TextNode("return lhs == rhs.AsString();"))
decls.extend([EmptyNode(), func3_def, EmptyNode(), func4_def])
return decls, None return decls, None
......
...@@ -23,9 +23,17 @@ class PLATFORM_EXPORT EnumerationBase { ...@@ -23,9 +23,17 @@ class PLATFORM_EXPORT EnumerationBase {
public: public:
~EnumerationBase() = default; ~EnumerationBase() = default;
// Returns the IDL enumeration value as a string.
// https://heycam.github.io/webidl/#dfn-enumeration-value
const char* AsCStr() const { return string_literal_; } const char* AsCStr() const { return string_literal_; }
String AsString() const { return string_literal_; } String AsString() const { return string_literal_; }
// Returns the string representation to be used by CHECK_OP family.
// This member function is meant only for CHECK_EQ, etc.
String ToString() const {
return String::Format("IDL enum value \"%s\"", string_literal_);
}
// Migration adapter // Migration adapter
operator AtomicString() const { return string_literal_; } operator AtomicString() const { return string_literal_; }
operator String() const { return string_literal_; } operator String() const { return string_literal_; }
...@@ -57,34 +65,70 @@ class PLATFORM_EXPORT EnumerationBase { ...@@ -57,34 +65,70 @@ class PLATFORM_EXPORT EnumerationBase {
// |string_literal_| is a pointer to a string in the table. // |string_literal_| is a pointer to a string in the table.
enum_int_t enum_value_ = 0; enum_int_t enum_value_ = 0;
const char* string_literal_ = nullptr; const char* string_literal_ = nullptr;
template <typename T>
friend typename std::
enable_if_t<std::is_base_of<bindings::EnumerationBase, T>::value, bool>
operator==(const T& lhs, const T& rhs);
template <typename T>
friend typename std::
enable_if_t<std::is_base_of<bindings::EnumerationBase, T>::value, bool>
operator!=(const T& lhs, const T& rhs);
}; };
} // namespace bindings } // namespace bindings
template <typename T> template <typename EnumTypeClass>
typename std::enable_if_t<std::is_base_of<bindings::EnumerationBase, T>::value, typename std::enable_if_t<
bool> std::is_base_of<bindings::EnumerationBase, EnumTypeClass>::value,
operator==(const T& lhs, const T& rhs) { bool>
operator==(const EnumTypeClass& lhs, const EnumTypeClass& rhs) {
DCHECK(!lhs.IsEmpty() && !rhs.IsEmpty()); DCHECK(!lhs.IsEmpty() && !rhs.IsEmpty());
return lhs.string_literal_ == rhs.string_literal_; return lhs.AsCStr() == rhs.AsCStr();
} }
template <typename T> template <typename EnumTypeClass>
typename std::enable_if_t<std::is_base_of<bindings::EnumerationBase, T>::value, typename std::enable_if_t<
bool> std::is_base_of<bindings::EnumerationBase, EnumTypeClass>::value,
operator!=(const T& lhs, const T& rhs) { bool>
operator!=(const EnumTypeClass& lhs, const EnumTypeClass& rhs) {
DCHECK(!lhs.IsEmpty() && !rhs.IsEmpty()); DCHECK(!lhs.IsEmpty() && !rhs.IsEmpty());
return lhs.string_literal_ != rhs.string_literal_; return lhs.AsCStr() != rhs.AsCStr();
}
// Migration adapters
template <typename EnumTypeClass>
typename std::enable_if_t<
std::is_base_of<bindings::EnumerationBase, EnumTypeClass>::value,
bool>
operator==(const EnumTypeClass& lhs, const String& rhs) {
DCHECK(!lhs.IsEmpty());
return lhs.AsString() == rhs;
}
template <typename EnumTypeClass>
typename std::enable_if_t<
std::is_base_of<bindings::EnumerationBase, EnumTypeClass>::value,
bool>
operator!=(const EnumTypeClass& lhs, const String& rhs) {
return !(lhs == rhs);
}
template <typename EnumTypeClass>
typename std::enable_if_t<
std::is_base_of<bindings::EnumerationBase, EnumTypeClass>::value,
bool>
operator==(const EnumTypeClass& lhs, const AtomicString& rhs) {
DCHECK(!lhs.IsEmpty());
return lhs.AsString() == rhs;
}
template <typename EnumTypeClass>
typename std::enable_if_t<
std::is_base_of<bindings::EnumerationBase, EnumTypeClass>::value,
bool>
operator==(const EnumTypeClass& lhs, const char* rhs) {
DCHECK(!lhs.IsEmpty());
return lhs.AsString() == rhs;
}
template <typename EnumTypeClass>
typename std::enable_if_t<
std::is_base_of<bindings::EnumerationBase, EnumTypeClass>::value,
bool>
operator==(const char* lhs, const EnumTypeClass& rhs) {
return rhs == lhs;
} }
} // namespace blink } // namespace blink
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment