Path: blob/main/contrib/kyua/utils/cmdline/commands_map.hpp
48180 views
// Copyright 2011 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728/// \file utils/cmdline/commands_map.hpp29/// Maintains a collection of dynamically-instantiated commands.30///31/// Commands need to be dynamically-instantiated because they are often32/// complex data structures. Instantiating them as static variables causes33/// problems with the order of construction of globals. The commands_map class34/// provided by this module provides a mechanism to maintain these instantiated35/// objects.3637#if !defined(UTILS_CMDLINE_COMMANDS_MAP_HPP)38#define UTILS_CMDLINE_COMMANDS_MAP_HPP3940#include "utils/cmdline/commands_map_fwd.hpp"4142#include <map>43#include <memory>44#include <set>45#include <string>4647#include "utils/noncopyable.hpp"484950namespace utils {51namespace cmdline {525354/// Collection of dynamically-instantiated commands.55template< typename BaseCommand >56class commands_map : noncopyable {57/// Map of command names to their implementations.58typedef std::map< std::string, BaseCommand* > impl_map;5960/// Map of category names to the command names they contain.61typedef std::map< std::string, std::set< std::string > > categories_map;6263/// Collection of all available commands.64impl_map _commands;6566/// Collection of defined categories and their commands.67categories_map _categories;6869public:70commands_map(void);71~commands_map(void);7273/// Scoped, strictly-owned pointer to a command from this map.74typedef typename std::unique_ptr< BaseCommand > command_ptr;75void insert(command_ptr, const std::string& = "");76void insert(BaseCommand*, const std::string& = "");7778/// Type for a constant iterator.79typedef typename categories_map::const_iterator const_iterator;8081bool empty(void) const;8283const_iterator begin(void) const;84const_iterator end(void) const;8586BaseCommand* find(const std::string&);87const BaseCommand* find(const std::string&) const;88};899091} // namespace cmdline92} // namespace utils939495#endif // !defined(UTILS_CMDLINE_BASE_COMMAND_HPP)969798