Interactive CLI 2.3
A cross-platform C++14 library for interactive command-line interfaces
Loading...
Searching...
No Matches
cmdhandler.h
Go to the documentation of this file.
1/*******************************************************************************
2 * CLI - A simple command line interface.
3 * Copyright (C) 2016-2026 Daniele Pallastrelli
4 *
5 * Boost Software License - Version 1.0 - August 17th, 2003
6 *
7 * Permission is hereby granted, free of charge, to any person or organization
8 * obtaining a copy of the software and accompanying documentation covered by
9 * this license (the "Software") to use, reproduce, display, distribute,
10 * execute, and transmit the Software, and to prepare derivative works of the
11 * Software, and to permit third-parties to whom the Software is furnished to
12 * do so, all subject to the following:
13 *
14 * The copyright notices in the Software and this entire statement, including
15 * the above license grant, this restriction and the following disclaimer,
16 * must be included in all copies of the Software, in whole or in part, and
17 * all derivative works of the Software, unless such copies or derivative
18 * works are solely in the form of machine-executable object code generated by
19 * a source language processor.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 ******************************************************************************/
29
30#ifndef CLI_CMDHANDLER_H_
31#define CLI_CMDHANDLER_H_
32
33#include <algorithm>
34#include <memory>
35#include <string>
36#include <vector>
37
38#include "command.h"
39
40namespace cli
41{
43 {
44 public:
45 using CmdVec = std::vector<std::shared_ptr<Command>>;
46
47 CmdHandler() : descriptor(std::make_shared<Descriptor>()) {}
48 CmdHandler(std::weak_ptr<Command> c, std::weak_ptr<CmdVec> v) :
49 descriptor(std::make_shared<Descriptor>(c, v))
50 {}
51
52 void Enable() { if (descriptor) descriptor->Enable(); }
53 void Disable() { if (descriptor) descriptor->Disable(); }
54 void Remove() { if (descriptor) descriptor->Remove(); }
55
56 private:
57 struct Descriptor
58 {
59 Descriptor() noexcept = default;
60 Descriptor(std::weak_ptr<Command> c, std::weak_ptr<CmdVec> v) noexcept :
61 cmd(std::move(c)), cmds(std::move(v))
62 {}
63
64 void Enable()
65 {
66 if (auto c = cmd.lock())
67 c->Enable();
68 }
69
70 void Disable()
71 {
72 if (auto c = cmd.lock())
73 c->Disable();
74 }
75
76 void Remove()
77 {
78 auto scmd = cmd.lock();
79 auto scmds = cmds.lock();
80 if (scmd && scmds)
81 {
82 auto i = std::find_if(
83 scmds->begin(),
84 scmds->end(),
85 [&](const auto& c) { return c.get() == scmd.get(); }
86 );
87 if (i != scmds->end())
88 scmds->erase(i);
89 }
90 }
91
92 std::weak_ptr<Command> cmd;
93 std::weak_ptr<CmdVec> cmds;
94 };
95
96 std::shared_ptr<Descriptor> descriptor;
97 };
98}
99
100#endif // CLI_CMDHANDLER_H_
std::vector< std::shared_ptr< Command > > CmdVec
Definition cmdhandler.h:45
CmdHandler(std::weak_ptr< Command > c, std::weak_ptr< CmdVec > v)
Definition cmdhandler.h:48