Skip to content

Commit

Permalink
C++17: Make use of static inline for Activatable
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn committed Feb 22, 2023
1 parent dc5a99f commit 0587f3e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
12 changes: 5 additions & 7 deletions src/libtiled/logginginterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,11 @@ void LoggingInterface::log(OutputType type, const QString &message)
}


std::function<void (const OpenFile &)> OpenFile::activated;
std::function<void (const JumpToTile &)> JumpToTile::activated;
std::function<void (const JumpToObject &)> JumpToObject::activated;
std::function<void (const SelectLayer &)> SelectLayer::activated;
std::function<void (const SelectCustomProperty &)> SelectCustomProperty::activated;
std::function<void (const SelectTile &)> SelectTile::activated;

OpenFile::OpenFile(const QString &file)
: file(file)
{
Q_ASSERT(!file.isEmpty());
}

JumpToTile::JumpToTile(const Map *map, QPoint tilePos, const Layer *layer)
: mapFile(map->fileName)
Expand Down
39 changes: 17 additions & 22 deletions src/libtiled/logginginterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,70 +162,65 @@ inline void ERROR(QLatin1String message, std::function<void()> callback = std::f
ERROR(QString(message), std::move(callback), context);
}

// TODO: Try "static inline" once we switch to C++17
#define ACTIVATABLE(Class) \
void operator() () const { activated(*this); } \
static std::function<void (const Class &)> activated;
template<typename Class>
struct Activatable
{
void operator() () const {
activated(*static_cast<const Class*>(this));
}

static inline std::function<void (const Class &)> activated;
};

struct TILEDSHARED_EXPORT OpenFile
struct TILEDSHARED_EXPORT OpenFile : Activatable<OpenFile>
{
QString file;
OpenFile(const QString &file);

ACTIVATABLE(OpenFile)
QString file;
};

struct TILEDSHARED_EXPORT JumpToTile
struct TILEDSHARED_EXPORT JumpToTile : Activatable<JumpToTile>
{
JumpToTile(const Map *map, QPoint tilePos, const Layer *layer = nullptr);

QString mapFile;
QPoint tilePos;
int layerId = -1;

ACTIVATABLE(JumpToTile)
};

struct TILEDSHARED_EXPORT JumpToObject
struct TILEDSHARED_EXPORT JumpToObject : Activatable<JumpToObject>
{
JumpToObject(const MapObject *object);

QString mapFile;
int objectId;

ACTIVATABLE(JumpToObject)
};

struct TILEDSHARED_EXPORT SelectLayer
struct TILEDSHARED_EXPORT SelectLayer : Activatable<SelectLayer>
{
SelectLayer(const Layer *layer);

QString mapFile;
int layerId;

ACTIVATABLE(SelectLayer)
};

struct TILEDSHARED_EXPORT SelectCustomProperty
struct TILEDSHARED_EXPORT SelectCustomProperty : Activatable<SelectCustomProperty>
{
SelectCustomProperty(QString fileName, QString propertyName, const Object *object);

QString fileName;
QString propertyName;
int objectType; // see Object::TypeId
int id = -1;

ACTIVATABLE(SelectCustomProperty)
};

struct TILEDSHARED_EXPORT SelectTile
struct TILEDSHARED_EXPORT SelectTile : Activatable<SelectTile>
{
SelectTile(const Tile *tile);

QWeakPointer<Tileset> tileset;
QString tilesetFile;
int tileId;

ACTIVATABLE(SelectTile)
};

#undef ACTIVATABLE
Expand Down

0 comments on commit 0587f3e

Please sign in to comment.