1 // Copyright 2011 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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.
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.
29 #include "utils/cmdline/commands_map.hpp"
30 #include "utils/sanity.hpp"
36 /// Constructs an empty set of commands.
37 template< typename BaseCommand >
38 cmdline::commands_map< BaseCommand >::commands_map(void)
43 /// Destroys a set of commands.
45 /// This releases the dynamically-instantiated objects.
46 template< typename BaseCommand >
47 cmdline::commands_map< BaseCommand >::~commands_map(void)
49 for (typename impl_map::iterator iter = _commands.begin();
50 iter != _commands.end(); iter++)
51 delete (*iter).second;
55 /// Inserts a new command into the map.
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.
62 template< typename BaseCommand >
64 cmdline::commands_map< BaseCommand >::insert(command_ptr command,
65 const std::string& category)
67 INV(_commands.find(command->name()) == _commands.end());
68 BaseCommand* ptr = command.release();
70 _commands[ptr->name()] = ptr;
71 _categories[category].insert(ptr->name());
75 /// Inserts a new command into the map.
77 /// This grabs ownership of the pointer, so it is ONLY safe to use with the
78 /// following idiom: insert(new foo()).
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.
85 template< typename BaseCommand >
87 cmdline::commands_map< BaseCommand >::insert(BaseCommand* command,
88 const std::string& category)
90 insert(command_ptr(command), category);
94 /// Checks whether the list of commands is empty.
96 /// \return True if there are no commands in this map.
97 template< typename BaseCommand >
99 cmdline::commands_map< BaseCommand >::empty(void) const
101 return _commands.empty();
105 /// Returns a constant iterator to the beginning of the categories mapping.
107 /// \return A map (string -> BaseCommand*) iterator.
108 template< typename BaseCommand >
109 typename cmdline::commands_map< BaseCommand >::const_iterator
110 cmdline::commands_map< BaseCommand >::begin(void) const
112 return _categories.begin();
116 /// Returns a constant iterator to the end of the categories mapping.
118 /// \return A map (string -> BaseCommand*) iterator.
119 template< typename BaseCommand >
120 typename cmdline::commands_map< BaseCommand >::const_iterator
121 cmdline::commands_map< BaseCommand >::end(void) const
123 return _categories.end();
127 /// Finds a command by name; mutable version.
129 /// \param name The name of the command to locate.
131 /// \return The command itself or NULL if it does not exist.
132 template< typename BaseCommand >
134 cmdline::commands_map< BaseCommand >::find(const std::string& name)
136 typename impl_map::iterator iter = _commands.find(name);
137 if (iter == _commands.end())
140 return (*iter).second;
144 /// Finds a command by name; constant version.
146 /// \param name The name of the command to locate.
148 /// \return The command itself or NULL if it does not exist.
149 template< typename BaseCommand >
151 cmdline::commands_map< BaseCommand >::find(const std::string& name) const
153 typename impl_map::const_iterator iter = _commands.find(name);
154 if (iter == _commands.end())
157 return (*iter).second;