The Battle for Wesnoth  1.19.0-dev
replay_recorder_base.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2024
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "replay_recorder_base.hpp"
17 
19  : upload_log_()
20  , commands_()
21  , pos_(0)
22 {
23 
24 }
25 
26 
28 {
29 }
30 
31 
33 {
34  commands_.swap(other.commands_);
35  std::swap(pos_, other.pos_);
37 }
38 
40 {
41  return pos_;
42 }
43 
45 {
46  return commands_.size();
47 }
48 
50 {
51  assert(pos < size());
52  return commands_[pos];
53 }
54 
56 {
57  assert(pos_ <= size());
58  commands_.insert(commands_.begin() + pos_, new config());
59  ++pos_;
60  return commands_[pos_ - 1];
61 }
63 {
64  assert(pos <= size());
65  pos_ = pos;
66 }
68 {
69  pos_ = size();
70 }
72 {
73  return upload_log_;
74 }
75 
77 {
78  assert(index < size());
79  commands_.erase(commands_.begin() + index);
80  if(index < pos_)
81  {
82  --pos_;
83  }
84 }
85 
87 {
88  assert(index <= size());
89  if(index < pos_)
90  {
91  ++pos_;
92  }
93  return *commands_.insert(commands_.begin() + index, new config());
94 }
95 
96 
98 {
99  if(const auto upload_log = data.optional_child("upload_log"))
100  {
101  upload_log_ = upload_log.value();
102  }
103  for(const config& command : data.child_range("command"))
104  {
105  commands_.push_back(new config(command));
106  }
107 }
108 
110 {
111  if(auto upload_log = data.optional_child("upload_log"))
112  {
113  upload_log_.swap(upload_log.value());
114  }
115  for(config& command : data.child_range("command"))
116  {
117  config* new_config = new config();
118  new_config->swap(command);
119  commands_.push_back(new_config);
120 
121 
122  }
123 }
124 
126 {
127  out.write_child("upload_log", upload_log_);
128  for(int i = 0; i < pos_; ++i)
129  {
130  out.write_child("command", commands_[i]);
131  }
132 }
133 
135 {
136  out.add_child("upload_log", upload_log_);
137  for(int i = 0; i < pos_; ++i)
138  {
139  out.add_child("command", commands_[i]);
140  }
141 }
143 {
144  commands_.resize(pos_);
145 }
146 
147 bool replay_recorder_base::is_ancestor(const config& other_replay) const
148 {
149  auto other_commands = other_replay.child_range("command");
150  if(other_commands.size() > commands_.size()) {
151  return false;
152  }
153  for(size_t index = 0; index < other_commands.size(); ++index) {
154  if(commands_[index] != other_commands[index]) {
155  return false;
156  }
157  }
158  return true;
159 }
160 
162 {
163  lhs.swap(rhs);
164 }
Class for writing a config out to a file in pieces.
void write_child(const std::string &key, const config &cfg)
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
child_itors child_range(config_key_type key)
Definition: config.cpp:273
void swap(config &cfg)
Definition: config.cpp:1340
config & add_child(config_key_type key)
Definition: config.cpp:441
void write(config_writer &out) const
void append_config(const config &data)
void swap(replay_recorder_base &other)
bool is_ancestor(const config &other_replay) const
checks whether the parameter is an earlier state in the same "savegame gamestate branch"
config & get_command_at(int pos)
config & insert_command(int index)
boost::ptr_vector< config > commands_
void remove_command(int index)
std::size_t i
Definition: function.cpp:968
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:70
std::string_view data
Definition: picture.cpp:194
void swap(replay_recorder_base &lhs, replay_recorder_base &rhs)
Implement non-member swap function for std::swap (calls replay_recorder_base::swap).