Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Examples

LCDElement.cpp

00001 #include "LCDElement.h"
00002 #include "LCDLock.h"
00003 #include <unistd.h>
00004 #include <pthread.h>
00005 #include <sstream>
00006 #include <iostream>
00007 
00008 using namespace std;
00009 
00010 set<std::string> LCDElement::_elementsList;
00011 LCDMutex LCDElement::_elementMutex;
00012 unsigned int LCDElement::_elementCounter = 0;
00013 
00014 LCDElement::LCDElement(const string &id, const string &addCommand, const string &addParam, LCDElement *parent)
00015 {
00016   if (id.size() == 0)
00017   {
00018     LCDLock l(&LCDElement::_elementMutex);
00019     ostringstream idBuffer;
00020     idBuffer << "LCDAPI_"
00021              << getpid()
00022              << "_"
00023              << LCDElement::_elementCounter;
00024 
00025     LCDElement::_elementCounter++;
00026     _id = idBuffer.str();
00027   }
00028   else
00029   {
00030     _id = id;
00031   }
00032   _elementAddCmd = addCommand;
00033   _elementAddParam = addParam;
00034   _parent = parent;
00035   if (_parent)
00036   {
00037     _parent->addToList(this);
00038   }
00039   notifyCreated();
00040 }
00041 
00042 LCDElement::~LCDElement()
00043 {
00044   notifyDestroyed();
00045 }
00046 
00047 void LCDElement::notifyCreated()
00048 {
00049   LCDLock l(&LCDElement::_elementMutex);
00050   sendCommand(_elementAddCmd, _elementAddParam);
00051   _iAmDead = false;
00052   LCDElement::_elementsList.insert(_id);
00053 }
00054 
00055 void LCDElement::notifyDestroyed()
00056 {
00057   LCDLock l(&LCDElement::_elementMutex);
00058   if (!_iAmDead)
00059   {
00060     LCDElement::_elementsList.erase(_id);
00061     ElementMap::iterator it;
00062     for (it = _childrenList.begin(); it != _childrenList.end(); it++)
00063     {
00064       it->second->notifyDestroyed();
00065     }
00066     _childrenList.clear();
00067     sendCommand(_elementDel);
00068     if (_parent)
00069     {
00070       _parent->removeFromList(this);
00071     }
00072     _iAmDead = true;
00073   }
00074 }
00075 
00076 bool LCDElement::exists(string id)
00077 {
00078   LCDLock l(&LCDElement::_elementMutex);
00079   return ( (LCDElement::_elementsList.find(id)) != (LCDElement::_elementsList.end()) );
00080 }
00081 
00082 const string &LCDElement::getId()
00083 {
00084   return _id;
00085 }
00086 
00087 void LCDElement::sendCommand(const std::string &cmd, const std::string &parameters)
00088 {
00089   LCDLock l(&LCDElement::_elementMutex);
00090   if (cmd.size() > 0)
00091   {
00092     if (_parent)
00093     {
00094       string realParams = _id + " " + parameters;
00095       _parent->sendCommand(cmd, realParams);
00096     }
00097     else
00098     {
00099       _commandBuffer.push_back(Command(cmd, parameters));
00100     }
00101   }
00102 }
00103 
00104 void LCDElement::setParent(LCDElement *parent)
00105 {
00106   _parent = parent;
00107   notifyAdded();
00108 }
00109 
00110 void LCDElement::add(LCDElement *child)
00111 {
00112   child->setParent(this);
00113   addToList(child);
00114 }
00115 
00116 void LCDElement::addToList(LCDElement *elt)
00117 {
00118   _childrenList[elt->getId()] = elt;
00119 }
00120 
00121 void LCDElement::removeFromList(LCDElement *elt)
00122 {
00123   _childrenList.erase(elt->getId());
00124 }
00125 
00126 void LCDElement::notifyAdded()
00127 {
00128   CommandList::iterator it;
00129 
00130   for (it = _commandBuffer.begin(); it != _commandBuffer.end(); it++)
00131   {
00132     sendCommand(it->_cmd, it->_params);
00133   }
00134 }

Generated on Wed Aug 4 19:39:09 2004 for LCDApi by doxygen 1.3.6