Archived

This forum has been archived. Please start a new discussion on GitHub.

Slice Class inheritance question

Helo,

For my current server setup, I have a three step approach.

The first layer decides the function type being called. I setup an enum in my slice file for this called FUNCTION.
The second layer decides which sector the message is originating from.
The third layer is where the message is actually processed.

Now I have a slice file which has 3 classes atm. The first one is FunctionMessage, and it just contains a var Function func; This will hold the function type.
The second class, SectorMessage, extends the first one and adds a sectorId, which is an int. So far so good.
Now I get to the third class, MovementMessage. Since the MovementMessage is always linked to a particilar FUNCTION, I would like to set the func var right there in the slice file. The MovementMessage extends the SectorMessage, which in turn extends the FunctionMessage. As such, the MovementMessage already has access to the func variable. But for some reason I can't seem to figure out a way to set it right there in the slice file.

I've added what I have so far below:
module com {
	module ractoc {
		module test {
			enum FUNCTION { COMBAT, MOVEMENT, MARKET };
			
			class FunctionMessage {
				FUNCTION funct;
			};
			
			class SectorMessage extends FunctionMessage {
				int sectorId;
			};
			
			class MovementMessage extends SectorMessage {
				int speed;
			};
			
			interface MessageTest {
				void sendMessage(FunctionMessage msg);
			};
		};
	};
};

Comments

  • mes
    mes California
    The Slice syntax doesn't provide a way to do what you want. You can assign a default value to a member at the point of declaration, but there is no way for a subclass to assign a default value to an inherited member.

    Regards,
    Mark
  • Ok, I'll have to figure out a different way to determine the type of sub-class.

    The idea behind using the enum instead of the ice_isA method is speed, comparing an enum is faster then a binary search.
  • mes
    mes California
    ractoc wrote: »
    The idea behind using the enum instead of the ice_isA method is speed, comparing an enum is faster then a binary search.
    That's true, but will this test be performed locally or remotely?

    These are two very different use cases. If you are invoking ice_isA on a remote Ice object, the difference between comparing an enum and performing a binary search is insignificant compared to the overhead of the remote invocation itself.

    On the other hand, if you are transferring instances of these classes by value and performing a local test to determine which type of object you have received, then another alternative is to perform a language cast. For example, in Java:
    FunctionMessage fm = ...;
    if(fm instanceof MovementMessage)
    {
        // handle movement
    }
    ...
    

    My point is that performing a language cast is equivalent in purpose to calling ice_isA on a local object, but I expect a language cast would be more efficient.

    Regards,
    Mark
  • True, so I will probably end up using instanceof, since my server is in Java.

    Thanks for your time anyway.