00001 /* Seventh VOS tutorial. Creating your own MetaObjects 00002 00003 This tutorial file covers: 00004 - Subclassing from MetaObject 00005 - Essential methods to override 00006 - Defining specialized Local and Remote MetaObjects 00007 00008 This file (vostut7hello.hh) is released into the public domain. No 00009 restrictions are placed on its use, distribution or inclusion into 00010 other works. 00011 */ 00012 00013 #ifndef _VOSTUT7_HH_ 00014 #define _VOSTUT7_HH_ 00015 00016 #include <vos/corelibs/vos/vos.hh> 00017 00018 /* In this tutorial we demonstrate how to create a metaobject which 00019 implements an API for saying hello to things. The method that we 00020 will implement will be simple: 00021 00022 string hello(const string& s) 00023 00024 This method will accept a greeting and return a string with the 00025 Vobject's reply. 00026 */ 00027 00028 /* First define the "Hello" class. As a MetaObject extension, it must 00029 inherit from the "MetaObject" class. This class will provides an 00030 abstract interface for the "hello" method we want to support. 00031 Because the action of this method depends on whether we are 00032 accessing a local or remote object. the actual implementation of 00033 this method will be done in further subclasses. 00034 */ 00035 class Hello : public MetaObject 00036 { 00037 protected: 00038 00039 // There should only ever be one constructor, and it should take 00040 // one parameter. 00041 00042 Hello(MetaObject* superobject); 00043 00044 00045 public: 00046 // The hello method. By having "Hello" be an abstract class an 00047 // application do not need to know whether the vobject is local or 00048 // remote, simply that it has the "Hello" interface. 00049 00050 virtual string hello(const string& s) = 0; 00051 00052 00053 // This returns the VOS type string for this specific extension, 00054 // in this case "tutorial:hello". This is a vobject type, not the 00055 // C++ class name. 00056 00057 virtual const string getType(); 00058 00059 00060 // Standard method to be called by an application to request 00061 // that this type extension be registered with VOS. 00062 00063 static void registerExtenders(); 00064 }; 00065 00066 00067 // This class implements Hello for remote Vobjects. It is essentially 00068 // a "stand in" or "stub" and its purpose is to translate C++ method 00069 // calls into the actual network commands that acomplish the desired 00070 // task, in this case to say hello to the Vobject in question. 00071 00072 class RemoteHello : public virtual Hello 00073 { 00074 protected: 00075 00076 // There should only ever be one constructor, and it should take 00077 // one parameter. An application never calls the constructor 00078 // directly, instead remote metaobjects are instantiated by VOS 00079 // automatically when new vobjects are encountered. 00080 00081 RemoteHello(MetaObject* superobject); 00082 00083 public: 00084 00085 // This doesn't implement the "hello" logic, but rather creates 00086 // and sends a "tutorial:hello" message. 00087 00088 virtual string hello(const string& s); 00089 00090 00091 // This method will be used to instantiate a new Hello metaobject 00092 // when needed. We can't use the constructor directly because it 00093 // isn't a normal function or method. 00094 00095 static MetaObject* new_RemoteHello(MetaObject* superobject, const string& type); 00096 }; 00097 00098 00099 // This class implements Hello for local Vobjects. This means that it 00100 // implements the hello() method and responds to incoming 00101 // "tutorial:hello" messages 00102 00103 class LocalHello : public virtual Hello 00104 { 00105 protected: 00106 00107 // There should only ever be one constructor, and it should take 00108 // one parameter. An application never calls the constructor 00109 // directly, but rather will use Site::creatMetaObject(). 00110 00111 LocalHello(MetaObject* superobject); 00112 00113 public: 00114 00115 // This implements the actual logic for answering a "hello". 00116 00117 virtual string hello(const string& s); 00118 00119 00120 // This method is called when a "tutorial:hello" message is 00121 // delivered to this Vobject. See vostut7hello.cc for details. 00122 00123 void handleHello(Message* m); 00124 00125 00126 // This method will be used to instantiate a new Hello metaobject 00127 // when needed. We can't use the constructor directly because it 00128 // isn't a normal function or method. 00129 00130 static MetaObject* new_LocalHello(MetaObject* superobject, const string& type); 00131 }; 00132 00133 #endif