xref: /minix3/external/bsd/kyua-cli/dist/utils/cmdline/commands_map.ipp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc// Copyright 2011 Google Inc.
2*11be35a1SLionel Sambuc// All rights reserved.
3*11be35a1SLionel Sambuc//
4*11be35a1SLionel Sambuc// Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc// modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc// met:
7*11be35a1SLionel Sambuc//
8*11be35a1SLionel Sambuc// * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc//   notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc// * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc//   notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc//   documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc// * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc//   may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc//   without specific prior written permission.
16*11be35a1SLionel Sambuc//
17*11be35a1SLionel Sambuc// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc#include "utils/cmdline/commands_map.hpp"
30*11be35a1SLionel Sambuc#include "utils/sanity.hpp"
31*11be35a1SLionel Sambuc
32*11be35a1SLionel Sambuc
33*11be35a1SLionel Sambucnamespace utils {
34*11be35a1SLionel Sambuc
35*11be35a1SLionel Sambuc
36*11be35a1SLionel Sambuc/// Constructs an empty set of commands.
37*11be35a1SLionel Sambuctemplate< typename BaseCommand >
38*11be35a1SLionel Sambuccmdline::commands_map< BaseCommand >::commands_map(void)
39*11be35a1SLionel Sambuc{
40*11be35a1SLionel Sambuc}
41*11be35a1SLionel Sambuc
42*11be35a1SLionel Sambuc
43*11be35a1SLionel Sambuc/// Destroys a set of commands.
44*11be35a1SLionel Sambuc///
45*11be35a1SLionel Sambuc/// This releases the dynamically-instantiated objects.
46*11be35a1SLionel Sambuctemplate< typename BaseCommand >
47*11be35a1SLionel Sambuccmdline::commands_map< BaseCommand >::~commands_map(void)
48*11be35a1SLionel Sambuc{
49*11be35a1SLionel Sambuc    for (typename impl_map::iterator iter = _commands.begin();
50*11be35a1SLionel Sambuc         iter != _commands.end(); iter++)
51*11be35a1SLionel Sambuc        delete (*iter).second;
52*11be35a1SLionel Sambuc}
53*11be35a1SLionel Sambuc
54*11be35a1SLionel Sambuc
55*11be35a1SLionel Sambuc/// Inserts a new command into the map.
56*11be35a1SLionel Sambuc///
57*11be35a1SLionel Sambuc/// \param command The command to insert.  This must have been dynamically
58*11be35a1SLionel Sambuc///     allocated with new.  The call grabs ownership of the command, or the
59*11be35a1SLionel Sambuc///     command is freed if the call fails.
60*11be35a1SLionel Sambuc/// \param category The category this command belongs to.  Defaults to the empty
61*11be35a1SLionel Sambuc///     string, which indicates that the command has not be categorized.
62*11be35a1SLionel Sambuctemplate< typename BaseCommand >
63*11be35a1SLionel Sambucvoid
64*11be35a1SLionel Sambuccmdline::commands_map< BaseCommand >::insert(command_ptr command,
65*11be35a1SLionel Sambuc                                             const std::string& category)
66*11be35a1SLionel Sambuc{
67*11be35a1SLionel Sambuc    INV(_commands.find(command->name()) == _commands.end());
68*11be35a1SLionel Sambuc    BaseCommand* ptr = command.release();
69*11be35a1SLionel Sambuc    INV(ptr != NULL);
70*11be35a1SLionel Sambuc    _commands[ptr->name()] = ptr;
71*11be35a1SLionel Sambuc    _categories[category].insert(ptr->name());
72*11be35a1SLionel Sambuc}
73*11be35a1SLionel Sambuc
74*11be35a1SLionel Sambuc
75*11be35a1SLionel Sambuc/// Inserts a new command into the map.
76*11be35a1SLionel Sambuc///
77*11be35a1SLionel Sambuc/// This grabs ownership of the pointer, so it is ONLY safe to use with the
78*11be35a1SLionel Sambuc/// following idiom: insert(new foo()).
79*11be35a1SLionel Sambuc///
80*11be35a1SLionel Sambuc/// \param command The command to insert.  This must have been dynamically
81*11be35a1SLionel Sambuc///     allocated with new.  The call grabs ownership of the command, or the
82*11be35a1SLionel Sambuc///     command is freed if the call fails.
83*11be35a1SLionel Sambuc/// \param category The category this command belongs to.  Defaults to the empty
84*11be35a1SLionel Sambuc///     string, which indicates that the command has not be categorized.
85*11be35a1SLionel Sambuctemplate< typename BaseCommand >
86*11be35a1SLionel Sambucvoid
87*11be35a1SLionel Sambuccmdline::commands_map< BaseCommand >::insert(BaseCommand* command,
88*11be35a1SLionel Sambuc                                             const std::string& category)
89*11be35a1SLionel Sambuc{
90*11be35a1SLionel Sambuc    insert(command_ptr(command), category);
91*11be35a1SLionel Sambuc}
92*11be35a1SLionel Sambuc
93*11be35a1SLionel Sambuc
94*11be35a1SLionel Sambuc/// Checks whether the list of commands is empty.
95*11be35a1SLionel Sambuc///
96*11be35a1SLionel Sambuc/// \return True if there are no commands in this map.
97*11be35a1SLionel Sambuctemplate< typename BaseCommand >
98*11be35a1SLionel Sambucbool
99*11be35a1SLionel Sambuccmdline::commands_map< BaseCommand >::empty(void) const
100*11be35a1SLionel Sambuc{
101*11be35a1SLionel Sambuc    return _commands.empty();
102*11be35a1SLionel Sambuc}
103*11be35a1SLionel Sambuc
104*11be35a1SLionel Sambuc
105*11be35a1SLionel Sambuc/// Returns a constant iterator to the beginning of the categories mapping.
106*11be35a1SLionel Sambuc///
107*11be35a1SLionel Sambuc/// \return A map (string -> BaseCommand*) iterator.
108*11be35a1SLionel Sambuctemplate< typename BaseCommand >
109*11be35a1SLionel Sambuctypename cmdline::commands_map< BaseCommand >::const_iterator
110*11be35a1SLionel Sambuccmdline::commands_map< BaseCommand >::begin(void) const
111*11be35a1SLionel Sambuc{
112*11be35a1SLionel Sambuc    return _categories.begin();
113*11be35a1SLionel Sambuc}
114*11be35a1SLionel Sambuc
115*11be35a1SLionel Sambuc
116*11be35a1SLionel Sambuc/// Returns a constant iterator to the end of the categories mapping.
117*11be35a1SLionel Sambuc///
118*11be35a1SLionel Sambuc/// \return A map (string -> BaseCommand*) iterator.
119*11be35a1SLionel Sambuctemplate< typename BaseCommand >
120*11be35a1SLionel Sambuctypename cmdline::commands_map< BaseCommand >::const_iterator
121*11be35a1SLionel Sambuccmdline::commands_map< BaseCommand >::end(void) const
122*11be35a1SLionel Sambuc{
123*11be35a1SLionel Sambuc    return _categories.end();
124*11be35a1SLionel Sambuc}
125*11be35a1SLionel Sambuc
126*11be35a1SLionel Sambuc
127*11be35a1SLionel Sambuc/// Finds a command by name; mutable version.
128*11be35a1SLionel Sambuc///
129*11be35a1SLionel Sambuc/// \param name The name of the command to locate.
130*11be35a1SLionel Sambuc///
131*11be35a1SLionel Sambuc/// \return The command itself or NULL if it does not exist.
132*11be35a1SLionel Sambuctemplate< typename BaseCommand >
133*11be35a1SLionel SambucBaseCommand*
134*11be35a1SLionel Sambuccmdline::commands_map< BaseCommand >::find(const std::string& name)
135*11be35a1SLionel Sambuc{
136*11be35a1SLionel Sambuc    typename impl_map::iterator iter = _commands.find(name);
137*11be35a1SLionel Sambuc    if (iter == _commands.end())
138*11be35a1SLionel Sambuc        return NULL;
139*11be35a1SLionel Sambuc    else
140*11be35a1SLionel Sambuc        return (*iter).second;
141*11be35a1SLionel Sambuc}
142*11be35a1SLionel Sambuc
143*11be35a1SLionel Sambuc
144*11be35a1SLionel Sambuc/// Finds a command by name; constant version.
145*11be35a1SLionel Sambuc///
146*11be35a1SLionel Sambuc/// \param name The name of the command to locate.
147*11be35a1SLionel Sambuc///
148*11be35a1SLionel Sambuc/// \return The command itself or NULL if it does not exist.
149*11be35a1SLionel Sambuctemplate< typename BaseCommand >
150*11be35a1SLionel Sambucconst BaseCommand*
151*11be35a1SLionel Sambuccmdline::commands_map< BaseCommand >::find(const std::string& name) const
152*11be35a1SLionel Sambuc{
153*11be35a1SLionel Sambuc    typename impl_map::const_iterator iter = _commands.find(name);
154*11be35a1SLionel Sambuc    if (iter == _commands.end())
155*11be35a1SLionel Sambuc        return NULL;
156*11be35a1SLionel Sambuc    else
157*11be35a1SLionel Sambuc        return (*iter).second;
158*11be35a1SLionel Sambuc}
159*11be35a1SLionel Sambuc
160*11be35a1SLionel Sambuc
161*11be35a1SLionel Sambuc}  // namespace utils
162