00001 // Torc - Copyright 2011 University of Southern California. All Rights Reserved. 00002 // $HeadURL: https://svn.east.isi.edu/torc/trunk/src/torc/physical/Named.hpp $ 00003 // $Id: Named.hpp 523 2011-06-25 00:54:11Z nsteiner $ 00004 00005 // This program is free software: you can redistribute it and/or modify it under the terms of the 00006 // GNU General Public License as published by the Free Software Foundation, either version 3 of the 00007 // License, or (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 00010 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 00011 // the GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License along with this program. If 00014 // not, see <http://www.gnu.org/licenses/>. 00015 00016 /// \file 00017 /// \brief Header for the Named class. 00018 00019 #ifndef TORC_PHYSICAL_NAMED_HPP 00020 #define TORC_PHYSICAL_NAMED_HPP 00021 00022 #include <string> 00023 #include <boost/smart_ptr.hpp> 00024 00025 namespace torc { 00026 namespace physical { 00027 00028 /// \brief Concept for any object that can be named. 00029 /// \details No support is provided at this level for renaming, because many subclasses may 00030 /// may require oversight from their parent to avoid name collisions. 00031 /// \todo Add a setName() accessor to support renaming. For many design elements, this may 00032 /// require permission from the parent, to avoid name collisions. We could make the 00033 /// function virtual, but that would come at the cost of a vtable, and that's very 00034 /// expensive for things like Config objects. In general, the renaming should probably be 00035 /// managed by the Progeny<T> template. 00036 class Named { 00037 protected: 00038 // types 00039 /// \brief Imported type name. 00040 typedef std::string string; 00041 // members 00042 /// \brief The name of the object. 00043 string mName; 00044 public: 00045 // constructors 00046 /// \brief Constructor which must specify the object name. 00047 /// \param inName The object name. 00048 Named(const string& inName) : mName(inName) {} 00049 // accessors 00050 /// \brief Returns the object name. 00051 const string& getName(void) const { return mName; } 00052 // operators 00053 /// \brief Equality operator. 00054 bool operator ==(const Named& rhs) const { return mName == rhs.mName; } 00055 }; 00056 00057 /// \brief Shared pointer encapsulation of a Named object. 00058 typedef boost::shared_ptr<Named> NamedSharedPtr; 00059 00060 /// \brief Comparator class to serve as a predicate when searching for names. 00061 class NameComparator { 00062 protected: 00063 // types 00064 /// \brief Imported type name. 00065 typedef std::string string; 00066 // members 00067 /// \brief The name to compare against. 00068 const string& mName; 00069 public: 00070 // constructors 00071 /// \brief Constructor. 00072 /// \param inName The name to compare against. 00073 NameComparator(const string& inName) : mName(inName) {} 00074 // operators 00075 /// \brief Function operator taking a Named object. 00076 /// \returns true if the given object's name matches the desired name, or false 00077 /// otherwise. 00078 bool operator() (const Named& inNamed) const { return inNamed.getName() == mName; } 00079 /// \brief Function operator taking a Named shared pointer. 00080 /// \returns true if the given object's name matches the desired name, or false 00081 /// otherwise. 00082 bool operator() (const NamedSharedPtr& inNamedPtr) const 00083 { return inNamedPtr->getName() == mName; } 00084 }; 00085 00086 } // namespace physical 00087 } // namespace torc 00088 00089 #endif // TORC_PHYSICAL_NAMED_HPP