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/connection_header.h>
00030 #include <rosmatlab/options.h>
00031 #include <boost/lexical_cast.hpp>
00032
00033 namespace rosmatlab {
00034
00035 ConnectionHeader::ConnectionHeader()
00036 {
00037 }
00038
00039 ConnectionHeader::ConnectionHeader(const mxArray *array)
00040 {
00041 fromMatlab(array);
00042 }
00043
00044 ConnectionHeader::ConnectionHeader(const boost::shared_ptr<ros::M_string> &connection_header)
00045 : data_(connection_header)
00046 {
00047 }
00048
00049 ConnectionHeader::~ConnectionHeader()
00050 {
00051 }
00052
00053 bool ConnectionHeader::fromMatlab(const mxArray *array)
00054 {
00055 data_.reset();
00056 if (!mxIsStruct(array) && !mxGetNumberOfElements(array) == 1) return false;
00057
00058 data_.reset(new ros::M_string);
00059 int numberOfFields = mxGetNumberOfFields(array);
00060 for(int i = 0; i < numberOfFields; i++) {
00061 std::string key = mxGetFieldNameByNumber(array, i);
00062 const mxArray *value = mxGetFieldByNumber(array, 0, i);
00063 if (Options::isString(value)) {
00064 data_->insert(ros::StringPair(key, Options::getString(value)));
00065 } else if (Options::isDoubleScalar(value)) {
00066 data_->insert(ros::StringPair(key, boost::lexical_cast<std::string>(Options::getDoubleScalar(value))));
00067 } else if (Options::isIntegerScalar(value)) {
00068 data_->insert(ros::StringPair(key, boost::lexical_cast<std::string>(Options::getIntegerScalar(value))));
00069 } else if (Options::isLogicalScalar(value)) {
00070 data_->insert(ros::StringPair(key, boost::lexical_cast<std::string>(Options::getLogicalScalar(value))));
00071 }
00072 }
00073
00074 return true;
00075 }
00076
00077 mxArray *ConnectionHeader::toMatlab() const {
00078 mxArray *header = mxCreateStructMatrix(1, 1, 0, 0);
00079 if (!data_) return header;
00080
00081 for(ros::M_string::iterator it = data_->begin(); it != data_->end(); ++it) {
00082 mxAddField(header, it->first.c_str());
00083 mxSetField(header, 0, it->first.c_str(), mxCreateString(it->second.c_str()));
00084 }
00085
00086 return header;
00087 }
00088
00089 }