Libmacro  0.2
Libmacro is an extensible macro and hotkey library.
command.h
Go to the documentation of this file.
1 /* Libmacro - A multi-platform, extendable macro and hotkey C library
2  Copyright (C) 2013 Jonathan Pelletier, New Paradigm Software
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
24 #ifndef MCR_EXTRAS_SIGNALS_COMMAND_H_
25 #define MCR_EXTRAS_SIGNALS_COMMAND_H_
26 
28 #include "mcr/extras/safe_string.h"
29 
30 namespace mcr
31 {
32 class MCR_API ICommand
33 {
34 public:
36 
37  virtual bool cryptic() const = 0;
38  virtual void setCryptic(bool val) = 0;
40  virtual String file() const = 0;
42  virtual void setFile(const String &val) = 0;
44  virtual size_t argCount() const = 0;
45  virtual void setArgCount(size_t val) = 0;
46  virtual String arg(size_t index) const = 0;
47  virtual void setArg(size_t index, const String &val) = 0;
48 };
49 
51 class CommandPrivate;
53 class MCR_API Command : public ISignalMember, public ICommand
54 {
55  friend class CommandPrivate;
56 public:
57  Command(bool cryptic = false);
58  Command(const Command &copytron);
59  virtual ~Command() override;
60  Command &operator =(const Command &copytron);
61 
62  static void setKeyProvider(IKeyProvider *provider)
63  {
64  _keyProvider = provider;
65  }
66 
68  virtual bool cryptic() const override
69  {
70  return _cryptic;
71  }
73  virtual void setCryptic(bool val) override;
74 
76  virtual String file() const override
77  {
78  return _file.text();
79  }
81  virtual void setFile(const String &val) override
82  {
83  _file.setText(val);
84  }
85 
87  virtual size_t argCount() const override;
88  virtual void setArgCount(size_t count) override;
89  virtual String arg(size_t index) const override;
90  virtual void setArg(size_t index, const String &value) override;
91  inline void setArg(size_t index, const std::string &value)
92  {
93  setArg(index, String(value.c_str(), value.size()));
94  }
95 
101  template<class T>
102  inline T args() const
103  {
104  T ret;
105  auto ptr = argsArray();
106  size_t i, count = argCount();
107  ret.reserve(count);
108  for (i = 0; i < count; i++) {
109  ret.push_back(*ptr[i].text());
110  }
111  return ret;
112  }
118  template<class T>
119  inline void setArgs(const T &val)
120  {
121  size_t i = 0;
122  setArgCount(val.size());
123  for (auto &iter: val) {
124  setArg(i++, iter);
125  }
126  }
127 
129  virtual int compare(const IDataMember &rhs) const override
130  {
131  return compare(dynamic_cast<const Command &>(rhs));
132  }
134  virtual int compare(const Command &rhs) const;
138  virtual void copy(const IDataMember *copytron) override;
140  virtual const char *name() const override
141  {
142  return "Command";
143  }
144  virtual size_t addNameCount() const override
145  {
146  return 1;
147  }
148  virtual void addNames(const char **bufferOut,
149  size_t bufferLength) const override
150  {
151  if (bufferOut && bufferLength)
152  bufferOut[0] = "Cmd";
153  }
155  virtual void send(mcr_Signal *signalPt) override;
156 
157  inline void clear()
158  {
159  _file.clear();
160  setArgCount(0);
161  }
162 private:
163  static IKeyProvider *_keyProvider;
165  SafeString _file;
166  bool _cryptic;
167  /* Not exportable */
168  CommandPrivate *_private;
169 
170  SafeString *argsArray() const;
171 };
172 }
173 
174 #endif
ISignalData - Data type for signal instances C++.
virtual size_t addNameCount() const override
Definition: command.h:144
virtual void addNames(const char **bufferOut, size_t bufferLength) const override
Definition: command.h:148
void setArgs(const T &val)
Definition: command.h:119
virtual bool cryptic() const override
Definition: command.h:68
#define MCR_DECL_INTERFACE(className)
Definition: defines.h:434
virtual const char * name() const override
Definition: command.h:140
virtual String file() const override
Definition: command.h:76
Libmacro, by Jonathan Pelletier, New Paradigm Software. Alpha version.
Definition: classes.h:31
virtual int compare(const IDataMember &rhs) const override
Definition: command.h:129
virtual void setFile(const String &val) override
Definition: command.h:81
SafeString - A string that can be encrypted in memory IKeyProvider - Application-defined objects that...
T args() const
Definition: command.h:102