From 602a6a5e662784c2a9cbceb4ce7174ddd5128f3a Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sat, 30 Mar 2013 16:39:46 +0100 Subject: [PATCH] Added explicit keyword to all constructors. --- source/Game.h | 2 +- source/abstract/Character.h | 2 +- source/abstract/Sprite.h | 4 +-- source/effects/Bullet.h | 2 +- source/items/Weapon.h | 2 +- source/particle/Emitter.h | 2 +- source/particle/Particle.h | 2 +- source/sprites/Corpse.h | 2 +- source/sprites/Enemy.h | 2 +- source/sprites/Player.h | 2 +- source/sprites/TileManager.h | 4 +-- source/util/Yaml.h | 2 +- source/util/Yaml.inl | 47 ++++++++++++++++++++++++++++++++++++ 13 files changed, 61 insertions(+), 14 deletions(-) create mode 100644 source/util/Yaml.inl diff --git a/source/Game.h b/source/Game.h index 8fd86e0..85996ea 100644 --- a/source/Game.h +++ b/source/Game.h @@ -21,7 +21,7 @@ class World; class Game : private sf::NonCopyable { // Public functions. public: - Game(sf::RenderWindow& window); + explicit Game(sf::RenderWindow& window); ~Game(); void loop(); diff --git a/source/abstract/Character.h b/source/abstract/Character.h index 38532d2..edbf222 100644 --- a/source/abstract/Character.h +++ b/source/abstract/Character.h @@ -20,7 +20,7 @@ class Yaml; class Character : public Sprite { // Public functions. public: - Character(World& world, const Data& data, const Yaml& config); + explicit Character(World& world, const Data& data, const Yaml& config); virtual ~Character() = 0; void onDamage(int damage); diff --git a/source/abstract/Sprite.h b/source/abstract/Sprite.h index 96ba7e2..cb7f087 100755 --- a/source/abstract/Sprite.h +++ b/source/abstract/Sprite.h @@ -37,7 +37,7 @@ public: */ class Data { public: - Data(const sf::Vector2f& position, + explicit Data(const sf::Vector2f& position, Category category, unsigned short mask, const sf::Vector2f& direction = sf::Vector2f(0, 0)); const sf::Vector2f& position; @@ -56,7 +56,7 @@ public: // Public functions. public: - Sprite(const Data& data, const Yaml& config); + explicit Sprite(const Data& data, const Yaml& config); virtual ~Sprite() = 0; sf::Vector2f getPosition() const; diff --git a/source/effects/Bullet.h b/source/effects/Bullet.h index b91a1ce..5dacf39 100755 --- a/source/effects/Bullet.h +++ b/source/effects/Bullet.h @@ -18,7 +18,7 @@ class Yaml; class Bullet : public Particle { // Public functions. public: - Bullet(const sf::Vector2f& position, Sprite& shooter, + explicit Bullet(const sf::Vector2f& position, Sprite& shooter, sf::Vector2f direction, const Yaml& config); void onCollide(std::shared_ptr other); diff --git a/source/items/Weapon.h b/source/items/Weapon.h index 3179b89..d3d5f7d 100755 --- a/source/items/Weapon.h +++ b/source/items/Weapon.h @@ -27,7 +27,7 @@ class Particle; class Weapon : public Emitter { // Public functions. public: - Weapon(World& world, Sprite& holder, const Yaml& config); + explicit Weapon(World& world, Sprite& holder, const Yaml& config); void pullTrigger(); void releaseTrigger(); diff --git a/source/particle/Emitter.h b/source/particle/Emitter.h index f96de2f..75cda73 100755 --- a/source/particle/Emitter.h +++ b/source/particle/Emitter.h @@ -17,7 +17,7 @@ class Sprite; class Emitter { // Public functions. public: - Emitter(World& world); + explicit Emitter(World& world); virtual ~Emitter(); // Protected functions. diff --git a/source/particle/Particle.h b/source/particle/Particle.h index 6639b1d..e5f4b45 100755 --- a/source/particle/Particle.h +++ b/source/particle/Particle.h @@ -18,7 +18,7 @@ class Yaml; class Particle : public Sprite { // Public functions. public: - Particle(const Yaml& config, const Data& data); + explicit Particle(const Yaml& config, const Data& data); virtual ~Particle(); }; diff --git a/source/sprites/Corpse.h b/source/sprites/Corpse.h index e82c970..d00d28e 100644 --- a/source/sprites/Corpse.h +++ b/source/sprites/Corpse.h @@ -15,7 +15,7 @@ class Yaml; class Corpse : public Sprite { // Public functions. public: - Corpse(const sf::Vector2f& position, const Yaml& config); + explicit Corpse(const sf::Vector2f& position, const Yaml& config); }; #endif /* DG_CORPSE_H_ */ diff --git a/source/sprites/Enemy.h b/source/sprites/Enemy.h index 35c4067..809b8ab 100644 --- a/source/sprites/Enemy.h +++ b/source/sprites/Enemy.h @@ -16,7 +16,7 @@ class Yaml; class Enemy : public Character { // Public functions. public: - Enemy(World& world, const sf::Vector2f& position, const Yaml& config); + explicit Enemy(World& world, const sf::Vector2f& position, const Yaml& config); }; #endif /* DG_ENEMY_H_ */ diff --git a/source/sprites/Player.h b/source/sprites/Player.h index 4c432c4..9501423 100644 --- a/source/sprites/Player.h +++ b/source/sprites/Player.h @@ -31,7 +31,7 @@ public: // Public functions. public: - Player(World& world, const sf::Vector2f& position, const Yaml& config); + explicit Player(World& world, const sf::Vector2f& position, const Yaml& config); void setCrosshairPosition(const sf::Vector2f& position); void pullTrigger(); diff --git a/source/sprites/TileManager.h b/source/sprites/TileManager.h index 0bf66c1..55373a1 100644 --- a/source/sprites/TileManager.h +++ b/source/sprites/TileManager.h @@ -32,7 +32,7 @@ public: // Public functions. public: - TileManager(World& world); + explicit TileManager(World& world); void insertTile(const TilePosition& position, Type type); void removeTile(const TilePosition& position); @@ -57,7 +57,7 @@ private: class TileManager::Tile : public Sprite { // Public functions. public: - Tile(Type type, const TilePosition& position); + explicit Tile(Type type, const TilePosition& position); Type getType() const; TilePosition getTilePosition() const; diff --git a/source/util/Yaml.h b/source/util/Yaml.h index 60eaf94..6328809 100644 --- a/source/util/Yaml.h +++ b/source/util/Yaml.h @@ -21,7 +21,7 @@ class Yaml { // Public functions. public: - Yaml(const std::string& filename); + explicit Yaml(const std::string& filename); ~Yaml(); std::string getFilename() const; diff --git a/source/util/Yaml.inl b/source/util/Yaml.inl new file mode 100644 index 0000000..5d96a9e --- /dev/null +++ b/source/util/Yaml.inl @@ -0,0 +1,47 @@ +/* + * Yaml.h + * + * Created on: 30.03.2013 + * Author: Felix + */ + +#include + +#include "Log.h" + +/** + * Stream output operators to specialize Yaml::get for other types. + * Error handling is done in Yaml::get. + */ +namespace { + void operator>>(const YAML::Node& node, sf::Vector2i& vector) { + node[0] >> vector.x; + node[1] >> vector.y; + } + + void operator>>(const YAML::Node& node, sf::Vector2f& vector) { + node[0] >> vector.x; + node[1] >> vector.y; + } +} + +/** + * Gets a value of a specified type by key. Returns default value on error. + * + * @param key The string by which to select the return value. + * @tparam T The type of the return value. + * @return The value of the specified key. + */ +template +T Yaml::get(const std::string& key, const T& defaultValue) const { + try { + const YAML::Node* node = mNode.FindValue(key); + T value; + *node >> value; + return value; + } + catch (YAML::InvalidScalar&) { + LOG_W("Failed to get key " << key << " from " << mFolder << mFilename); + return defaultValue; + } +}; \ No newline at end of file