message_expansion.h
Go to the documentation of this file.
00001 //=================================================================================================
00002 // Copyright (c) 2012, Johannes Meyer, TU Darmstadt
00003 // All rights reserved.
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
00008 //       notice, 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 the Flight Systems and Automatic Control group,
00013 //       TU Darmstadt, nor the names of its contributors may be used to
00014 //       endorse or promote products derived from this software without
00015 //       specific prior written permission.
00016 
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00018 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //=================================================================================================
00028 
00029 #ifndef CPP_INTROSPECTION_MESSAGE_EXPANSION_H
00030 #define CPP_INTROSPECTION_MESSAGE_EXPANSION_H
00031 
00032 #include <introspection/message.h>
00033 
00034 namespace cpp_introspection {
00035 
00036 class MessageForwarder : public Message {
00037 protected:
00038   const MessagePtr& parent_;
00039 
00040 public:
00041   MessageForwarder(const MessagePtr& parent) : parent_(parent) {}
00042   virtual ~MessageForwarder() {}
00043 
00044   virtual const char* getName() const { return parent_->getName(); }
00045   virtual const char* getDataType() const { return parent_->getDataType(); }
00046   virtual const char* getMD5Sum() const { return parent_->getMD5Sum(); }
00047   virtual const char* getDefinition() const { return parent_->getDefinition(); }
00048   virtual const std::type_info& getTypeId() const { return parent_->getTypeId(); }
00049 
00050   virtual bool isMessage() const { return parent_->isMessage(); }
00051   virtual bool isSimple() const { return parent_->isSimple(); }
00052   virtual bool isFixedSize() const { return parent_->isFixedSize(); }
00053   virtual bool hasHeader() const { return parent_->hasHeader(); }
00054 
00055   virtual std_msgs::Header* getHeader(const VoidPtr& instance) const { return parent_->getHeader(instance); }
00056   virtual const std_msgs::Header* getHeader(const VoidConstPtr& instance) const { return parent_->getHeader(instance); }
00057   virtual std::string* getFrameId(const VoidPtr& instance) const { return parent_->getFrameId(instance); }
00058   virtual const std::string* getFrameId(const VoidConstPtr& instance) const { return parent_->getFrameId(instance); }
00059   virtual ros::Time* getTimeStamp(const VoidPtr& instance) const { return parent_->getTimeStamp(instance); }
00060   virtual const ros::Time* getTimeStamp(const VoidConstPtr& instance) const { return parent_->getTimeStamp(instance); }
00061 
00062   virtual VoidPtr createInstance() const { return parent_->createInstance(); }
00063   virtual void serialize(ros::serialization::OStream& stream, const VoidConstPtr& instance = VoidConstPtr()) const { parent_->serialize(stream, instance); }
00064   virtual ros::SerializedMessage serialize(const VoidConstPtr& instance = VoidConstPtr()) const { return parent_->serialize(instance); }
00065   virtual std::size_t serializationLength(const VoidConstPtr& instance = VoidConstPtr()) const { return parent_->serializationLength(instance); }
00066   virtual VoidPtr deserialize(ros::serialization::IStream& stream, const VoidPtr& instance = VoidPtr()) const { return parent_->deserialize(stream, instance); }
00067 
00068   virtual bool hasInstance() const { return parent_->hasInstance(); }
00069   virtual VoidPtr getInstance() const { return parent_->getInstance(); }
00070   virtual VoidConstPtr getConstInstance() const { return parent_->getConstInstance(); }
00071 
00072   virtual MessagePtr introspect(const VoidPtr& instance) const { return parent_->introspect(instance); }
00073   virtual MessagePtr introspect(void *instance) const { return parent_->introspect(instance); }
00074   virtual MessagePtr introspect(const VoidConstPtr& instance) const { return parent_->introspect(instance); }
00075   virtual MessagePtr introspect(void const *instance) const { return parent_->introspect(instance); }
00076 };
00077 
00078 class ExpandedMessage : public MessageForwarder {
00079 private:
00080   V_Field fields_;
00081   M_Field fields_by_name_;
00082   V_FieldName field_names_;
00083 
00084   std::string separator_;
00085   std::string prefix_;
00086 
00087 public:
00088   ExpandedMessage(const MessagePtr& parent, const std::string &separator = ".", const std::string &prefix = std::string())
00089     : MessageForwarder(parent)
00090     , separator_(separator)
00091     , prefix_(prefix)
00092   {
00093     expand(parent, prefix);
00094   }
00095   virtual ~ExpandedMessage() {}
00096 
00097   virtual const V_Field& fields() const { return fields_; }
00098   virtual FieldWPtr field(const std::string& name) const { return fields_by_name_.at(name); }
00099   virtual const V_FieldName& getFieldNames() const { return field_names_; }
00100 
00101   virtual MessagePtr introspect(void *instance) const { return MessagePtr(new ExpandedMessage(parent_->introspect(instance), separator_, prefix_)); }
00102   virtual MessagePtr introspect(void const *instance) const { return MessagePtr(new ExpandedMessage(parent_->introspect(instance), separator_, prefix_)); }
00103 
00104 private:
00105   void expand(const MessagePtr& message, const std::string& prefix);
00106 };
00107 
00108 class ExpandedField : public Field {
00109 private:
00110   const Field& parent_;
00111   std::size_t index_;
00112   std::string name_;
00113 
00114 public:
00115   ExpandedField(const Field& parent, std::string name = std::string(), std::size_t index = 0)
00116     : parent_(parent), index_(index), name_(!name.empty() ? name : parent_.getName())
00117   {}
00118   virtual ~ExpandedField() {}
00119 
00120   virtual const Message& message() const { return parent_.message(); }
00121 
00122   virtual const char* getName() const { return name_.c_str(); }
00123   virtual const char* getDataType() const { return parent_.getValueType(); }
00124   virtual const char* getValueType() const { return parent_.getValueType(); }
00125   virtual std::size_t getIndex() const { return parent_.getIndex(); }
00126   virtual const std::type_info& getTypeId() const { return parent_.getTypeId(); }
00127   virtual TypePtr getType() const { return parent_.getType(); }
00128 
00129   virtual bool isArray() const { return false; }
00130   virtual bool isVector() const { return false; }
00131   virtual bool isContainer() const { return false; }
00132   virtual bool isSimple() const { return parent_.isSimple(); }
00133   virtual bool isFixedSize() const { return parent_.isFixedSize(); }
00134   virtual bool isMessage() const { return parent_.isMessage(); }
00135 
00136   virtual bool hasInstance() const { return parent_.hasInstance(); }
00137   virtual boost::any get(std::size_t i = 0) const { return parent_.get(index_); }
00138   virtual void setAny(const boost::any& value, std::size_t i = 0) const { parent_.setAny(value, index_); }
00139 
00140   virtual FieldPtr access(AccessorBase& accessor) const { return FieldPtr(); }
00141 };
00142 
00143 } // namespace cpp_introspection
00144 
00145 #endif // CPP_INTROSPECTION_MESSAGE_EXPANSION_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends Defines


cpp_introspection
Author(s): Johannes Meyer
autogenerated on Tue Jan 8 2013 17:30:47