Next: The Main Program
Up: Getting Started with Shore
Previous: Defining Data Types
The second step in building an application is to write the code that
implements the operations of its interfaces.
Currently, this code must be written in C++.
The Shore project intends to support other implementation languages
in future releases.
The implementation code for the search tree example is contained in four
files, one for each interface:
tree.C,
word.C,
cite.C,
and
document.C.
On the whole, this code is similar to the code one would write to implement
the C++ classes generated from the interface definitions.
However, there are several points to note:
- Each of these files #includes the header file stree.h,
which is generated
from the interface definitions.
This file, in turn,
#includes header files from the Shore library that define a variety
of types and macros mentioned below.
- Types defined as ref<T> in SDL are written as Ref<T>
in C++.
Similar remarks apply to set, bag, etc.
- Operations declared const in SDL become const member
functions in the C++ binding.
- Values of type Ref<T> can be used as
if they were of type const T*.
For example, consider the body of the function
SearchTree::find.
The attribute root
of interface SearchTree, which is declared to have type ref<Word>,
is translated to a data member of type Ref<Word> of class
SearchTree.
Thus the test "if (root)" checks whether the root pointer
is null, and the call "root->find(str)" invokes the const
member function Word::find.
- The special member function Ref<T>::update converts a value of
type Ref<T> into a pointer of type T*. In other words,
update explicitly performs the conversion from Ref to pointer
described in the preceding paragraph, but without the
const qualification.
For example, consider the body of the function
SearchTree::insert.
Since the automatic conversion
described in the preceding paragraph can only convert a Ref<Word> to
a const Word *, the compiler would reject
root->find_or_add(s)
because Word::find_or_add is a non-const member function of Word.
Thus we write root.update()->find_or_add(s) to explicitly convert
root from Ref<Word> to Word *.
The update function has the runtime effect of locking
the referenced object to prevent interference from other users
and marking it as (potentially) modified,
so that it will be written back to persistent storage at the end of a
transaction.
Next: The Main Program
Up: Getting Started with Shore
Previous: Defining Data Types
Marvin Solomon
Fri Aug 2 13:40:31 CDT 1996