#ifndef NodeOrString_h
#define NodeOrString_h
#include "Node.h"
#include <wtf/StdLibExtras.h>
#include <wtf/Vector.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
class NodeOrString {
public:
enum class Type {
String,
Node
};
NodeOrString(const String& string)
: m_type(Type::String)
{
m_data.string = string.impl();
m_data.string->ref();
}
NodeOrString(Node* node)
: m_type(Type::Node)
{
m_data.node = node;
m_data.node->ref();
}
NodeOrString(const NodeOrString& other)
: m_type(other.m_type)
{
switch (m_type) {
case Type::String:
m_data.string = other.m_data.string;
m_data.string->ref();
break;
case Type::Node:
m_data.node = other.m_data.node;
m_data.node->ref();
break;
}
}
NodeOrString(NodeOrString&& other)
: m_type(other.m_type)
{
switch (m_type) {
case Type::String:
m_data.string = std::exchange(other.m_data.string, nullptr);
break;
case Type::Node:
m_data.node = std::exchange(other.m_data.node, nullptr);
break;
}
}
NodeOrString& operator=(const NodeOrString& other)
{
if (this == &other)
return *this;
derefData();
m_type = other.m_type;
switch (m_type) {
case Type::String:
m_data.string = other.m_data.string;
m_data.string->ref();
break;
case Type::Node:
m_data.node = other.m_data.node;
m_data.node->ref();
break;
}
return *this;
}
NodeOrString& operator=(NodeOrString&& other)
{
if (this == &other)
return *this;
derefData();
m_type = other.m_type;
switch (m_type) {
case Type::String:
m_data.string = std::exchange(other.m_data.string, nullptr);
break;
case Type::Node:
m_data.node = std::exchange(other.m_data.node, nullptr);
break;
}
return *this;
}
~NodeOrString()
{
derefData();
}
Type type() const { return m_type; }
Node& node() const { ASSERT(m_type == Type::Node); ASSERT(m_data.node); return *m_data.node; }
StringImpl& string() const { ASSERT(m_type == Type::String); ASSERT(m_data.string); return *m_data.string; }
private:
void derefData()
{
switch (m_type) {
case Type::String:
if (m_data.string)
m_data.string->deref();
break;
case Type::Node:
if (m_data.node)
m_data.node->deref();
break;
}
}
Type m_type;
union {
StringImpl* string;
Node* node;
} m_data;
};
RefPtr<Node> convertNodesOrStringsIntoNode(Node& context, Vector<NodeOrString>&&, ExceptionCode&);
}
#endif // NodeOrString_h