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