accessor.h
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2012, Johannes Meyer, TU Darmstadt
3 // All rights reserved.
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Flight Systems and Automatic Control group,
13 // TU Darmstadt, nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without
15 // specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //=================================================================================================
28 
29 #ifndef CPP_INTROSPECTION_ACCESSOR_H
30 #define CPP_INTROSPECTION_ACCESSOR_H
31 
33 #include <introspection/field.h>
35 
36 #include <boost/type_traits.hpp>
37 #include <boost/utility/enable_if.hpp>
38 #include <boost/mpl/if.hpp>
39 
40 namespace cpp_introspection {
41 
42  class AccessorBase {};
43 
44  template <typename T>
45  class Accessor : public AccessorBase, public MessageTemplate<typename boost::remove_const<T>::type> {
46  public:
49 
50  private:
51  const Message& message_;
55 
56  public:
57  Accessor(const Message& message, const boost::shared_ptr<T>& instance)
58  : message_(message)
59  , instance_(instance)
60  {
61  fields_.reserve(message.size());
62  for(Message::const_iterator field = message.begin(); field != message.end(); ++field) {
63  FieldPtr access = (*field)->access(*this);
64  fields_.push_back(access);
65  fields_by_name_[access->getName()] = access;
66  }
67  }
68  virtual ~Accessor() {}
69 
70  bool hasInstance() const { return instance_.get(); }
71  VoidPtr getInstance() const { return is_const::value ? VoidPtr() : boost::const_pointer_cast<void>(getConstInstance()); }
72  VoidConstPtr getConstInstance() const { return boost::shared_static_cast<void const>(instance_); }
73 
74  T& instance() const { return *instance_; }
75 
76  PackagePtr package() const { return message_.package(); }
77  const char* getPackageName() const { return message_.getPackageName(); }
78 
79  const char *getName() const { return message_.getName(); }
80 
81 // const Package& package() const { return message_.package(); }
82  const V_Field& fields() const { return fields_; }
83  FieldWPtr field(const std::string& name) const { return fields_by_name_.at(name); }
84  const V_FieldName& getFieldNames() const { return message_.getFieldNames(); }
85 
86  void serialize(ros::serialization::OStream& stream, const VoidConstPtr&) const { message_.serialize(stream, getConstInstance()); }
87  ros::SerializedMessage serialize(const VoidConstPtr&) const { return message_.serialize(getConstInstance()); }
88  std::size_t serializationLength(const VoidConstPtr&) const { return message_.serializationLength(getConstInstance()); }
89  VoidPtr deserialize(ros::serialization::IStream& stream, const VoidPtr&) const { return is_const::value ? VoidPtr() : message_.deserialize(stream, getInstance()); }
90 
91 
92  private:
93  template <typename FieldType>
94  class FieldAccess : public FieldType {
95  private:
98  typedef typename boost::mpl::if_<boost::is_const<T>, const typename FieldType::field_type, typename FieldType::field_type>::type field_type;
99  typedef typename boost::mpl::if_<boost::is_const<T>, const typename FieldType::value_type, typename FieldType::value_type>::type value_type;
100 
101  public:
102  FieldAccess(const FieldType& field, const Accessor& accessor) : FieldType(field), accessor_(accessor), expanded_(size()) {}
103  virtual ~FieldAccess() {}
104 
105  const Accessor& accessor() const { return accessor_; }
106 
107  using FieldType::reference;
108  field_type& reference() const { return reference(accessor().instance()); }
109  value_type& reference(std::size_t i) const { return reference(accessor().instance(), i); }
110  bool hasInstance() const { return true; }
111 
112  // size() should return the size of the vector in the given instance
113  std::size_t size() const { return field_traits::size<typename FieldType::field_type>::value(reference()); }
115  void resize(std::size_t new_size) const { field_traits::size<typename FieldType::field_type>::resize(reference(), new_size); expanded_.resize(size()); }
116  std::size_t capacity() const { return field_traits::size<typename FieldType::field_type>::capacity(reference()); }
117 
118  boost::any get(std::size_t i) const { return boost::any(reference(i)); }
119  void setAny(const boost::any& value, std::size_t i) const { reference(i) = boost::any_cast<typename FieldType::value_type>(value); }
120 
121  MessagePtr expand(std::size_t i) const {
122  if (i >= expanded_.size()) expanded_.resize(size());
123  if (i >= expanded_.size()) return MessagePtr();
124  if (expanded_[i]) return expanded_[i];
125 
126  MessagePtr message(messageByTypeId(this->getTypeId()));
127  if (!message) {
128  ROS_WARN_NAMED("cpp_introspection", "failed to expand field %s of type %s (unknown type)", FieldType::getName(), FieldType::getDataType());
129  return MessagePtr();
130  }
131  expanded_[i] = message->introspect(&reference(i));
132  return expanded_[i];
133  }
134  };
135 
136  public:
137  template <class FieldType> static FieldPtr access(const FieldType& field, AccessorBase& accessor) {
138  return FieldPtr(new FieldAccess<FieldType>(field, static_cast<Accessor&>(accessor)));
139  }
140  };
141 
142  namespace {
143  struct null_deleter { void operator()(void const *) const { } };
144  }
145 
146  template <typename T>
148  {
149  return introspect(VoidPtr(instance, null_deleter()));
150  }
151 
152  template <typename T>
154  {
155  boost::shared_ptr<T> x(boost::shared_static_cast<T>(instance));
156  if (!x) return MessagePtr();
157  return MessagePtr(new Accessor<T>(*this, x));
158  }
159 
160  template <typename T>
161  MessagePtr MessageTemplate<T>::introspect(void const *instance) const
162  {
163  return introspect(VoidConstPtr(instance, null_deleter()));
164  }
165 
166  template <typename T>
168  {
169  boost::shared_ptr<T const> x(boost::shared_static_cast<T const>(instance));
170  if (!x) return MessagePtr();
171  return MessagePtr(new Accessor<T const>(*this, x));
172  }
173 
174 } // namespace cpp_introspection
175 
176 #endif // CPP_INTROSPECTION_ACCESSOR_H
void resize(std::size_t new_size) const
Definition: accessor.h:115
boost::mpl::if_< boost::is_const< T >, const typename FieldType::value_type, typename FieldType::value_type >::type value_type
Definition: accessor.h:99
std::vector< MessagePtr > V_Message
Definition: forwards.h:56
std::vector< FieldPtr > V_Field
Definition: forwards.h:57
virtual const char * getName() const =0
VoidPtr deserialize(ros::serialization::IStream &stream, const VoidPtr &) const
Definition: accessor.h:89
boost::weak_ptr< Field const > FieldWPtr
Definition: forwards.h:51
std::map< std::string, FieldWPtr > M_Field
Definition: forwards.h:63
#define ROS_WARN_NAMED(name,...)
bool hasInstance() const
Definition: accessor.h:70
virtual std::size_t serializationLength(const VoidConstPtr &instance=VoidConstPtr()) const =0
virtual PackagePtr package() const =0
virtual MessagePtr introspect() const
Definition: message.h:95
const char * getPackageName() const
Definition: accessor.h:77
const V_Field & fields() const
Definition: accessor.h:82
boost::is_const< T >::type is_const
Definition: accessor.h:48
const_iterator end() const
Definition: message.h:103
ros::SerializedMessage serialize(const VoidConstPtr &) const
Definition: accessor.h:87
FieldAccess(const FieldType &field, const Accessor &accessor)
Definition: accessor.h:102
std::size_t serializationLength(const VoidConstPtr &) const
Definition: accessor.h:88
boost::shared_ptr< void > VoidPtr
Definition: forwards.h:69
TypePtr type(const std::string &name)
Definition: types.cpp:82
MessagePtr messageByTypeId(const std::type_info &type_info)
const Message & message_
Definition: accessor.h:51
MessagePtr introspect(T &instance)
Definition: introspection.h:42
V_Field::const_iterator const_iterator
Definition: message.h:101
boost::shared_ptr< T > instance_
Definition: accessor.h:52
static void resize(T &x, size_t new_size, const T &initial=T())
Definition: field_traits.h:52
boost::shared_ptr< Field const > FieldPtr
Definition: forwards.h:50
virtual void serialize(ros::serialization::OStream &stream, const VoidConstPtr &instance=VoidConstPtr()) const =0
boost::shared_ptr< void const > VoidConstPtr
Definition: forwards.h:71
const_iterator begin() const
Definition: message.h:102
const char * getName() const
Definition: accessor.h:79
std::size_t size() const
Definition: message.h:104
Accessor(const Message &message, const boost::shared_ptr< T > &instance)
Definition: accessor.h:57
void setAny(const boost::any &value, std::size_t i) const
Definition: accessor.h:119
void serialize(ros::serialization::OStream &stream, const VoidConstPtr &) const
Definition: accessor.h:86
FieldWPtr field(const std::string &name) const
Definition: accessor.h:83
static FieldPtr access(const FieldType &field, AccessorBase &accessor)
Definition: accessor.h:137
virtual const V_FieldName & getFieldNames() const =0
MessagePtr expand(std::size_t i) const
Definition: accessor.h:121
boost::mpl::if_< boost::is_const< T >, const typename FieldType::field_type, typename FieldType::field_type >::type field_type
Definition: accessor.h:98
const V_FieldName & getFieldNames() const
Definition: accessor.h:84
VoidConstPtr getConstInstance() const
Definition: accessor.h:72
boost::shared_ptr< Message const > MessagePtr
Definition: forwards.h:48
virtual const char * getPackageName() const =0
virtual VoidPtr deserialize(ros::serialization::IStream &stream, const VoidPtr &instance=VoidPtr()) const =0
PackagePtr package() const
Definition: accessor.h:76
value_type & reference(std::size_t i) const
Definition: accessor.h:109
VoidPtr getInstance() const
Definition: accessor.h:71
std::vector< const char * > V_FieldName
Definition: forwards.h:58
const Accessor & accessor() const
Definition: accessor.h:105


cpp_introspection
Author(s): Johannes Meyer
autogenerated on Mon Jun 10 2019 12:56:17