The Battle for Wesnoth  1.19.16+dev
replay_recorder_base.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2025
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 
26 {
27 }
28 
30 {
31  commands_.swap(other.commands_);
32  std::swap(pos_, other.pos_);
33  upload_log_.swap(other.upload_log_);
34 }
35 
37 {
38  return pos_;
39 }
40 
42 {
43  return commands_.size();
44 }
45 
47 {
48  assert(pos < size());
49  return commands_[pos];
50 }
51 
53 {
54  assert(pos_ <= size());
55  commands_.insert(commands_.begin() + pos_, new config());
56  ++pos_;
57  return commands_[pos_ - 1];
58 }
60 {
61  assert(pos <= size());
62  pos_ = pos;
63 }
65 {
66  pos_ = size();
67 }
69 {
70  return upload_log_;
71 }
72 
74 {
75  assert(index < size());
76  commands_.erase(commands_.begin() + index);
77  if(index < pos_)
78  {
79  --pos_;
80  }
81 }
82 
84 {
85  assert(index <= size());
86  if(index < pos_)
87  {
88  ++pos_;
89  }
90  return *commands_.insert(commands_.begin() + index, new config());
91 }
92 
93 
95 {
96  if(const auto upload_log = data.optional_child("upload_log"))
97  {
98  upload_log_ = upload_log.value();
99  }
100  for(const config& command : data.child_range("command"))
101  {
102  commands_.push_back(new config(command));
103  }
104 }
105 
107 {
108  if(auto upload_log = data.optional_child("upload_log"))
109  {
110  upload_log_.swap(upload_log.value());
111  }
112  for(config& command : data.child_range("command"))
113  {
114  config* new_config = new config();
115  new_config->swap(command);
116  commands_.push_back(new_config);
117 
118 
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 
144 bool replay_recorder_base::is_ancestor(const config& other_replay) const
145 {
146  auto other_commands = other_replay.child_range("command");
147  if(other_commands.size() > commands_.size()) {
148  return false;
149  }
150  for(std::size_t index = 0; index < other_commands.size(); ++index) {
151  if(commands_[index] != other_commands[index]) {
152  return false;
153  }
154  }
155  return true;
156 }
157 
159 {
160  lhs.swap(rhs);
161 }
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:157
config & add_child(std::string_view key)
Definition: config.cpp:435
child_itors child_range(std::string_view key)
Definition: config.cpp:267
void swap(config &cfg) noexcept
Definition: config.cpp:1309
void write(config_writer &out) const
void append_config(const config &data)
bool is_ancestor(const config &other_replay) const
checks whether the parameter is an earlier state in the same "savegame gamestate branch"
void swap(replay_recorder_base &other) noexcept
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:1032
std::size_t index(std::string_view 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:188
void swap(replay_recorder_base &lhs, replay_recorder_base &rhs) noexcept
Implement non-member swap function for std::swap (calls replay_recorder_base::swap).