Table of Contents
Writing C++ code that makes use of Tweek is not difficult, though
it often requires some good planning. With the current code base, the
C++ side of things maintains the state information through an
implementation of the Tweek
Subject interface. Instances of such an implementation may need
to communicate with other parts of a given application, and it is
important to define these relationships well. In other words, as a
maintainer of application state information, the subject implementation
should have easy access to that state information.
Furthermore, developers must keep in mind that there may be asynchronous execution of application code as a result of using Tweek. The local ORB runs in its own thread, and as such, it executes methods of servants from that thread. Whatever the servant does, it should be thread-safe with respect to the rest of the application.
In this chapter, we cover each aspect of writing C++ code that uses the Tweek API. We begin by explaining how to make a custom subject implementation. Then, we discuss the use of the CORBA Manager from user-level code. We conclude the chapter with an overview of using the Subject Manager.
To create a custom subject implementation, you must derive from
two classes: the abstract class that defines the custom interface and
tweek::SubjectImpl. Referring back to the
interface shown in Example 3.2, “CustomSubject.idl”, the
basic C++ class declaration would appear as follows:
Example 4.1. CustomSubjectImpl.h
1 #ifndef _CUSTOM_SUBJECT_IMPL_H_
#define _CUSTOM_SUBJECT_IMPL_H_
#include <tweek/CORBA/SubjectImpl.h>
5 #include <CustomSubject.h>
namespace mymod
{
10 class CustomSubjectImpl : public POA_mymod::CustomSubject,
public tweek::SubjectImpl
public:
CustomSubjectImpl() : mValue(0.0f)
{
15 ;
}
virtual ~CustomSubjectImpl()
{
20 ;
}
virtual float getValue();
25 virtual void setValue(float v);
mymod::CustomSubject_ptr _this()
{
return POA_mymod::CustomSubject::_this();
30 }
private:
float mValue;
};
35
}
#endif | Here we declare our parent classes,
|
| These two declarations correspond to the
|
| Overriding the method named
Note the namespaces used on this method. The return type
is |
The implementations of getValue() and
setValue() are fairly obvious, though they
are presented here for the sake of completeness. Note, however, that
setValue() changes the state of the subject,
and thus any observers must be notified of the change. The
implementations are shown in the following example.
Example 4.2. CustomSubjectImpl.cpp
#include <CustomSubjectImpl.h>
namespace mymod
{
float CustomSubjectImpl::getValue()
{
return mValue;
}
void CustomSubjectImpl::setValue(float v)
{
mValue = v;
tweek::SubjectImpl::notify();
}
}The key point to note is the call to
tweek::SubjectImpl::notify() in the
setValue() implementation. In general,
anything that modifies the state of the subject requires that this
method be invoked. Note also that the method is fully qualified so
that we are sure to call the correct implementation.