www.gusucode.com > ros 工具箱 matlab源码程序 > ros/+robotics/+ros/+msg/+internal/JavaMessage.m

    classdef JavaMessage
    %This class is for internal use only. It may be removed in the future.
    
    %JavaMessage Internal utility functions for Java message objects
    %
    %   JavaMessage methods:    
    %      extractMessageType  - Extract message type from object
    %      getAccessor         - Return Java call for message property
    %      isJavaMessageObject - Determines if message object a Java message
    
    %   Copyright 2014 The MathWorks, Inc.
    
    methods (Static)       
        function [getJavaCall, setJavaCall] = getAccessor(str)
            %getAccessor Return Java call for message property
            %   Based on a ROS based property string, return the corresponding Java 
            %   function calls for getting and setting that property. These
            %   return values can be used to access data in a Java message
            %   object.
            %   Multiple sequential dots will be merged.
            %
            %   [GETJAVACALL, SETJAVACALL] =
            %   robotics.ros.msg.internal.getAccessor(STR) returns the corresponding
            %   Java function calls for getting (GETJAVACALL) and setting 
            %   (SETJAVACALL) of the nested property in STR. 
            %
            %   The input STR has to be a nonempty string and consists of 
            %   multiple property names separated by '.'. Inputs with no
            %   dots are acceptable as well. Underscores in the property
            %   names will result in the following letter being capitalized
            %   in the Java function calls. Any input property names that
            %   are not valid ROS names will trigger an error, e.g. spaces
            %   or invalid characters in property names. Multiple sequential 
            %   dots and underscores will be merged for processing.
            %
            %   Examples:
            %      import robotics.ros.msg.internal.JavaMessage.getAccessor;
            %
            %      [get,set] = getAccessor('pose.position.x')
            %      returns get = 'getPose.getPosition.getX' and
            %      set = 'getPose.getPosition.setX'
            %
            %      [get,set] = getAccessor('pose') returns get = 'getPose'
            %      and set = 'setPose'
            %
            %      [get,set] = getAccessor('pos.is_valid') returns get = 
            %      'getPos.getIsValid' and set = 'getPos.setIsValid'
            %
            %      [get,set] = getAccessor('image...img___enc') returns get = 
            %      'getImage.getImgEnc' and set = 'getImage.setImgEnc'
            
            validateattributes(str, {'char'}, {'nonempty'}, mfilename, 'str', 1);

            % Use the dot as delimiter (merging subsequent dots)
            elems = strsplit(str, '.');
            for j = 1:length(elems)
                s = elems{j};
                
                % Check for ROS name validity
                if ~robotics.ros.internal.Namespace.isValidFieldName(s)
                    error(message('robotics:ros:util:NameInvalid', ...
                        'field', s));
                end               
                
                s(1) = upper(s(1));
                
                % Find all underscores, as they will be removed and the
                % subsequent letter capitalized
                uScores = strsplit(s, '_');
                for k = 1:length(uScores)
                    u = uScores{k};
                    
                    if isempty(u)
                        continue;
                    end                   
                    
                    u(1) = upper(u(1));
                    uScores{k} = u;
                end
                elems{j} = ['get' strjoin(uScores, '')];
            end
            
            setElem = elems{end};
            setElem(1) = 's';
            
            getJavaCall = strjoin(elems, '.');
            setJavaCall = strjoin({elems{1:end-1}, setElem}, '.');
        end
        
        function javaType = isValid(msg)
            %isJavaMessageObject Determines if object is a Java message
            %
            %   JAVATYPE = robotics.ros.msg.internal.JavaMessage.isValid(MSG)
            %   returns TRUE if the input object MSG is a valid Java message
            %   object (derived from class org.ros.internal.message.Message).
            %   If the input is empty or of some other class the JAVATYPE
            %   return will be FALSE.
            
            if isempty(msg)
                javaType = false;
                return;
            end
            
            if isa(msg, 'org.ros.internal.message.Message')
                javaType = true;
            else
                javaType = false;
            end
        end
        
        function type = extractMessageType(msg)
            %extractMessageType Extract message type from object
            %   Based on a given object representing a ROS message, extract
            %   the ROS message type as a string.
            
            if robotics.ros.msg.internal.JavaMessage.isValid(msg)
                type = char(msg.toRawMessage.getType);
            else
                error(message('robotics:ros:message:NotValidJava'));
            end
        end
    end
    
end