Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <rosmatlab/message.h>
00030 #include <rosmatlab/conversion.h>
00031 #include <rosmatlab/options.h>
00032 #include <rosmatlab/log.h>
00033 #include <rosmatlab/exception.h>
00034
00035 #include <introspection/message.h>
00036
00037 #include <mex.h>
00038
00039 #include <boost/algorithm/string.hpp>
00040
00041 namespace rosmatlab {
00042
00043 mxArray *message_constructor(const MessagePtr& message, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
00044 {
00045 mxArray *result = 0;
00046
00047
00048 if (nrhs > 0 && Options::isString(prhs[0])) {
00049 std::string option = Options::getString(prhs[0]);
00050 if (nrhs == 1 && boost::algorithm::iequals(option, "definition")) {
00051 result = mxCreateString(message->getDefinition());
00052 return result;
00053 }
00054 if (nrhs == 1 && boost::algorithm::iequals(option, "md5sum")) {
00055 result = mxCreateString(message->getMD5Sum());
00056 return result;
00057 }
00058 if (nrhs == 1 && boost::algorithm::iequals(option, "fields")) {
00059 result = mxCreateCellMatrix(1, message->getFieldNames().size());
00060 for(std::size_t i = 0; i < message->getFieldNames().size(); ++i) {
00061 mxSetCell(result, i, mxCreateString(message->getFieldNames().at(i)));
00062 }
00063 return result;
00064 }
00065 if ((nrhs == 1 || nrhs == 2) && boost::algorithm::iequals(option, "default")) {
00066 if (nrhs == 2) {
00067 const mxArray *default_options = prhs[1];
00068 Conversion::perMessageOptions(message).merge(ConversionOptions(1, &default_options));
00069 }
00070 return Conversion::perMessageOptions(message).toMatlab();
00071 }
00072 }
00073
00074
00075 std::size_t count = 1;
00076 if (nrhs > 0 && (Options::isIntegerScalar(prhs[0]) || Options::isDoubleScalar(prhs[0]))) {
00077 count = Options::getIntegerScalar(prhs[0]);
00078 nrhs--; prhs++;
00079 }
00080
00081
00082 ConversionOptions options;
00083 if (nrhs % 2 == 0) {
00084 options.init(nrhs, prhs);
00085 } else {
00086 options.init(nrhs - 1, prhs + 1);
00087 }
00088
00089
00090 if (nrhs % 2 != 0) {
00091 Conversion conversion(message, options);
00092 V_Message copy(conversion.numberOfInstances(prhs[0]));
00093
00094 for(std::size_t j = 0; j < copy.size(); j++) {
00095 copy[j] = conversion.fromMatlab(prhs[0], j);
00096
00097 result = Conversion(conversion, copy[j]).toMatlab(result, j, copy.size());
00098 }
00099
00100
00101 } else {
00102 MessagePtr m = message->introspect(message->createInstance());
00103
00104 Conversion conversion(m, options);
00105 result = conversion.toMatlab();
00106 }
00107
00108
00109 if (count > 1) {
00110
00111 mxArray *repmatrhs[] = { result, mxCreateDoubleScalar(1), mxCreateDoubleScalar(count) };
00112 mxArray *repmatlhs[] = { 0 };
00113 mexCallMATLAB(1, repmatlhs, 3, repmatrhs, "repmat");
00114 mxDestroyArray(repmatrhs[0]);
00115 mxDestroyArray(repmatrhs[1]);
00116 mxDestroyArray(repmatrhs[2]);
00117 result = repmatlhs[0];
00118 }
00119
00120 return result;
00121 }
00122
00123 }