Home > Message-Passing, Projects, RTOS, SKC++ > SKC++: Message-Passing Classes

SKC++: Message-Passing Classes

Thursday, November 5, 2009 Leave a comment Go to comments

Message

To avoid clutter, I haven’t shown the class methods. On this occasion, I’ll just describe the classes and their characteristics in the text.

Message and aMessage

The class aMessage, shown in green is some kind of message the application needs. The only rules for aMessage, as far as SKC++ is concerned, are:

  • It must be derived from Message, though Message itself is unlikely to be of much interest to an application programmer thereafter.
  • It must also be derived from Pooled, because messages must be dynamically allocated.

Ā«autoptrĀ»

This is not a standard UML stereotype; I invented it. Its purpose is to indicate that MessagePtr and its template derivative, which I’ll call MsgPtr<> in the text from now on, behave something like the auto_ptr in the C++ Standard Template Library. An auto_ptr is a kind of “smart pointer” whose key characteristics are:

  • It contains an actual pointer, of appropriate type, which points to some object.
  • The internal pointer is never accessed by the user.
  • When an auto_ptr is destroyed, the object pointed to is destroyed as well.
  • When an auto_ptr object is copied, the internal pointer is copied to the new auto_ptr object but the original internal pointer is nulled. Thus the object pointed to is accessible through only one auto_ptr object at a time and ownership of the object pointed to is thereby transferred.

MessagePtr and MsgPtr<>

MsgPtr<> classes are derived from MessagePtr. The base class is used when passing messages from one task to another (more on this next time) but within a single context the appropriate MsgPtr<> would normally be used.

Each MsgPtr<> class is specified, through its template parameter, for use with a particular type of message, and its corresponding objects are used as pointers to messages of that type. A MsgPtr<> object behaves much like an auto_ptr object but differs from it in the following ways:

  • A raw pointer cannot be assigned to or converted into a MsgPtr<> or vice versa.
  • MsgPtr<> has a method which is used to create an object of the corresponding message type, which can thereafter be accessed only through the MsgPtr<>.

Putting it all together

That’s the theory; here’s some example code:

class aMessage: public Message, public Pooled<aMessage, 10>
{
    // Stuff goes here.
};

MsgPtr<aMessage> pMsg;
pMsg.newMsg();

// Do things to the message via pMsg...
// Passing pMsg by value to another function will make
//  a copy and invalidate pMsg.
// If that's a problem, it's OK, within a single context, to
//  pass pMsg by reference.

When the time comes to send the message to another task, this can be done because the sending function will accept, as an argument, any well-formed MsgPtr<> object. In fact the parameter type is the base class, MessagePtr, and the argument is passed by value to ensure ownership transfer. More on sending and receiving messages next time.

How not to do it

The design of these classes will not stop someone from coding this:

class aMessage: public Message, public Pooled<aMessage, 10>
{
    // Stuff goes here.
};

aMessage * pMsg = new aMessage;

// Do things to the message via pMsg...

But this message cannot be sent to another task because pMsg cannot be converted to a MessagePtr object and is therefore unacceptable to the sending function.

Advertisements
Categories: Message-Passing, Projects, RTOS, SKC++ Tags:
  1. Peter Bushell
    Thursday, November 5, 2009 at 22:21

    Correction…

    Can’t overload class names (template and non-template) like I did with MessgePtr in the diagram. That works only for functions!

    Update…

    I corrected the article (and updated this comment) on 09 November 2009. The rules of blogging are breakable; the rules of C++ are not!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: