From 55ae5202de17fb7f7d1d00fc76defa194d7f9b06 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sat, 27 Nov 2010 02:43:27 +0100 Subject: Added List class to avoid reinventing features in each case. --- common/action.cpp | 8 -------- common/action.h | 17 ++--------------- common/list.h | 34 ++++++++++++++++++++++++++++++++++ common/tile.cpp | 4 ---- common/tile.h | 18 +++++------------- 5 files changed, 41 insertions(+), 40 deletions(-) create mode 100644 common/list.h (limited to 'common') diff --git a/common/action.cpp b/common/action.cpp index 58b7392..77988d6 100644 --- a/common/action.cpp +++ b/common/action.cpp @@ -12,11 +12,3 @@ Action::Action(Type ty, int ta) : type(ty), target(ta) { bool Action::operator==(const Action& other) { return type == other.type && target == other.target; } - -Actions::operator bool() { - return !empty(); -} - -bool Actions::contains(Action action) { - return std::find(begin(), end(), action) != end(); -} diff --git a/common/action.h b/common/action.h index 4024c67..e1b06f2 100644 --- a/common/action.h +++ b/common/action.h @@ -1,8 +1,7 @@ #ifndef ACTION_H #define ACTION_H -#include -#include +#include "list.h" class Action { public: @@ -39,18 +38,6 @@ class Action { }; //! List of actions. -class Actions : public std::vector { - public: - //! Check if specified action is present in list. - bool contains(Action action); - - //! Check whether list is empty or not. - operator bool(); - - template - void serialize(Archive & ar, const unsigned int version) { - ar & boost::serialization::base_object >(*this); - } -}; +typedef List Actions; #endif diff --git a/common/list.h b/common/list.h new file mode 100644 index 0000000..92452f1 --- /dev/null +++ b/common/list.h @@ -0,0 +1,34 @@ +#ifndef LIST_H +#define LIST_H + +#include +#include +#include + +//! List class with extended functionality over std::vector. +template > +class List : public B { + public: + //! Sort the list. + void sort() { + std::sort(B::begin(), B::end()); + } + + //! Check if specified item is present in list. + bool contains(T item) { + return std::find(B::begin(), B::end(), item) != B::end(); + } + + //! Check whether list is empty or not. + operator bool() { + return !B::empty(); + } + + //! Allow serialization through Boost.Serialize. + template + void serialize(Archive & ar, const unsigned int version) { + ar & boost::serialization::base_object(*this); + } +}; + +#endif diff --git a/common/tile.cpp b/common/tile.cpp index 5a05548..0fb2230 100644 --- a/common/tile.cpp +++ b/common/tile.cpp @@ -59,7 +59,3 @@ Tile Tile::operator++(int) { type = Type(type + 1); return *this; } - -void Tiles::sort() { - std::sort(begin(), end()); -} diff --git a/common/tile.h b/common/tile.h index fe25a06..748d80b 100644 --- a/common/tile.h +++ b/common/tile.h @@ -1,9 +1,7 @@ #ifndef TILE_H #define TILE_H -#include -#include -#include +#include "list.h" class Tile { public: @@ -99,15 +97,9 @@ class Tile { }; //! List of tiles. -class Tiles : public std::vector { - public: - //! Sort the list of tiles. - void sort(); - - template - void serialize(Archive & ar, const unsigned int version) { - ar & boost::serialization::base_object >(*this); - } -}; +typedef List Tiles; + +//! List of list of tiles. +typedef List Tilegroups; #endif -- cgit v1.2.3