WOscContainer Class Reference

#include <WOscContainer.h>

Inherits WOscPatternMatch.

List of all members.

Public Member Functions

 WOscContainer (const WOscContainerInfo &info=WOscContainerInfo("root container"), int defNumChildren=32, int defNumMethods=32)
 WOscContainer (WOscContainer *parent, const char *name, const WOscContainerInfo &info=WOscContainerInfo(), int defNumChildren=32, int defNumMethods=32)
void AddContainerAlias (WOscContainer *parent, const char *alias)
void DispatchMessage (WOscCallbackList &methods, WOscMessage *msg)
WOscString GetName () const
WOscString GetAddress () const
WOscString GetAddressSpace () const
WOscString GetMethodList () const
const WOscContainerFindContainer (const char *address) const
bool RemoveContainer (const char *name)
bool RemoveMethod (const char *name)
const WOscContainerFindRoot () const
bool IsRoot () const
void RemoveAll ()

Friends

class WOscMethod

Detailed Description

OSC Address-space node. An OSC-address space consists of a tree of containers with methods as leaves.

Todo:
More elaborate documentation of WOscContainer class.
Remarks:
ALL CONTAINERS EXCEPT THE ROOT CONTAINER MUST BE ALLOCATED DYNAMICALLY, since the root container deletes all its children during its destruction!!
See also:
See the examples for address-tree generation and deletion.
Examples:

WOscClient.cpp, WOscServer.cpp, WOscStreamingClient.cpp, and WOscStreamingServer.cpp.

Definition at line 104 of file WOscContainer.h.


Constructor & Destructor Documentation

WOscContainer::WOscContainer ( const WOscContainerInfo info = WOscContainerInfo("root container"),
int  defNumChildren = 32,
int  defNumMethods = 32 
)

Constructor for a root container. Root container do not have parents (what's implicated by the name). The root-container is responsible for clean-up. Deletion is not allowed for WOscContainer and WOscMethod objects. The address space can be removed only as whole or be manipulated by dedicated functions.

Parameters:
info Container information.
defNumChildren Default internal array-size for children-references. If not sufficient during later use, the array-sizes double themselves automatically.
defNumMethods See parameter defNumChildren.

Definition at line 67 of file WOscContainer.cpp.

00068 {
00069 
00070     /* a root container does not have any parents */
00071     m_parent = NULL;
00072 
00073     m_containerEntries.reserve(defNumChildren);
00074     m_methodEntries.reserve(defNumMethods);
00075 
00076     /* container info */
00077     m_info = info;
00078 }

WOscContainer::WOscContainer ( WOscContainer parent,
const char *  name,
const WOscContainerInfo info = WOscContainerInfo(),
int  defNumChildren = 32,
int  defNumMethods = 32 
)

Constructor for child containers. Child containers must supply a parent. They get added to the parent-container list of children and can be parents if constructed.

Parameters:
info Container information.
parent Pointer to the parent-container. Must not be NULL.
name OSC-pattern with the name of the container. This name will be later a part of the OSC-address (/the/"name"/of/mama/mia/is/method ...). If WOSC_ADDR_WILDCARDS is defined 1, wildcard matching will be performed on the address path segments.
defNumChildren (Optional) See WOscContainer::WOscContainer(WOscContainerInfo* info, int defNumChildren, int defNumMethods)
defNumMethods (Optional) See WOscContainer::WOscContainer(WOscContainerInfo* info, int defNumChildren, int defNumMethods)
Exceptions:
WOscException If parent pointer NULL. That's illegal. Children should have parents
See also:
WOscContainer::WOscContainer(WOscContainerInfo* info, int defNumChildren , int defNumMethods )

Definition at line 108 of file WOscContainer.cpp.

References ERR_PARENT_POINTER_NULL.

00108                                                                                                                                          {
00109 
00110     if ( parent == NULL )
00111         throw WOscException(ERR_PARENT_POINTER_NULL, __FILE__, __LINE__);
00112 
00113     m_parent    = parent;
00114     m_info      = info;
00115 
00116     m_containerEntries.reserve(defNumChildren);
00117     m_methodEntries.reserve(defNumMethods);
00118 
00119     m_parent->AddContainer(this, name, false);
00120 }


Member Function Documentation

void WOscContainer::AddContainerAlias ( WOscContainer parent,
const char *  alias 
)

Adds an alias of this container to the parent list-of-children.

Parameters:
parent Pointer to container, which becomes the parent of the alias.
alias New name of container under which the parent saves the alias.
Exceptions:
WOscException When this container is the root-container, the root-container can not be aliased. When the parent pointer is this container itself. When invalid parent-container pointer (NULL) is passed.
Remarks:
Aliases are not real children and won't be deleted during recursive deletion.
Examples:
WOscClient.cpp, WOscStreamingClient.cpp, and WOscStreamingServer.cpp.

Definition at line 191 of file WOscContainer.cpp.

References ERR_PARENT_POINTER_NULL, and ERR_RECURSIVE_ALIAS.

00192 {
00193     if ( m_parent == NULL )
00194         throw WOscException(ERR_ROOT_ALIAS, __FILE__, __LINE__);
00195 
00196     if ( parent == this )
00197         throw WOscException(ERR_RECURSIVE_ALIAS, __FILE__, __LINE__);
00198     
00199     if ( parent == NULL ) 
00200         throw WOscException(ERR_PARENT_POINTER_NULL, __FILE__, __LINE__);
00201 
00202     parent->AddContainer(this, alias, true);
00203 }

void WOscContainer::DispatchMessage ( WOscCallbackList methods,
WOscMessage msg 
)

Creates a list of methods which match the address of the message passed as argument. Those lists reflect the effect of an OSC-message. The list can then be used to invoke all matching methods.

Parameters:
msg Message to be scanned for.
Returns:
A list containing all matching functions. Its the task of the list if none are found and an invocation is issued. The caller must free the returned list.
Exceptions:
WOscException When dispatching is not executed from a root container. (can be removed by first searching for root. discussion?).
Remarks:
Code stolen from Matt Wright's OSC-Kit and adapted to this architecture (Thank You!).
See also:
WOscContainer::dispatchSubMessage(char* pattern)

Definition at line 361 of file WOscContainer.cpp.

References ERR_INVALID_ADDRESS_NO_SLASH, ERR_NOT_DISPATCHING_FROM_ROOT, WOscString::GetBuffer(), and WOscMessage::GetOscAddress().

00362 {
00363     if ( m_parent != NULL )
00364         throw WOscException(ERR_NOT_DISPATCHING_FROM_ROOT, __FILE__, __LINE__);
00365 
00366     const char* pattern = msg->GetOscAddress().GetBuffer();
00367 
00368     if ( pattern == NULL || pattern[0] != '/')
00369         throw WOscException(ERR_INVALID_ADDRESS_NO_SLASH, __FILE__, __LINE__);
00370 
00371     // dispatch address pattern (note: leading slash removed!)
00372     DispatchSubMessage(methods, pattern+1);
00373 }

const WOscContainer * WOscContainer::FindContainer ( const char *  address  )  const

Tries to find a certain container by its OSC-address. Not pattern but address (/hello/world/whats/up).

Parameters:
address String containing OSC-address to be searched for.
Returns:
Pointer to container if found else NULL.
Todo:
Verify/test this function.

Definition at line 270 of file WOscContainer.cpp.

References WOscPatternMatch::NextSlashOrNull().

00271 {
00272     unsigned int addrlen = strlen(address);
00273     
00274     if ( addrlen == 0 )                     // no address
00275         return NULL;
00276     if ( address[0] != '/' )                // invalid address
00277         return NULL;
00278     if ( addrlen == 1 && *address == '/' )  // root container "/"
00279         return this;
00280     
00281     // find next slash (if any)
00282     const char* next = NextSlashOrNull(address+1);
00283     
00284     if ( *next == 0 ) {
00285         // leaf
00286         for (int i = m_containerEntries.size()-1; i >= 0; i-- )
00287             if ( m_containerEntries[i].m_name == (address+1) )
00288                 return m_containerEntries[i].m_container;
00289     } else {
00290         // not a leaf. just compare node name
00291         unsigned int thisNodeStrLen = next-address;
00292         for ( int i = m_containerEntries.size()-1; i >= 0; i-- ) {
00293             if ( strncmp(m_containerEntries[i].m_name.c_str(), address+1, thisNodeStrLen) == 0 ) {
00294                 const WOscContainer* ctnr = m_containerEntries[i].m_container->FindContainer(next);
00295                 if ( ctnr )
00296                     return ctnr;
00297             }
00298         }
00299     }
00300         
00301     return NULL;
00302 }

const WOscContainer * WOscContainer::FindRoot (  )  const

Traverses the container-tree upwards until root is reached.

Returns:
The pointer of the root-container.

Definition at line 525 of file WOscContainer.cpp.

Referenced by WOscMethod::GetAddressSpace(), GetAddressSpace(), and RemoveAll().

00526 {
00527 
00528     const WOscContainer* momentaryParent = m_parent;
00529     const WOscContainer* lastParent = this;
00530 
00531     /* traverse tree upwards until root is reached */
00532     while ( momentaryParent != NULL ){
00533         lastParent = momentaryParent;
00534         momentaryParent = momentaryParent->m_parent;
00535     }
00536     return lastParent;
00537 }

WOscString WOscContainer::GetAddress (  )  const

Traverses the container-tree upwards until root is reached and assembles its unaliased address.

Returns:
OSC-address of this container.
Remarks:
OSC-addresses of containers end with a slash ('\'), OSC-addresses of methods end with nothing.

Definition at line 449 of file WOscContainer.cpp.

References GetName().

Referenced by GetMethodList().

00450 {
00451 
00452     if ( m_parent == NULL)
00453         return WOscString("/");
00454 
00455     WOscContainer* momentaryParent = m_parent;
00456     WOscString address = GetName();
00457     address += '/';
00458 
00459     /* traverse tree upwards until root is reached */
00460     while ( momentaryParent != NULL ) {
00461         address = momentaryParent->GetName() + address;
00462         momentaryParent = momentaryParent->m_parent;
00463     }
00464     
00465     return address;
00466 }

WOscString WOscContainer::GetAddressSpace (  )  const

Returns a line-break('\n')-separated list of all OSC-addresses of methods as seen from root.

Returns:
WOscString containing the list.
Remarks:
OSC-addresses of containers end with a slash ('\'), OSC-addresses of methods end with nothing.
Examples:
WOscClient.cpp, WOscServer.cpp, WOscStreamingClient.cpp, and WOscStreamingServer.cpp.

Definition at line 479 of file WOscContainer.cpp.

References FindRoot(), and GetMethodList().

00480 {
00481     const WOscContainer* root = FindRoot();
00482     return root->GetMethodList();
00483 }

WOscString WOscContainer::GetMethodList (  )  const

Scans for all local methods and aliased methods.

Returns:
A list as described in WOscContainer::getAddressSpace() containing the addresses of all method-leaves reachable from this node/container.

Todo:
Does not handle aliased containers !!!!

Definition at line 492 of file WOscContainer.cpp.

References GetAddress().

Referenced by WOscMethod::GetAddressSpace(), and GetAddressSpace().

00493 {
00494     WOscString methodList;
00495 
00496     // scan for local methods
00497     for ( int m = m_methodEntries.size()-1; m >= 0; m-- ){
00498         if ( m_methodEntries[m].m_isAlias ) {
00499             methodList += GetAddress();
00500             methodList += m_methodEntries[m].m_name.c_str();
00501         } else {
00502             methodList += m_methodEntries[m].m_method->GetAddress();
00503         }
00504         methodList += '\n';
00505     }
00506 
00507     // scan children containers
00508     for ( int c = m_containerEntries.size()-1; c >= 0; c-- ) {
00509         if ( m_containerEntries[c].m_isAlias ) {
00510             /** \todo Does not handle aliased containers !!!! */
00511         } else {
00512             methodList += m_containerEntries[c].m_container->GetMethodList();
00513         }
00514     }
00515     
00516     return methodList;
00517 }

WOscString WOscContainer::GetName (  )  const

Returns the not aliased name (OSC-pattern, not address) of this container.

Returns:
WOscString containing the name-pattern of this container.

Definition at line 424 of file WOscContainer.cpp.

Referenced by WOscMethod::GetAddress(), and GetAddress().

00425 {
00426     if ( m_parent != NULL ) {
00427         /* search all children of parent for not aliased name */
00428         const std::vector<WOscContainerEntry>& entries = m_parent->m_containerEntries;
00429         for ( int i = entries.size()-1; i >= 0; i-- )
00430             if ( (entries[i].m_container == this) && (!(entries[i].m_isAlias))){
00431                 return WOscString(entries[i].m_name.c_str());
00432                 break;
00433             }
00434     }
00435     return WOscString("/");
00436 }

bool WOscContainer::IsRoot (  )  const

Checks if the current container is the root-container.

Returns:
True if it is, false else.

Definition at line 545 of file WOscContainer.cpp.

Referenced by WOscReceiver::SetAddressSpace().

00546 {
00547     return m_parent == NULL ? true : false;
00548 }

void WOscContainer::RemoveAll (  ) 

Public function for deletion of a whole container-/method-tree. Searches the root and starts a recursive deletion.

See also:
WOscContainer::~WOscContainer()
Examples:
WOscClient.cpp, and WOscServer.cpp.

Definition at line 169 of file WOscContainer.cpp.

References FindRoot().

00170 {
00171     delete FindRoot();
00172 }

bool WOscContainer::RemoveContainer ( const char *  name  ) 

Removes child container or container alias by name (not address).

Definition at line 308 of file WOscContainer.cpp.

00309 {
00310     for ( int i = m_containerEntries.size()-1; i >= 0; i-- )
00311         if ( m_containerEntries[i].m_name == name ) {
00312             // deallocated only if not an alias.
00313             if ( ! m_containerEntries[i].m_isAlias )
00314                 delete m_containerEntries[i].m_container;
00315             m_containerEntries.erase(m_containerEntries.begin() + i);
00316             return true;
00317         }
00318     return false;
00319 }

bool WOscContainer::RemoveMethod ( const char *  name  ) 

Removes child method or container alias by name (not address).

Definition at line 325 of file WOscContainer.cpp.

00326 {
00327     for ( int i = m_methodEntries.size()-1; i >= 0; i-- )
00328         if ( m_methodEntries[i].m_name == name ) {
00329             // deallocated only if not an alias.
00330             if ( ! m_methodEntries[i].m_isAlias )
00331                 delete m_methodEntries[i].m_method;
00332             m_methodEntries.erase(m_methodEntries.begin() + i);
00333             return true;
00334         }
00335     return false;
00336 }


The documentation for this class was generated from the following files:
Generated on Sat Oct 23 03:05:59 2010 for WOscLib by  doxygen 1.6.3