16b3a42afSjmmv // Copyright 2011 Google Inc. 26b3a42afSjmmv // All rights reserved. 36b3a42afSjmmv // 46b3a42afSjmmv // Redistribution and use in source and binary forms, with or without 56b3a42afSjmmv // modification, are permitted provided that the following conditions are 66b3a42afSjmmv // met: 76b3a42afSjmmv // 86b3a42afSjmmv // * Redistributions of source code must retain the above copyright 96b3a42afSjmmv // notice, this list of conditions and the following disclaimer. 106b3a42afSjmmv // * Redistributions in binary form must reproduce the above copyright 116b3a42afSjmmv // notice, this list of conditions and the following disclaimer in the 126b3a42afSjmmv // documentation and/or other materials provided with the distribution. 136b3a42afSjmmv // * Neither the name of Google Inc. nor the names of its contributors 146b3a42afSjmmv // may be used to endorse or promote products derived from this software 156b3a42afSjmmv // without specific prior written permission. 166b3a42afSjmmv // 176b3a42afSjmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 186b3a42afSjmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 196b3a42afSjmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 206b3a42afSjmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 216b3a42afSjmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 226b3a42afSjmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 236b3a42afSjmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 246b3a42afSjmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 256b3a42afSjmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 266b3a42afSjmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 276b3a42afSjmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 286b3a42afSjmmv 296b3a42afSjmmv /// \file utils/cmdline/commands_map.hpp 306b3a42afSjmmv /// Maintains a collection of dynamically-instantiated commands. 316b3a42afSjmmv /// 326b3a42afSjmmv /// Commands need to be dynamically-instantiated because they are often 336b3a42afSjmmv /// complex data structures. Instantiating them as static variables causes 346b3a42afSjmmv /// problems with the order of construction of globals. The commands_map class 356b3a42afSjmmv /// provided by this module provides a mechanism to maintain these instantiated 366b3a42afSjmmv /// objects. 376b3a42afSjmmv 386b3a42afSjmmv #if !defined(UTILS_CMDLINE_COMMANDS_MAP_HPP) 396b3a42afSjmmv #define UTILS_CMDLINE_COMMANDS_MAP_HPP 406b3a42afSjmmv 416b3a42afSjmmv #include <map> 426b3a42afSjmmv #include <memory> 436b3a42afSjmmv #include <set> 446b3a42afSjmmv #include <string> 456b3a42afSjmmv 466b3a42afSjmmv #include "utils/noncopyable.hpp" 476b3a42afSjmmv 486b3a42afSjmmv 496b3a42afSjmmv namespace utils { 506b3a42afSjmmv namespace cmdline { 516b3a42afSjmmv 526b3a42afSjmmv 536b3a42afSjmmv /// Collection of dynamically-instantiated commands. 546b3a42afSjmmv template< typename BaseCommand > 556b3a42afSjmmv class commands_map : noncopyable { 566b3a42afSjmmv /// Map of command names to their implementations. 576b3a42afSjmmv typedef std::map< std::string, BaseCommand* > impl_map; 586b3a42afSjmmv 596b3a42afSjmmv /// Map of category names to the command names they contain. 606b3a42afSjmmv typedef std::map< std::string, std::set< std::string > > categories_map; 616b3a42afSjmmv 626b3a42afSjmmv /// Collection of all available commands. 636b3a42afSjmmv impl_map _commands; 646b3a42afSjmmv 656b3a42afSjmmv /// Collection of defined categories and their commands. 666b3a42afSjmmv categories_map _categories; 676b3a42afSjmmv 686b3a42afSjmmv public: 696b3a42afSjmmv commands_map(void); 706b3a42afSjmmv ~commands_map(void); 716b3a42afSjmmv 726b3a42afSjmmv /// Scoped, strictly-owned pointer to a command from this map. 73*46b85cbbSlukem typedef typename std::unique_ptr< BaseCommand > command_ptr; 746b3a42afSjmmv void insert(command_ptr, const std::string& = ""); 756b3a42afSjmmv void insert(BaseCommand*, const std::string& = ""); 766b3a42afSjmmv 776b3a42afSjmmv /// Type for a constant iterator. 786b3a42afSjmmv typedef typename categories_map::const_iterator const_iterator; 796b3a42afSjmmv 806b3a42afSjmmv bool empty(void) const; 816b3a42afSjmmv 826b3a42afSjmmv const_iterator begin(void) const; 836b3a42afSjmmv const_iterator end(void) const; 846b3a42afSjmmv 856b3a42afSjmmv BaseCommand* find(const std::string&); 866b3a42afSjmmv const BaseCommand* find(const std::string&) const; 876b3a42afSjmmv }; 886b3a42afSjmmv 896b3a42afSjmmv 906b3a42afSjmmv } // namespace cmdline 916b3a42afSjmmv } // namespace utils 926b3a42afSjmmv 936b3a42afSjmmv 946b3a42afSjmmv #endif // !defined(UTILS_CMDLINE_BASE_COMMAND_HPP) 95