#pragma once
#include "GridPosition.h"
#include <wtf/HashMap.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
class GridSpan {
public:
static GridSpan untranslatedDefiniteGridSpan(int startLine, int endLine)
{
return GridSpan(startLine, endLine, UntranslatedDefinite);
}
static GridSpan translatedDefiniteGridSpan(unsigned startLine, unsigned endLine)
{
return GridSpan(startLine, endLine, TranslatedDefinite);
}
static GridSpan indefiniteGridSpan()
{
return GridSpan(0, 1, Indefinite);
}
bool operator==(const GridSpan& o) const
{
return m_type == o.m_type && m_startLine == o.m_startLine && m_endLine == o.m_endLine;
}
unsigned integerSpan() const
{
ASSERT(isTranslatedDefinite());
return m_endLine - m_startLine;
}
int untranslatedStartLine() const
{
ASSERT(m_type == UntranslatedDefinite);
return m_startLine;
}
int untranslatedEndLine() const
{
ASSERT(m_type == UntranslatedDefinite);
return m_endLine;
}
unsigned startLine() const
{
ASSERT(isTranslatedDefinite());
ASSERT(m_endLine >= 0);
return m_startLine;
}
unsigned endLine() const
{
ASSERT(isTranslatedDefinite());
ASSERT(m_endLine > 0);
return m_endLine;
}
struct GridSpanIterator {
GridSpanIterator(unsigned value)
: value(value)
{
}
operator unsigned&() { return value; }
unsigned operator*() const { return value; }
unsigned value;
};
GridSpanIterator begin() const
{
ASSERT(isTranslatedDefinite());
return m_startLine;
}
GridSpanIterator end() const
{
ASSERT(isTranslatedDefinite());
return m_endLine;
}
bool isTranslatedDefinite() const
{
return m_type == TranslatedDefinite;
}
bool isIndefinite() const
{
return m_type == Indefinite;
}
void translate(unsigned offset)
{
ASSERT(m_type == UntranslatedDefinite);
m_type = TranslatedDefinite;
m_startLine += offset;
m_endLine += offset;
ASSERT(m_startLine >= 0);
ASSERT(m_endLine > 0);
}
private:
enum GridSpanType {UntranslatedDefinite, TranslatedDefinite, Indefinite};
GridSpan(int startLine, int endLine, GridSpanType type)
: m_type(type)
{
#if ENABLE(ASSERT)
ASSERT(startLine < endLine);
if (type == TranslatedDefinite) {
ASSERT(startLine >= 0);
ASSERT(endLine > 0);
}
#endif
m_startLine = std::max(GridPosition::min(), std::min(startLine, GridPosition::max() - 1));
m_endLine = std::max(GridPosition::min() + 1, std::min(endLine, GridPosition::max()));
}
int m_startLine;
int m_endLine;
GridSpanType m_type;
};
class GridArea {
public:
GridArea()
: columns(GridSpan::indefiniteGridSpan())
, rows(GridSpan::indefiniteGridSpan())
{
}
GridArea(const GridSpan& r, const GridSpan& c)
: columns(c)
, rows(r)
{
}
bool operator==(const GridArea& o) const
{
return columns == o.columns && rows == o.rows;
}
bool operator!=(const GridArea& o) const
{
return !(*this == o);
}
GridSpan columns;
GridSpan rows;
};
typedef HashMap<String, GridArea> NamedGridAreaMap;
}