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