The Battle for Wesnoth  1.15.2+dev
replay_recorder_base.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017-2018 by the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #include "replay_recorder_base.hpp"
16 
18  : upload_log_()
19  , commands_()
20  , pos_(0)
21 {
22 
23 }
24 
25 
27 {
28 }
29 
30 
32 {
33  commands_.swap(other.commands_);
34  std::swap(pos_, other.pos_);
36 }
37 
39 {
40  return pos_;
41 }
42 
44 {
45  return commands_.size();
46 }
47 
49 {
50  assert(pos < size());
51  return commands_[pos];
52 }
53 
55 {
56  assert(pos_ <= size());
57  commands_.insert(commands_.begin() + pos_, new config());
58  ++pos_;
59  return commands_[pos_ - 1];
60 }
62 {
63  assert(pos <= size());
64  pos_ = pos;
65 }
67 {
68  pos_ = size();
69 }
71 {
72  return upload_log_;
73 }
74 
76 {
77  assert(index < size());
78  commands_.erase(commands_.begin() + index);
79  if(index < pos_)
80  {
81  --pos_;
82  }
83 }
84 
86 {
87  assert(index <= size());
88  if(index < pos_)
89  {
90  ++pos_;
91  }
92  return *commands_.insert(commands_.begin() + index, new config());
93 }
94 
95 
97 {
98  if(const config& upload_log = data.child("upload_log"))
99  {
100  upload_log_ = upload_log;
101  }
102  for(const config& command : data.child_range("command"))
103  {
104  commands_.push_back(new config(command));
105  }
106 }
107 
109 {
110  if(config& upload_log = data.child("upload_log"))
111  {
112  upload_log_.swap(upload_log);
113  }
114  for(config& command : data.child_range("command"))
115  {
116  config* new_config = new config();
117  new_config->swap(command);
118  commands_.push_back(new_config);
119  }
120 }
121 
123 {
124  out.write_child("upload_log", upload_log_);
125  for(int i = 0; i < pos_; ++i)
126  {
127  out.write_child("command", commands_[i]);
128  }
129 }
130 
132 {
133  out.add_child("upload_log", upload_log_);
134  for(int i = 0; i < pos_; ++i)
135  {
136  out.add_child("command", commands_[i]);
137  }
138 }
140 {
141  commands_.resize(pos_);
142 }
143 
145 {
146  lhs.swap(rhs);
147 }
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:420
child_itors child_range(config_key_type key)
Definition: config.cpp:362
config & insert_command(int index)
boost::ptr_vector< config > commands_
void write_child(const std::string &key, const config &cfg)
void swap(config &cfg)
Definition: config.cpp:1377
void remove_command(int index)
Class for writing a config out to a file in pieces.
void write(config_writer &out) const
config & get_command_at(int pos)
std::size_t i
Definition: function.cpp:933
void swap(replay_recorder_base &lhs, replay_recorder_base &rhs)
Implement non-member swap function for std::swap (calls replay_recorder_base::swap).
void append_config(const config &data)
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
config & add_child(config_key_type key)
Definition: config.cpp:476
void swap(replay_recorder_base &other)
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68