comparison irccdctl/alias.hpp @ 348:24b1709093e7

Irccdctl: resurrect
author David Demelier <markand@malikania.fr>
date Sun, 13 Nov 2016 14:16:58 +0100
parents
children e9adab218027
comparison
equal deleted inserted replaced
347:ec43b9ac3df7 348:24b1709093e7
1 /*
2 * alias.hpp -- create irccdctl aliases
3 *
4 * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #ifndef IRCCD_ALIAS_HPP
20 #define IRCCD_ALIAS_HPP
21
22 /**
23 * \file alias.hpp
24 * \brief Create irccdctl aliases.
25 */
26
27 #include <ostream>
28 #include <string>
29 #include <vector>
30
31 #include "sysconfig.hpp"
32
33 namespace irccd {
34
35 /**
36 * \class AliasArg
37 * \brief Describe an alias argument.
38 *
39 * When the user specify arguments, it can precise an applied argument or a
40 * placeholder that will be substituted during command line invocation.
41 *
42 * Placeholders are placed using %n where n is an integer starting from 0.
43 */
44 class AliasArg {
45 private:
46 std::string m_value;
47 bool m_isPlaceholder;
48
49 public:
50 /**
51 * Construct an argument.
52 *
53 * \pre value must not be empty
54 * \param value the value
55 */
56 IRCCD_EXPORT AliasArg(std::string value);
57
58 /**
59 * Check if the argument is a placeholder.
60 *
61 * \return true if the argument is a placeholder
62 */
63 inline bool isPlaceholder() const noexcept
64 {
65 return m_isPlaceholder;
66 }
67
68 /**
69 * Get the placeholder index (e.g %0 returns 0)
70 *
71 * \pre isPlaceholder() must return true
72 * \return the position
73 */
74 IRCCD_EXPORT unsigned index() const noexcept;
75
76 /**
77 * Get the real value.
78 *
79 * \pre isPlaceholder() must return false
80 * \return the value
81 */
82 IRCCD_EXPORT const std::string &value() const noexcept;
83
84 /**
85 * Output the alias to the stream.
86 *
87 * \param out the output stream
88 * \return out
89 */
90 IRCCD_EXPORT friend std::ostream &operator<<(std::ostream &out, const AliasArg &);
91 };
92
93 /**
94 * \class AliasCommand
95 * \brief Describe a user-provided alias command.
96 *
97 * An alias command is just a command with a set of applied or placeholders
98 * arguments.
99 */
100 class AliasCommand {
101 private:
102 std::string m_command;
103 std::vector<AliasArg> m_args;
104
105 public:
106 /**
107 * Create an alias command.
108 *
109 * \param command the command
110 * \param args the arguments
111 */
112 inline AliasCommand(std::string command, std::vector<AliasArg> args = {}) noexcept
113 : m_command(std::move(command))
114 , m_args(std::move(args))
115 {
116 }
117
118 /**
119 * Get the command to execute.
120 *
121 * \return the command name
122 */
123 inline const std::string &command() const noexcept
124 {
125 return m_command;
126 }
127
128 /**
129 * Get the arguments.
130 *
131 * \return the arguments
132 */
133 inline const std::vector<AliasArg> &args() const noexcept
134 {
135 return m_args;
136 }
137 };
138
139 /**
140 * \class Alias
141 * \brief A set of commands to execute with their arguments.
142 *
143 * An alias is a composition of AliasCommand, typically, the user is able to set
144 * an alias that execute a list of specified commands in order they are defined.
145 */
146 class Alias : public std::vector<AliasCommand> {
147 private:
148 std::string m_name;
149
150 public:
151 /**
152 * Create an alias.
153 *
154 * \param name the alias name
155 */
156 inline Alias(std::string name) noexcept
157 : m_name(std::move(name))
158 {
159 }
160
161 /**
162 * Get the alias name.
163 *
164 * \return the name
165 */
166 inline const std::string &name() const noexcept
167 {
168 return m_name;
169 }
170 };
171
172 } // !irccd
173
174 #endif // !IRCCD_ALIAS_HPP