The Battle for Wesnoth  1.19.0-dev
control.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2015 - 2024
3  by Iris Morelle <shadowm2006@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
19 
20 #include <stdexcept>
21 #include <vector>
22 
23 namespace campaignd
24 {
25 
26 /**
27  * Represents a server control line written to a communication socket.
28  *
29  * Control lines are plain text command lines using the ASCII space character
30  * (0x20) as command separator. This type is really only used to keep the code
31  * pretty.
32  */
34 {
35 public:
36  /**
37  * Parses a control line string.
38  */
39  control_line(const std::string& str) : args_(utils::split(str, ' '))
40  {
41  if(args_.empty()) {
42  args_.emplace_back();
43  }
44  }
45 
46  /**
47  * Whether the control line is empty.
48  */
49  bool empty() const
50  {
51  // Because of how utils::split() works, this can only happen if there
52  // are no other arguments.
53  return args_[0].empty();
54  }
55 
56  /**
57  * Returns the control command.
58  */
59  operator const std::string&() const
60  {
61  return cmd();
62  }
63 
64  /**
65  * Returns the control command.
66  */
67  const std::string& cmd() const
68  {
69  return args_[0];
70  }
71 
72  /**
73  * Returns the total number of arguments, not including the command itself.
74  */
75  std::size_t args_count() const
76  {
77  return args_.size() - 1;
78  }
79 
80  /**
81  * Returns the nth argument.
82  *
83  * @throws std::out_of_range @a n exceeds args_count().
84  */
85  const std::string& operator[](std::size_t n) const
86  {
87  return args_.at(n);
88  }
89 
90  /**
91  * Return the full command line string.
92  */
93  std::string full() const
94  {
95  return utils::join(args_, " ");
96  }
97 
98 private:
99  std::vector<std::string> args_;
100 };
101 
102 } // end namespace campaignd
Represents a server control line written to a communication socket.
Definition: control.hpp:34
std::vector< std::string > args_
Definition: control.hpp:99
const std::string & cmd() const
Returns the control command.
Definition: control.hpp:67
control_line(const std::string &str)
Parses a control line string.
Definition: control.hpp:39
std::string full() const
Return the full command line string.
Definition: control.hpp:93
const std::string & operator[](std::size_t n) const
Returns the nth argument.
Definition: control.hpp:85
bool empty() const
Whether the control line is empty.
Definition: control.hpp:49
std::size_t args_count() const
Returns the total number of arguments, not including the command itself.
Definition: control.hpp:75
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
std::vector< std::string > split(const config_attribute_value &val)
static map_location::DIRECTION n