File: CmdLine.h

package info (click to toggle)
pentobi 18.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,912 kB
  • sloc: cpp: 26,729; javascript: 907; xml: 49; sh: 35; makefile: 21; java: 11
file content (92 lines) | stat: -rw-r--r-- 2,331 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//-----------------------------------------------------------------------------
/** @file libboardgame_gtp/CmdLine.h
    @author Markus Enzenberger
    @copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------

#ifndef LIBBOARDGAME_GTP_CMDLINE_H
#define LIBBOARDGAME_GTP_CMDLINE_H

#include <cassert>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>

namespace libboardgame_gtp {

using namespace std;

//-----------------------------------------------------------------------------

/** Parsed GTP command line.
    Only used internally by libboardgame_gtp::Engine. GTP command handlers
    query arguments of the command line through the instance of class Arguments
    given as a function argument by class Engine to the command handler. */
class CmdLine
{
public:
    /** Construct empty command.
        @warning An empty command cannot be used, before init() was called.
        This constructor exists only to reuse instances. */
    CmdLine() = default;

    /** Construct with a command line.
        @see init() */
    explicit CmdLine(const string& line);

    ~CmdLine();

    void init(const string& line);

    void init(const CmdLine& c);

    const string& get_line() const { return m_line; }

    /** Get command name. */
    string_view get_name() const { return m_elem[m_idx_name]; }


    void write_id(ostream& out) const;

    string_view get_trimmed_line_after_elem(unsigned i) const;

    const vector<string_view>& get_elements() const { return m_elem; }


    const string_view& get_element(unsigned i) const;

    unsigned get_idx_name() const { return m_idx_name; }

private:
    unsigned m_idx_name;

    /** Full command line. */
    string m_line;

    vector<string_view> m_elem;

    void add_elem(string::const_iterator begin, string::const_iterator end);

    void find_elem();

    void parse_id();
};

inline const string_view& CmdLine::get_element(unsigned i) const
{
    assert(i < m_elem.size());
    return m_elem[i];
}

inline void CmdLine::write_id(ostream& out) const
{
    if (m_idx_name != 0)
        out << m_elem[0];
}

//-----------------------------------------------------------------------------

} // namespace libboardgame_gtp

#endif // LIBBOARDGAME_GTP_CMDLINE_H