Skip to content

Commit

Permalink
Refactor: Renamed castTo<> to as<> in Info and Event
Browse files Browse the repository at this point in the history
If C# has taught me anything it is that "as" is quite an intuitive
keyword for casting.
  • Loading branch information
skyjake committed May 31, 2013
1 parent 51c74f5 commit c2eb94f
Show file tree
Hide file tree
Showing 18 changed files with 36 additions and 24 deletions.
2 changes: 2 additions & 0 deletions doomsday/client/include/map/mapelement.h
Expand Up @@ -84,6 +84,7 @@ class MapElement

void setIndexInMap(int newIndex = NoIndex);

/// @todo Rename to as().
template <typename Type>
inline Type *castTo()
{
Expand All @@ -92,6 +93,7 @@ class MapElement
return t;
}

/// @todo Rename to as().
template <typename Type>
inline Type const *castTo() const
{
Expand Down
2 changes: 1 addition & 1 deletion doomsday/client/src/ui/dd_input.cpp
Expand Up @@ -983,7 +983,7 @@ void DD_ConvertEvent(de::Event const &event, ddevent_t *ddEvent)
case de::Event::KeyPress:
case de::Event::KeyRelease:
{
KeyEvent const &kev = static_cast<KeyEvent const &>(event);
KeyEvent const &kev = event.as<KeyEvent>();

ddEvent->device = IDEV_KEYBOARD;
ddEvent->type = E_TOGGLE;
Expand Down
2 changes: 1 addition & 1 deletion doomsday/client/src/ui/widgets/consolecommandwidget.cpp
Expand Up @@ -70,7 +70,7 @@ bool ConsoleCommandWidget::handleEvent(Event const &event)
{
if(!event.isKeyDown()) return false;

KeyEvent const &key = static_cast<KeyEvent const &>(event);
KeyEvent const &key = event.as<KeyEvent>();

// Override the handling of the Enter key.
if(key.qtKey() == Qt::Key_Return || key.qtKey() == Qt::Key_Enter)
Expand Down
2 changes: 1 addition & 1 deletion doomsday/client/src/ui/widgets/consolewidget.cpp
Expand Up @@ -131,7 +131,7 @@ bool ConsoleWidget::handleEvent(Event const &event)
{
if(event.type() == Event::KeyPress)
{
KeyEvent const &key = static_cast<KeyEvent const &>(event);
KeyEvent const &key = event.as<KeyEvent>();

if(key.qtKey() == Qt::Key_PageUp ||
key.qtKey() == Qt::Key_PageDown)
Expand Down
2 changes: 1 addition & 1 deletion doomsday/client/src/ui/widgets/legacywidget.cpp
Expand Up @@ -201,7 +201,7 @@ bool LegacyWidget::handleEvent(Event const &event)
if(event.type() == Event::KeyPress ||
event.type() == Event::KeyRelease)
{
KeyEvent const &ev = static_cast<KeyEvent const &>(event);
KeyEvent const &ev = event.as<KeyEvent>();
Keyboard_Submit(ev.state() == KeyEvent::Pressed? IKE_DOWN : IKE_UP,
ev.ddKey(), ev.nativeCode(), ev.text().toLatin1());
}
Expand Down
2 changes: 1 addition & 1 deletion doomsday/client/src/ui/widgets/lineeditwidget.cpp
Expand Up @@ -260,7 +260,7 @@ bool LineEditWidget::handleEvent(Event const &event)
{
if(event.isKeyDown())
{
KeyEvent const &key = static_cast<KeyEvent const &>(event);
KeyEvent const &key = event.as<KeyEvent>();

// Control character.
if(handleControlKey(key.qtKey(), key.modifiers().testFlag(KeyEvent::Control)))
Expand Down
2 changes: 1 addition & 1 deletion doomsday/client/src/ui/widgets/logwidget.cpp
Expand Up @@ -867,7 +867,7 @@ bool LogWidget::handleEvent(Event const &event)
{
if(!event.isKeyDown()) return false;

KeyEvent const &ev = static_cast<KeyEvent const &>(event);
KeyEvent const &ev = event.as<KeyEvent>();

int pageSize = scrollPageSize();

Expand Down
12 changes: 12 additions & 0 deletions doomsday/libdeng2/include/de/core/event.h
Expand Up @@ -50,6 +50,18 @@ class DENG2_PUBLIC Event

bool isKeyDown() const { return _type == KeyPress || _type == KeyRepeat; }

template <typename Type>
Type &as() {
DENG2_ASSERT(dynamic_cast<Type *>(this) != 0);
return *static_cast<Type *>(this);
}

template <typename Type>
Type const &as() const {
DENG2_ASSERT(dynamic_cast<Type const *>(this) != 0);
return *static_cast<Type const *>(this);
}

private:
int _type;
};
Expand Down
4 changes: 2 additions & 2 deletions doomsday/libdeng2/include/de/data/info.h
Expand Up @@ -95,14 +95,14 @@ class Info
String const &name() const { return _name; }

template <typename T>
T &castTo() {
T &as() {
T *t = dynamic_cast<T *>(this);
DENG2_ASSERT(t != 0);
return *t;
}

template <typename T>
T const &castTo() const {
T const &as() const {
T const *t = dynamic_cast<T const *>(this);
DENG2_ASSERT(t != 0);
return *t;
Expand Down
8 changes: 4 additions & 4 deletions doomsday/libdeng2/src/data/info.cpp
Expand Up @@ -586,7 +586,7 @@ void Info::BlockElement::add(Info::Element *elem)
if(elem->name() && _contents.contains(elem->name()))
{
if(!elem->isBlock() || !info().d->allowDuplicateBlocksOfType.contains(
elem->castTo<BlockElement>().blockType()))
elem->as<BlockElement>().blockType()))
{
LOG_AS("Info::BlockElement");
LOG_WARNING("Block '%s' already has an element named '%s'")
Expand Down Expand Up @@ -614,7 +614,7 @@ Info::Element::Value Info::BlockElement::keyValue(String const &name) const
{
Element *e = find(name);
if(!e || !e->isKey()) return Value();
return e->castTo<KeyElement>().value();
return e->as<KeyElement>().value();
}

Info::Element *Info::BlockElement::findByPath(String const &path) const
Expand All @@ -640,7 +640,7 @@ Info::Element *Info::BlockElement::findByPath(String const &path) const
if(e->isBlock())
{
// Descend into sub-blocks.
return e->castTo<BlockElement>().findByPath(remainder);
return e->as<BlockElement>().findByPath(remainder);
}
return e;
}
Expand Down Expand Up @@ -700,7 +700,7 @@ bool Info::findValueForKey(String const &key, String &value) const
Element const *element = findByPath(key);
if(element && element->isKey())
{
value = element->castTo<KeyElement>().value();
value = element->as<KeyElement>().value();
return true;
}
return false;
Expand Down
6 changes: 3 additions & 3 deletions doomsday/libdeng2/src/scriptsys/scriptedinfo.cpp
Expand Up @@ -70,15 +70,15 @@ DENG2_PIMPL(ScriptedInfo)
{
if(element->isBlock())
{
processBlock(element->castTo<Info::BlockElement>());
processBlock(element->as<Info::BlockElement>());
}
else if(element->isKey())
{
processKey(element->castTo<Info::KeyElement>());
processKey(element->as<Info::KeyElement>());
}
else if(element->isList())
{
processList(element->castTo<Info::ListElement>());
processList(element->as<Info::ListElement>());
}
}

Expand Down
2 changes: 1 addition & 1 deletion doomsday/libshell/src/choicewidget.cpp
Expand Up @@ -141,7 +141,7 @@ bool ChoiceWidget::handleEvent(Event const &ev)
{
if(ev.type() == Event::KeyPress)
{
KeyEvent const &event = static_cast<KeyEvent const &>(ev);
KeyEvent const &event = ev.as<KeyEvent>();
if(!event.text().isEmpty() || event.key() == Qt::Key_Enter)
{
DENG2_ASSERT(!isOpen());
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libshell/src/commandlinewidget.cpp
Expand Up @@ -42,7 +42,7 @@ bool CommandLineWidget::handleEvent(Event const &event)
{
// There are only key press events.
DENG2_ASSERT(event.type() == Event::KeyPress);
KeyEvent const &ev = static_cast<KeyEvent const &>(event);
KeyEvent const &ev = event.as<KeyEvent>();

// Override the editor's normal Enter handling.
if(ev.key() == Qt::Key_Enter)
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libshell/src/dialogwidget.cpp
Expand Up @@ -84,7 +84,7 @@ bool DialogWidget::handleEvent(Event const &event)
{
if(event.type() == Event::KeyPress)
{
KeyEvent const &ev = static_cast<KeyEvent const &>(event);
KeyEvent const &ev = event.as<KeyEvent>();
if(ev.key() == Qt::Key_Escape)
{
reject();
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libshell/src/lineeditwidget.cpp
Expand Up @@ -110,7 +110,7 @@ bool LineEditWidget::handleEvent(Event const &event)
{
// There are only key press events.
DENG2_ASSERT(event.type() == Event::KeyPress);
KeyEvent const &ev = static_cast<KeyEvent const &>(event);
KeyEvent const &ev = event.as<KeyEvent>();

bool eaten = true;

Expand Down
2 changes: 1 addition & 1 deletion doomsday/libshell/src/logwidget.cpp
Expand Up @@ -264,7 +264,7 @@ bool LogWidget::handleEvent(Event const &event)
{
if(event.type() != Event::KeyPress) return false;

KeyEvent const &ev = static_cast<KeyEvent const &>(event);
KeyEvent const &ev = event.as<KeyEvent>();

int pageSize = scrollPageSize();

Expand Down
2 changes: 1 addition & 1 deletion doomsday/libshell/src/menuwidget.cpp
Expand Up @@ -352,7 +352,7 @@ bool MenuWidget::handleEvent(Event const &event)

if(event.type() != Event::KeyPress) return false;

KeyEvent const &ev = static_cast<KeyEvent const &>(event);
KeyEvent const &ev = event.as<KeyEvent>();

// Check menu-related control keys.
if(ev.text().isEmpty())
Expand Down
4 changes: 1 addition & 3 deletions doomsday/libshell/src/textwidget.cpp
Expand Up @@ -134,9 +134,7 @@ bool TextWidget::handleEvent(Event const &event)
// We only support KeyEvents.
if(event.type() == Event::KeyPress)
{
DENG2_ASSERT(dynamic_cast<KeyEvent const *>(&event) != 0);

KeyEvent const &keyEvent = static_cast<KeyEvent const &>(event);
KeyEvent const &keyEvent = event.as<KeyEvent>();

foreach(Action *act, d->actions)
{
Expand Down

0 comments on commit c2eb94f

Please sign in to comment.