NamedDescriptor.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 by Ulrich Friedrich Klank <klank@in.tum.de>
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 3 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 
00019 /************************************************************************
00020                        NamedDescriptor.h - Copyright klank
00021 
00022 **************************************************************************/
00023 
00024 
00025 #ifndef NAMEDDESCRIPTOR_H
00026 #define NAMEDDESCRIPTOR_H
00027 
00028 #include <string>
00029 #include <ros/ros.h>
00030 #include <sstream>
00031 #include <Descriptor.h>
00032 
00033 
00034 namespace cop
00035 {
00036 
00037   template <class Content>  class NamedDescriptor: public Descriptor
00038   {
00039   public:
00040     NamedDescriptor(const char* Name, const char* ClassName, const ElemType_t DescriptorType) :
00041       m_stXMLName(Name),
00042       m_stClassName(ClassName),
00043       m_elemType(DescriptorType)
00044      {
00045        m_class = new Class();
00046        m_class->SetName(m_stClassName);
00047      }
00048      std::string BuildFileName(){
00049         std::ostringstream os;
00050         os << GetNodeName().c_str() << "_" <<m_ID << ".msg";
00051         std::string filename = os.str();
00052         return filename;
00053      }
00054      void SetClass(std::string name)
00055      {
00056        delete m_class;
00057        m_stClassName = name;
00058        m_class = new Class();
00059        m_class->SetName(m_stClassName);
00060      }
00061      std::string GetNodeName() const{return m_stXMLName;}
00062      ElemType_t GetType() const {return m_elemType;}
00063 
00064      virtual void ContentToFileFunction(std::string filename)
00065      {
00066         uint32_t length = m_content.serializationLength();
00067         uint8_t* write_pointer = new uint8_t[length];
00068         uint8_t seq = 0, *outp;
00069         outp = m_content.serialize(write_pointer, seq);
00070         FILE* file = fopen(filename.c_str(), "w");
00071         if(file != NULL)
00072         {
00073           fwrite(write_pointer, 1, length, file);
00074           fclose(file);
00075         }
00076         else
00077         {
00078           ROS_ERROR("Could not write message to file (filename: %s in Class %s)\n", filename.c_str(), GetNodeName().c_str());
00079         }
00080         delete write_pointer;
00081      };
00082      virtual void FileToContentFunction(std::string filename)
00083      {
00084        if(filename.length() > 0)
00085        {
00086           FILE* file = fopen(filename.c_str(), "r");
00087           if (file==NULL)
00088           {
00089             ROS_ERROR("Could not read message from file (filename: %s in Class %s)\n", filename.c_str(), GetNodeName().c_str());
00090             throw("Error reading class");
00091           }
00092 
00093           // obtain file size:
00094           fseek (file, 0 , SEEK_END);
00095           long lSize = ftell (file);
00096           rewind (file);
00097           uint8_t*  buffer = new uint8_t[lSize];
00098           if (buffer == NULL)
00099           {
00100             ROS_ERROR("Could not read message from file (filename: %s in Class %s)\n", filename.c_str(), GetNodeName().c_str());
00101             throw("Error reading class");
00102           }
00103 
00104           // copy the file into the buffer:
00105           long result = fread (buffer,1,lSize,file);
00106           if (result != lSize)
00107           {
00108             ROS_ERROR("Could not read message from file (filename: %s in Class %s)\n", filename.c_str(), GetNodeName().c_str());
00109             delete buffer;
00110             throw("Error reading class");
00111           }
00112           m_content.deserialize(buffer);
00113           fclose (file);
00114           delete buffer;
00115        }
00116      }
00117      void SetContent(Content cont){m_content = cont;};
00118      Content GetContent() { return m_content;}
00119      std::string m_stXMLName;
00120      std::string m_stClassName;
00121      ElemType_t m_elemType;
00122      Content m_content;
00123   };
00124 
00125 #define CREATE_NAMED_DESCRIPTOR(DescriptorName, XMLDescriptorName, ClassName, DescriptorType, Content) \
00126   class DescriptorName : public NamedDescriptor<Content>                                               \
00127   {                                                                                                    \
00128   public:                                                                                              \
00129     DescriptorName() : NamedDescriptor<Content>(XMLDescriptorName, ClassName, DescriptorType){}        \
00130     void SaveTo(XMLTag* tag)                                                                           \
00131     {                                                                                                  \
00132         Descriptor::SaveTo(tag);                                                                       \
00133         try                                                                                            \
00134         {                                                                                              \
00135            std::string filename = BuildFileName();                                                      \
00136            printf("Trying to save to %s\n", filename.c_str());                                     \
00137            tag->AddProperty(XML_ATTRIBUTE_FILENAME, filename);                                          \
00138           ContentToFileFunction(filename);                                                    \
00139         }                                                                                              \
00140         catch(...)                                                                                     \
00141         {                                                                                              \
00142           ROS_ERROR("%s:Failed to save data\n", GetNodeName().c_str());                                \
00143         }                                                                                              \
00144      }                                                                                                 \
00145   protected:                                                                                           \
00146     void SetData(XMLTag* tag)                                                                          \
00147     {                                                                                                  \
00148         Descriptor::SetData(tag);                                                                      \
00149         FileToContentFunction(tag->GetProperty(XML_ATTRIBUTE_FILENAME));                             \
00150     }                                                                                                 \
00151   };                                  \
00152 
00153 #define CREATE_NAMED_DESCRIPTOR_SHOW(DescriptorName, XMLDescriptorName, ClassName, DescriptorType, Content) \
00154   class DescriptorName : public NamedDescriptor<Content>                                               \
00155   {                                                                                                    \
00156   public:                                                                                              \
00157     DescriptorName() : NamedDescriptor<Content>(XMLDescriptorName, ClassName, DescriptorType){}        \
00158     void SaveTo(XMLTag* tag)                                                                           \
00159     {                                                                                                  \
00160         Descriptor::SaveTo(tag);                                                                       \
00161         try                                                                                            \
00162         {                                                                                              \
00163            std::string filename = BuildFileName();                                                      \
00164            printf("Trying to save to %s\n", filename.c_str());                                     \
00165            tag->AddProperty(XML_ATTRIBUTE_FILENAME, filename);                                          \
00166           ContentToFileFunction(filename);                                                    \
00167         }                                                                                              \
00168         catch(...)                                                                                     \
00169         {                                                                                              \
00170           ROS_ERROR("%s:Failed to save data\n", GetNodeName().c_str());                                \
00171         }                                                                                              \
00172      }                                                                                                 \
00173      void Show(RelPose* pose, Sensor* sens);                                                           \
00174   protected:                                                                                           \
00175     void SetData(XMLTag* tag)                                                                          \
00176     {                                                                                                  \
00177         Descriptor::SetData(tag);                                                                      \
00178         FileToContentFunction(tag->GetProperty(XML_ATTRIBUTE_FILENAME));                             \
00179     }                                                                                                 \
00180   };                                  \
00181 
00182 }
00183 #endif // NAMEDDESCRIPTOR_H
00184 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


cognitive_perception
Author(s): Ulrich F Klank
autogenerated on Thu May 23 2013 07:38:34