The Battle for Wesnoth  1.13.4+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 - 2016 by Ignacio Riquelme Morelle <[email protected]>
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 #ifndef CAMPAIGN_SERVER_CONTROL_HPP_INCLUDED
16 #define CAMPAIGN_SERVER_CONTROL_HPP_INCLUDED
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_.push_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  * Equivalent to calling arg(0).
60  */
61  operator const std::string&() const
62  {
63  return cmd();
64  }
65 
66  /**
67  * Returns the control command.
68  *
69  * Equivalent to calling arg(0).
70  */
71  const std::string& cmd() const
72  {
73  return args_[0];
74  }
75 
76  /**
77  * Returns the total number of arguments, not including the command itself.
78  */
79  size_t args_count() const
80  {
81  return args_.size() - 1;
82  }
83 
84  /**
85  * Returns the nth argument.
86  *
87  * @throws std::out_of_range @a n exceeds args_count().
88  */
89  const std::string& operator[](size_t n) const
90  {
91  return arg(n);
92  }
93 
94  /**
95  * Returns the nth argument.
96  *
97  * @throws std::out_of_range @a n exceeds args_count().
98  */
99  const std::string& arg(size_t n) const
100  {
101  if(n > args_count()) {
102  throw std::out_of_range("control line argument range exceeded");
103  }
104 
105  return args_[n];
106  }
107 
108  /**
109  * Return the full command line string.
110  */
112  {
113  return utils::join(args_, " ");
114  }
115 
116 private:
117  std::vector<std::string> args_;
118 };
119 
120 } // end namespace campaignd
121 
122 #endif // CAMPAIGN_SERVER_CONTROL_HPP_INCLUDED
const std::string & cmd() const
Returns the control command.
Definition: control.hpp:71
bool empty() const
Whether the control line is empty.
Definition: control.hpp:49
const std::string & operator[](size_t n) const
Returns the nth argument.
Definition: control.hpp:89
size_t args_count() const
Returns the total number of arguments, not including the command itself.
Definition: control.hpp:79
std::vector< std::string > args_
Definition: control.hpp:117
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:111
const std::string & arg(size_t n) const
Returns the nth argument.
Definition: control.hpp:99
std::string join(T const &v, const std::string &s=",")
Generates a new string joining container items in a list.
Represents a server control line written to a communication socket.
Definition: control.hpp:33
GLclampd n
Definition: glew.h:5903
std::vector< std::string > split(std::string const &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
GLsizei const GLcharARB ** string
Definition: glew.h:4503