Previous: Declaring Processor Object Types Up: Processor Objects Next: Allocating Processor Objects
Defining a processor object as an executable means there are two types of members for processor objects: implicit and explicit. Implicit members are those functions and objects at file scope in the executable, while explicit members are those explicitly declared in the processor object type. Implicit members are protected members of the processor object type and cannot be accessed using a global pointer to the processor object.
In our mergesort, we will define the Sorter processor object by compiling the file pobj_Sorter.cc++, shown here:
// Definition of Member Functions of Processor Object Sorter #include "pobj_MergeSort.h"void Sorter::sort() { // Sort a portion of an array, perhaps reading it from disk. // In this example, just output a sorted list of numbers. for (int i=start_index; i<stop_index; i++) out->append(i); out->append(ENDVALUE); }
Sorter::Sorter (DList_removing *global remover, int start, int stop) : start_index(start), stop_index(stop) { out = new DList_appending(remover); spawn sort(); }
We define the constructor Sorter::Sorter and the member function Sorter::sort as explicit members of the type Sorter. When we compile this using
>cc++ pobj_Sorter.cc++ -o pobj_Sorter.out -ptype=Sorter gptr_dlist.o
all file scope objects and variables in gptr_dlist.o become implicit members of Sorter. The executable pobj_Sorter.out is now a processor object of type Sorter.
We similarly define Merger
>cc++ pobj_Merger.cc+ -o pobj_Merger.out -ptype=Merger gptr_dlist.o
where pobj_Merger.cc++ contains
// Definition of Member Functions of Processor Object Merger #include "pobj_MergeSort.h"void Merger::merge() { int top1 = in1->remove(); // Smallest UnMerged Element in in1 int top2 = in2->remove(); // Smallest UnMerged Element in in2
while ((top1!=ENDVALUE) && (top2!=ENDVALUE)) { if (top1<=top2) { out->append(top1); top1 = in1->remove(); } else { out->append(top2); top2 = in2->remove(); } } while (top1!=ENDVALUE) { out->append(top1); top1 = in1->remove(); } while (top2!=ENDVALUE) { out->append(top2); top2 = in2->remove(); } out->append(ENDVALUE); }
Merger::Merger(DList_removing *global remover) { in1 = new DList_removing(); in2 = new DList_removing(); out = new DList_appending(remover); spawn merge(); }