interface_manager.h
Go to the documentation of this file.
00001 
00002 // Copyright (C) 2013, PAL Robotics S.L.
00003 // Copyright (C) 2012, hiDOF INC.
00004 //
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are met:
00007 //   * Redistributions of source code must retain the above copyright notice,
00008 //     this list of conditions and the following disclaimer.
00009 //   * Redistributions in binary form must reproduce the above copyright
00010 //     notice, this list of conditions and the following disclaimer in the
00011 //     documentation and/or other materials provided with the distribution.
00012 //   * Neither the name of hiDOF, Inc. nor the names of its
00013 //     contributors may be used to endorse or promote products derived from
00014 //     this software without specific prior written permission.
00015 //
00016 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00019 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00020 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00021 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00022 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00023 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00024 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00025 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026 // POSSIBILITY OF SUCH DAMAGE.
00028 
00030 
00031 #ifndef HARDWARE_INTERFACE_INTERFACE_MANAGER_H
00032 #define HARDWARE_INTERFACE_INTERFACE_MANAGER_H
00033 
00034 #include <map>
00035 #include <string>
00036 
00037 #include <ros/console.h>
00038 
00039 #include <hardware_interface/internal/demangle_symbol.h>
00040 
00041 namespace hardware_interface
00042 {
00043 
00044 class InterfaceManager
00045 {
00046 public:
00056   template<class T>
00057   void registerInterface(T* iface)
00058   {
00059     const std::string iface_name = internal::demangledTypeName<T>();
00060     if (interfaces_.find(iface_name) != interfaces_.end())
00061     {
00062       ROS_WARN_STREAM("Replacing previously registered interface '" << iface_name << "'.");
00063     }
00064     interfaces_[internal::demangledTypeName<T>()] = iface;
00065   }
00066 
00077   template<class T>
00078   T* get()
00079   {
00080     InterfaceMap::iterator it = interfaces_.find(internal::demangledTypeName<T>());
00081     if (it == interfaces_.end())
00082       return NULL;
00083 
00084     T* iface = static_cast<T*>(it->second);
00085     if (!iface)
00086     {
00087       ROS_ERROR_STREAM("Failed reconstructing type T = '" << internal::demangledTypeName<T>().c_str() <<
00088                        "'. This should never happen");
00089       return NULL;
00090     }
00091     return iface;
00092   }
00093 
00095   std::vector<std::string> getNames() const
00096   {
00097     std::vector<std::string> out;
00098     out.reserve(interfaces_.size());
00099     for(InterfaceMap::const_iterator it = interfaces_.begin(); it != interfaces_.end(); ++it)
00100     {
00101       out.push_back(it->first);
00102     }
00103     return out;
00104   }
00105 
00106 protected:
00107   typedef std::map<std::string, void*> InterfaceMap;
00108   InterfaceMap interfaces_;
00109 };
00110 
00111 } // namespace
00112 
00113 #endif // header guard


hardware_interface
Author(s): Wim Meeussen, Adolfo Rodriguez Tsouroukdissian
autogenerated on Thu Dec 1 2016 03:45:48