Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

corecontext.cpp

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2002,2003 Koblenz University
00006    Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group
00007    $Id: corecontext.cpp,v 1.6 2004/04/08 13:38:32 rollmark Exp $
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; version 2 of the License.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 */
00022 #include "corecontext.h"
00023 #include <salt/path.h>
00024 #include "leaf.h"
00025 #include "node.h"
00026 #include "core.h"
00027 #include <iostream>
00028 
00029 using namespace boost;
00030 using namespace salt;
00031 using namespace std;
00032 using namespace zeitgeist;
00033 
00034 CoreContext::CoreContext(const boost::shared_ptr<Core> &core, const boost::shared_ptr<Leaf> &root) : mCore(core), mObject(root)
00035 {
00036 }
00037 
00038 CoreContext::~CoreContext()
00039 {
00040     //cout << "~CoreContext()" << endl;
00041 }
00042 
00043 boost::shared_ptr<Leaf> CoreContext::New(const std::string& className, const std::string& pathStr)
00044 {
00045     // first, we try to create the class with the core
00046     shared_ptr<Object> instance = mCore->New(className);
00047 
00048     if (instance.get())
00049         {
00050             // now, we check the type by dynamic casting to leaf
00051             shared_ptr<Leaf> leaf = shared_static_cast<Leaf>(instance);
00052 
00053             if (leaf.get() != NULL)
00054                 {
00055                     // we have created the instance, now we install it at the location
00056                     // stored in pathName
00057                     if (Install(leaf, pathStr) == true)
00058                         {
00059                             mObject = leaf;
00060                             return leaf;
00061                         }
00062                 }
00063         }
00064 
00065     // return default constructed 'NULL' object
00066     return shared_ptr<Leaf>();
00067 }
00068 
00069 bool CoreContext::Delete (const std::string &name)
00070 {
00071     shared_ptr<Leaf> leaf = Get(name);
00072 
00073     if (leaf.get())
00074         {
00075             leaf->Unlink();
00076             return true;
00077         }
00078 
00079     return false;
00080 }
00081 
00082 
00083 boost::shared_ptr<Leaf> CoreContext::Select(const std::string &pathStr)
00084 {
00085     shared_ptr<Leaf> leaf = Get(pathStr);
00086 
00087     if (leaf.get())
00088         {
00089             mObject = leaf;
00090         }
00091 
00092     return leaf;
00093 }
00094 
00095 bool CoreContext::Install(const boost::shared_ptr<Leaf> &leaf, const std::string &pathStr, bool isNamed)
00096 {
00097     //cout << "CoreContext(" << (void*) this << ") Install '" << pathStr << "'" << endl;
00098     Path path(pathStr);
00099 
00100     if(! isNamed)
00101         {
00102             // we need at least one token to 'name' the leaf class
00103             if (path.IsEmpty())
00104                 {
00105                     return false;
00106                 }
00107 
00108             leaf->SetName(path.Back());
00109             path.PopBack();
00110             // now, we have named the leaf object, so we can install it
00111         }
00112 
00113     shared_ptr<Leaf> current;
00114 
00115     // check if we have a relative or absolute path
00116     if (path.IsAbsolute())
00117         {
00118             current = mCore->GetRoot();
00119         }
00120     else
00121         {
00122             current = mObject;
00123         }
00124 
00125     if (! current.get())
00126         {
00127             return false;
00128         }
00129 
00130     while (! path.IsEmpty())
00131         {
00132             current = mCore->GetChild(current, path.Front());
00133 
00134             if (! current.get())
00135                 {
00136                     return false;
00137                 }
00138 
00139             path.PopFront();
00140         }
00141 
00142     return current->AddChildReference(leaf);
00143 }
00144 
00145 boost::shared_ptr<Leaf> CoreContext::Get(const std::string& pathStr)
00146 {
00147     return mCore->Get(pathStr, mObject);
00148 }
00149 
00150 bool CoreContext::Test(const std::string& pathStr)
00151 {
00152     return mCore->Test(pathStr, mObject);
00153 }
00154 
00155 void CoreContext::ListObjects() const
00156 {
00157     Leaf::TLeafList::iterator i;
00158 
00159     for (i = mObject->begin(); i != mObject->end(); ++i)
00160         {
00161             cout << (*i)->GetName();
00162             if (!(*i)->IsLeaf())
00163                 {
00164                     cout << "/";
00165                 }
00166             cout << endl;
00167         }
00168 }
00169 
00170 void CoreContext::Push()
00171 {
00172     if (mObject.get() != NULL)
00173         mObjectStack.push_front(mObject);
00174 }
00175 
00176 void CoreContext::Pop()
00177 {
00178     if (!mObjectStack.empty())
00179         {
00180             mObject = mObjectStack.front();
00181             mObjectStack.pop_front();
00182         }
00183 }
00184 
00185 void CoreContext::Dir() const
00186 {
00187     for (
00188          TObjectStack::const_iterator i = mObjectStack.begin();
00189          i != mObjectStack.end();
00190          ++i
00191          )
00192         {
00193             cout << (*i)->GetName();
00194             if (!(*i)->IsLeaf())
00195                 {
00196                     cout << "/";
00197                 }
00198             cout << endl;
00199         }
00200 }

Generated on Thu Apr 6 15:25:37 2006 for rcssserver3d by  doxygen 1.4.4