00001 /* Second VOS tutorial. Introduction to Vobjects. 00002 00003 This tutorial covers: 00004 - Creating a Vobject on a site 00005 - Reference counting in VOS 00006 - Simple Vobjects methods 00007 00008 This file (vostut2.cc) is released into the public domain. No 00009 restrictions are placed on its use, distribution or inclusion into 00010 other works. 00011 */ 00012 00013 #include <vos/corelibs/vos/vos.hh> 00014 00015 int main(int argc, char** argv) 00016 { 00017 cout << "VOS Tutorial 2\n\n"; 00018 00019 LocalSocketSite localsite(&NoAccessControl::static_); 00020 00021 /* The fundamental abstraction in VOS is the "Vobject" (pronounced 00022 Vee-Object). A Vobject is an entity which can exchange 00023 messages with other Vobjects. Every Vobject is associated with 00024 a site, and the site provides the means by which the Vobject 00025 may communicate over a network using standards such as TCP/IP 00026 sockets. A Vobject is created using by using the 00027 createMetaObject() method on a site and assigning the returned 00028 object to a vRef<> template (explained below). */ 00029 00030 vRef<Vobject> primus = localsite.createMetaObject("primus", 0); 00031 00032 /* -- Sidebar on Reference Counting -- 00033 00034 Memory management in VOS uses a technique known as Reference 00035 Counting. The idea is simple: an object stores the number of 00036 times it refered to (via a C++ pointer or reference, the two 00037 are essentially interchangeable) in other parts of the program. 00038 When an object is constructed, its reference is initially 1. 00039 As additional pointers to the object are created, the reference 00040 count is increased, and as those pointers go away the count in 00041 decreased. When the count reaches 0, this means the object is 00042 no longer refered to anywhere in the program and can be safely 00043 deleted without leaving any dangling pointers. 00044 00045 When a pointer to an object is returned from a method in VOS, 00046 the object will usually have had its reference count increased 00047 (check the API documentation if you are not sure). If this is 00048 the case, you must decrease the reference count when you are 00049 done. This is done by calling the release() method. To 00050 increase the reference count (such as when you are creating a 00051 new pointer to the object) use the acquire() method. 00052 00053 To simplify reference counting you can use the smart pointer 00054 class vRef<>. It is a template class whose purpose is simply 00055 to ensure that a reference which you have acquired is released 00056 when you are done with it. The way it works is very simple: 00057 the template holds a pointer to the object. When the vRef 00058 object is destroyed (usually by going out of scope) it calles 00059 release() on the pointer it holds. Please note a vRef does NOT 00060 increment the reference count when it is created. You can, 00061 however, assign one vRef to another, in which case the count of 00062 the referenced object WILL be incremented. 00063 00064 -- End Sidebar on Reference Counting -- 00065 */ 00066 00067 /* Now, back to our VOS tutorial. Let's look at a few 00068 simple methods you can use on Vobjects. */ 00069 00070 // Get the Vobject's site name. This is the name it was created 00071 // with, and is unique on its site. 00072 cout << "Vobject site name is: " << primus->getName() << "\n"; 00073 00074 // Get the URL you can use to refer to this Vobject. It should 00075 // consist of the hostname of your site and the vobject's unique 00076 // name. 00077 cout << "Vobject URL is: " << primus->getURL().getString() << "\n"; 00078 00079 // Get the site the Vobject is associated with, which is our 00080 // localsite object. Sites are a subclass Vobjects, so you can 00081 // use the entire Vobject API (described in this and following 00082 // tutorials) with sites. 00083 vRef<Site> site = primus->getSite(); 00084 cout << "Vobject site is: " << site->getURL().getString() << "\n"; 00085 cout << "Our local site is: " << localsite.getURL().getString() << "\n"; 00086 00087 /* Now if you access this site with a VOS browser such as 00088 "mesh" you will see a single vobject named "primus". */ 00089 00090 while(true) { 00091 localsite.flushIncomingBuffers(); 00092 } 00093 }