The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
control.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2015 - 2017 by Ignacio Riquelme Morelle <shadowm2006@gmail.com>
3  Part of the Battle for Wesnoth Project http://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  * Equivalent to calling arg(0).
59  */
60  operator const std::string&() const
61  {
62  return cmd();
63  }
64 
65  /**
66  * Returns the control command.
67  *
68  * Equivalent to calling arg(0).
69  */
70  const std::string& cmd() const
71  {
72  return args_[0];
73  }
74 
75  /**
76  * Returns the total number of arguments, not including the command itself.
77  */
78  size_t args_count() const
79  {
80  return args_.size() - 1;
81  }
82 
83  /**
84  * Returns the nth argument.
85  *
86  * @throws std::out_of_range @a n exceeds args_count().
87  */
88  const std::string& operator[](size_t n) const
89  {
90  return arg(n);
91  }
92 
93  /**
94  * Returns the nth argument.
95  *
96  * @throws std::out_of_range @a n exceeds args_count().
97  */
98  const std::string& arg(size_t n) const
99  {
100  if(n > args_count()) {
101  throw std::out_of_range("control line argument range exceeded");
102  }
103 
104  return args_[n];
105  }
106 
107  /**
108  * Return the full command line string.
109  */
111  {
112  return utils::join(args_, " ");
113  }
114 
115 private:
116  std::vector<std::string> args_;
117 };
118 
119 } // end namespace campaignd
std::vector< char_t > string
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
const std::string & cmd() const
Returns the control command.
Definition: control.hpp:70
bool empty() const
Whether the control line is empty.
Definition: control.hpp:48
const std::string & operator[](size_t n) const
Returns the nth argument.
Definition: control.hpp:88
size_t args_count() const
Returns the total number of arguments, not including the command itself.
Definition: control.hpp:78
std::vector< std::string > args_
Definition: control.hpp:116
control_line(const std::string &str)
Parses a control line string.
Definition: control.hpp:38
std::string full() const
Return the full command line string.
Definition: control.hpp:110
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.
const std::string & arg(size_t n) const
Returns the nth argument.
Definition: control.hpp:98
Represents a server control line written to a communication socket.
Definition: control.hpp:32
static map_location::DIRECTION n