The Battle for Wesnoth  1.15.0-dev
control.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2015 - 2018 by Iris Morelle <shadowm2006@gmail.com>
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 #pragma once
16 
18 
19 #include <stdexcept>
20 #include <vector>
21 
22 namespace campaignd
23 {
24 
25 /**
26  * Represents a server control line written to a communication socket.
27  *
28  * Control lines are plain text command lines using the ASCII space character
29  * (0x20) as command separator. This type is really only used to keep the code
30  * pretty.
31  */
33 {
34 public:
35  /**
36  * Parses a control line string.
37  */
38  control_line(const std::string& str) : args_(utils::split(str, ' '))
39  {
40  if(args_.empty()) {
41  args_.emplace_back();
42  }
43  }
44 
45  /**
46  * Whether the control line is empty.
47  */
48  bool empty() const
49  {
50  // Because of how utils::split() works, this can only happen if there
51  // are no other arguments.
52  return args_[0].empty();
53  }
54 
55  /**
56  * Returns the control command.
57  */
58  operator const std::string&() const
59  {
60  return cmd();
61  }
62 
63  /**
64  * Returns the control command.
65  */
66  const std::string& cmd() const
67  {
68  return args_[0];
69  }
70 
71  /**
72  * Returns the total number of arguments, not including the command itself.
73  */
74  std::size_t args_count() const
75  {
76  return args_.size() - 1;
77  }
78 
79  /**
80  * Returns the nth argument.
81  *
82  * @throws std::out_of_range @a n exceeds args_count().
83  */
84  const std::string& operator[](std::size_t n) const
85  {
86  return args_.at(n);
87  }
88 
89  /**
90  * Return the full command line string.
91  */
92  std::string full() const
93  {
94  return utils::join(args_, " ");
95  }
96 
97 private:
98  std::vector<std::string> args_;
99 };
100 
101 } // end namespace campaignd
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
std::vector< std::string > args_
Definition: control.hpp:98
control_line(const std::string &str)
Parses a control line string.
Definition: control.hpp:38
const std::string & operator[](std::size_t n) const
Returns the nth argument.
Definition: control.hpp:84
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
std::size_t args_count() const
Returns the total number of arguments, not including the command itself.
Definition: control.hpp:74
Represents a server control line written to a communication socket.
Definition: control.hpp:32
const std::string & cmd() const
Returns the control command.
Definition: control.hpp:66
bool empty() const
Whether the control line is empty.
Definition: control.hpp:48
std::string full() const
Return the full command line string.
Definition: control.hpp:92
static map_location::DIRECTION n