Skip to content

Commit

Permalink
boost::ptr_vector -> std::vector<unique_ptr>
Browse files Browse the repository at this point in the history
boost::ptr_vector has some nice features, but vector<unique_ptr> is still
easier to use for most people basicially becasue people know it better.
Also boost::ptr_vector does not support move ctors and also does not
use std::unique_ptr, probably because it tries to stay compatible with
c++03 so one has to use 'ptr_vector::auto_type' with it instead which
has a different interface than std::unique_ptr
  • Loading branch information
gfgtdf committed Aug 13, 2018
1 parent f03e3e8 commit 7e2dc29
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
28 changes: 15 additions & 13 deletions src/actions/undo.cpp
Expand Up @@ -295,7 +295,7 @@ void undo_list::read(const config & cfg)
try {
undo_action_base * action = create_action(child);
if ( action ) {
undos_.push_back(action);
undos_.emplace_back(action);
}
} catch (const bad_lexical_cast &) {
ERR_NG << "Error when parsing undo list from config: bad lexical cast." << std::endl;
Expand All @@ -311,7 +311,7 @@ void undo_list::read(const config & cfg)
// Build the redo stack.
for (const config & child : cfg.child_range("redo")) {
try {
redos_.push_back(new config(child));
redos_.emplace_back(new config(child));
} catch (const bad_lexical_cast &) {
ERR_NG << "Error when parsing redo list from config: bad lexical cast." << std::endl;
ERR_NG << "config was: " << child.debug() << std::endl;
Expand All @@ -333,11 +333,11 @@ void undo_list::write(config & cfg) const
cfg["side"] = side_;
cfg["committed"] = committed_actions_;

for ( action_list::const_iterator it = undos_.begin(); it != undos_.end(); ++it )
it->write(cfg.add_child("undo"));
for ( const auto& action_ptr : undos_)
action_ptr->write(cfg.add_child("undo"));

for ( redos_list::const_iterator it = redos_.begin(); it != redos_.end(); ++it )
cfg.add_child("redo") = *it;
for ( const auto& cfg_ptr : redos_)
cfg.add_child("redo") = *cfg_ptr;
}


Expand All @@ -353,8 +353,9 @@ void undo_list::undo()

// Get the action to undo. (This will be placed on the redo stack, but
// only if the undo is successful.)
action_list::auto_type action = undos_.pop_back();
if (undo_action* undoable_action = dynamic_cast<undo_action*>(action.ptr()))
auto action = std::move(undos_.back());
undos_.pop_back();
if (undo_action* undoable_action = dynamic_cast<undo_action*>(action.get()))
{
int last_unit_id = resources::gameboard->unit_id_manager().get_save_id();
if ( !undoable_action->undo(side_) ) {
Expand All @@ -366,8 +367,8 @@ void undo_list::undo()
resources::gameboard->unit_id_manager().set_save_id(last_unit_id - undoable_action->unit_id_diff);

// Bookkeeping.
redos_.push_back(new config());
resources::recorder->undo_cut(redos_.back());
redos_.emplace_back(new config());
resources::recorder->undo_cut(*redos_.back());

resources::whiteboard->on_gamestate_change();
}
Expand All @@ -378,7 +379,7 @@ void undo_list::undo()
resources::recorder->undo_cut(replay_data);
undo();
resources::recorder->redo(replay_data);
undos_.push_back(action.release());
undos_.emplace_back(std::move(action));
}
}

Expand All @@ -396,7 +397,8 @@ void undo_list::redo()

// Get the action to redo. (This will be placed on the undo stack, but
// only if the redo is successful.)
redos_list::auto_type action = redos_.pop_back();
auto action = std::move(redos_.back());
redos_.pop_back();

const config& command_wml = action->child("command");
std::string commandname = command_wml.all_children_range().front().key;
Expand Down Expand Up @@ -436,7 +438,7 @@ bool undo_list::apply_shroud_changes() const

// Loop through the list of undo_actions.
for( std::size_t i = 0; i != list_size; ++i ) {
if (const shroud_clearing_action* action = dynamic_cast<const shroud_clearing_action*>(&undos_[i])) {
if (const shroud_clearing_action* action = dynamic_cast<const shroud_clearing_action*>(undos_[i].get())) {
LOG_NG << "Turning an undo...\n";

// Clear the hexes this unit can see from each hex occupied during
Expand Down
6 changes: 3 additions & 3 deletions src/actions/undo.hpp
Expand Up @@ -34,8 +34,8 @@ namespace actions {
/// Class to store the actions that a player can undo and redo.
class undo_list {

typedef boost::ptr_vector<undo_action_base> action_list;
typedef boost::ptr_vector<config> redos_list;
typedef std::vector<std::unique_ptr<undo_action_base>> action_list;
typedef std::vector<std::unique_ptr<config>> redos_list;

public:
undo_list(const undo_list&) = delete;
Expand Down Expand Up @@ -100,7 +100,7 @@ class undo_list {
private: // functions
/// Adds an action to the undo stack.
void add(undo_action_base * action)
{ undos_.push_back(action); redos_.clear(); }
{ undos_.emplace_back(action); redos_.clear(); }
/// Applies the pending fog/shroud changes from the undo stack.
bool apply_shroud_changes() const;

Expand Down

0 comments on commit 7e2dc29

Please sign in to comment.