conversion.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 ROSMATLAB_CONVERSION_H
00030 #define ROSMATLAB_CONVERSION_H
00031 
00032 #include <introspection/forwards.h>
00033 #include <matrix.h>
00034 
00035 #include <rosmatlab/options.h>
00036 
00037 #include <ros/time.h>
00038 
00039 namespace rosmatlab {
00040 
00041 using namespace cpp_introspection;
00042 
00043 class Conversion;
00044 typedef boost::shared_ptr<Conversion> ConversionPtr;
00045 
00046 typedef mxArray *Array;
00047 typedef mxArray const *ConstArray;
00048 
00049 class ConversionOptions : public Options {
00050 public:
00051   ConversionOptions();
00052   ConversionOptions(int nrhs, const mxArray *prhs[]);
00053   virtual ~ConversionOptions();
00054 
00055   virtual void init(int nrhs, const mxArray *prhs[]);
00056   virtual mxArray *toMatlab() const;
00057 
00058   typedef enum { MATLAB_STRUCT, MATLAB_MATRIX, MATLAB_EXTENDED_STRUCT, MATLAB_TYPE_MAX } MatlabType;
00059   MatlabType conversionType() const;
00060   std::string conversionTypeString() const;
00061   ConversionOptions &setConversionType(MatlabType type);
00062 
00063   bool addMetaData() const;
00064   ConversionOptions &setAddMetaData(bool value);
00065 
00066   bool addConnectionHeader() const;
00067   ConversionOptions &setAddConnectionHeader(bool value);
00068 };
00069 
00070 class Conversion {
00071 public:
00072   Conversion(const MessagePtr &message);
00073   Conversion(const MessagePtr &message, const ConversionOptions& options);
00074   Conversion(const Conversion &other, const MessagePtr &message = MessagePtr());
00075   virtual ~Conversion();
00076 
00077   operator void *() const { return reinterpret_cast<void *>(static_cast<bool>(message_)); }
00078 
00079   virtual Array toMatlab();
00080   virtual Array toMatlab(Array target, std::size_t index = 0, std::size_t size = 0);
00081 
00082   virtual Array toDoubleMatrix();
00083   virtual Array toDoubleMatrix(Array target, std::size_t index = 0, std::size_t size = 0);
00084 
00085   virtual Array toStruct();
00086   virtual Array toStruct(Array target, std::size_t index = 0, std::size_t size = 0);
00087 
00088   virtual Array toExtendedStruct();
00089   virtual Array toExtendedStruct(Array target, std::size_t index = 0, std::size_t size = 0);
00090 
00091   virtual std::size_t numberOfInstances(ConstArray source);
00092   virtual MessagePtr fromMatlab(ConstArray source, std::size_t index = 0);
00093   virtual void fromMatlab(const MessagePtr &message, ConstArray source, std::size_t index = 0);
00094 
00095   virtual Array convertToMatlab(const FieldPtr& field);
00096   virtual void convertFromMatlab(const FieldPtr& field, ConstArray source);
00097   virtual const double *convertFromDouble(const FieldPtr& field, const double *begin, const double *end);
00098 
00099   const MessagePtr& expanded();
00100 
00101   Options &options() { return options_; }
00102   const Options &options() const { return options_; }
00103   Conversion &setOptions(int nrhs, const mxArray *prhs[]);
00104 
00105   static ConversionOptions &defaultOptions();
00106   static ConversionOptions &perMessageOptions(const MessagePtr& message);
00107 
00108 protected:
00109   virtual void fromDoubleMatrix(const MessagePtr &target, ConstArray source, std::size_t n = 0);
00110   virtual void fromDoubleMatrix(const MessagePtr &target, const double *begin, const double *end);
00111   virtual void fromStruct(const MessagePtr &target, ConstArray source, std::size_t index = 0);
00112 
00113   MessagePtr message_;
00114   MessagePtr expanded_;
00115 
00116   ConversionOptions options_;
00117   static std::map<const char *,ConversionOptions> per_message_options_;
00118 };
00119 
00120 /*
00121   Some conversion helpers
00122 */
00123 
00124 template <typename Time>
00125 static inline mxArray *mxCreateTime(const Time& time) {
00126   if (time == ros::TIME_MIN || time == ros::TIME_MAX) return mxCreateDoubleMatrix(0, 0, mxREAL);
00127   return ::mxCreateDoubleScalar(time.toSec());
00128 }
00129 
00130 template <typename Duration>
00131 static inline mxArray *mxCreateDuration(const Duration& duration) {
00132   return ::mxCreateDoubleScalar(duration.toSec());
00133 }
00134 
00135 static inline mxArray *mxCreateString(const std::string& string) {
00136   return ::mxCreateString(string.c_str());
00137 }
00138 
00139 static inline mxArray *mxCreateEmpty() {
00140   return ::mxCreateDoubleMatrix(0, 0, mxREAL);
00141 }
00142 
00143 }
00144 
00145 #endif // ROSMATLAB_CONVERSION_H


rosmatlab
Author(s): Johannes Meyer
autogenerated on Fri Jul 25 2014 06:08:36