shape_shifter_factory.hpp
Go to the documentation of this file.
00001 #ifndef SHAPE_SHIFTER_FACTORY_HPP
00002 #define SHAPE_SHIFTER_FACTORY_HPP
00003 
00004 #include <ros_type_introspection/utils/shape_shifter.hpp>
00005 #include "PlotJuggler/any.hpp"
00006 
00007 
00008 class RosIntrospectionFactory{
00009 
00010 public:
00011   static RosIntrospectionFactory &get();
00012 
00013   static void registerMessage(const std::string& topic_name,
00014                               const std::string &md5sum,
00015                               const std::string& datatype,
00016                               const std::string& definition );
00017 
00018   static RosIntrospection::ShapeShifter* getShapeShifter(const std::string& topic_name);
00019 
00020   static std::vector<const std::string*> getTopicList();
00021 
00022   static RosIntrospection::Parser& parser()
00023   {
00024       return get()._parser;
00025   }
00026 
00027   static bool isRegistered(const std::string& topic_name);
00028 
00029   static void reset();
00030 
00031 private:
00032   RosIntrospectionFactory() = default;
00033   std::map<std::string, RosIntrospection::ShapeShifter> _ss_map;
00034   RosIntrospection::Parser _parser;
00035 
00036 };
00037 //---------------------------------------------
00038 
00039 inline RosIntrospectionFactory& RosIntrospectionFactory::get()
00040 {
00041   static RosIntrospectionFactory instance;
00042   return instance;
00043 }
00044 
00045 // return true if added
00046 inline void RosIntrospectionFactory::registerMessage(const std::string &topic_name,
00047                                                  const std::string &md5sum,
00048                                                  const std::string &datatype,
00049                                                  const std::string &definition)
00050 {
00051     auto& instance = get();
00052     auto it = instance._ss_map.find(topic_name);
00053     if( it == instance._ss_map.end() || it->second.getMD5Sum() != md5sum )
00054     {
00055         RosIntrospection::ShapeShifter msg;
00056         msg.morph(md5sum, datatype,definition);
00057         instance._ss_map.insert( std::make_pair(topic_name, std::move(msg) ));
00058         parser().registerMessageDefinition( topic_name, RosIntrospection::ROSType(datatype), definition);
00059     }
00060 }
00061 
00062 inline RosIntrospection::ShapeShifter* RosIntrospectionFactory::getShapeShifter(const std::string &topic_name)
00063 {
00064     auto& instance = get();
00065     auto it = instance._ss_map.find( topic_name );
00066     return ( it == instance._ss_map.end()) ? nullptr :  &(it->second);
00067 }
00068 
00069 inline std::vector<const std::string*> RosIntrospectionFactory::getTopicList()
00070 {
00071     std::vector<const std::string*> out;
00072     auto& instance = get();
00073     out.reserve( instance._ss_map.size() );
00074 
00075     for (const auto& ss: instance._ss_map)
00076     {
00077         out.push_back( &(ss.first) );
00078     }
00079     return out;
00080 }
00081 
00082 inline bool RosIntrospectionFactory::isRegistered(const std::string &topic_name)
00083 {
00084     return get()._ss_map.count(topic_name) != 0;
00085 }
00086 
00087 inline void RosIntrospectionFactory::reset()
00088 {
00089     get()._ss_map.clear();
00090 }
00091 
00092 #endif // SHAPE_SHIFTER_FACTORY_HPP
00093 
00094 
00095 


plotjuggler
Author(s): Davide Faconti
autogenerated on Wed Jul 3 2019 19:28:05