Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Published by Scroll Versions from space IceMaster and version 3.7.1

...

The MATLAB mapping for Fruit is shown below:

Code Block
languagematlab
titleMATLAB
classdef Fruit < int32
    enumeration
        Apple (0)
        Pear (1)
        Orange (2)
    end
    methods(Static)
        ...
        function r = ice_getValue(v)
            switch v
                case 0
                    r = Fruit.Apple;
                case 1
                    r = Fruit.Pear;
                case 2
                    r = Fruit.Orange;
                otherwise
                    throw(Ice.MarshalException(...));
            end
        end
    end
end

Given the above definitions, we can use enumerated values as follows:

Code Block
languagematlab
titleMATLAB
f1 = Fruit.Apple;
f2 = Fruit.Orange;

if f1 == Fruit.Apple % Compare with constant
    % ...
end

if f1 == f2          % Compare two enums
    % ...
end

switch f2            % Switch on enum
    case Fruit.Apple
        % ...
    case Fruit.Pear
        % ...
    case Fruit.Orange
        % ...
end

You can obtain the ordinal value of an enumerator using the int32 function:

Code Block
languagematlab
titleMATLAB
val = int32(Fruit.Pear);
assert(val == 1);

To convert an integer into its equivalent enumerator, call the ice_getValue function:

Code Block
languagematlab
titleMATLAB
f = Fruit.ice_getValue(2);
assert(f == Fruit.Orange);

...

The generated code changes accordingly:

Code Block
languagematlab
titleMATLAB
classdef Fruit < int32
    enumeration
        Apple (0)
        Pear (3)
        Orange (4)
    end
    ...
end

...