The Battle for Wesnoth  1.17.0-dev
replay_recorder_base.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2021
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 
148 {
149  lhs.swap(rhs);
150 }
child_itors child_range(config_key_type key)
Definition: config.cpp:344
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:1447
void remove_command(int index)
Class for writing a config out to a file in pieces.
void write(config_writer &out) const
utils::optional_reference< config > optional_child(config_key_type key, int n=0)
Euivalent to child, but returns an empty optional if the nth child was not found. ...
Definition: config.cpp:445
config & get_command_at(int pos)
std::size_t i
Definition: function.cpp:967
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:72
config & add_child(config_key_type key)
Definition: config.cpp:514
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:61