00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00041 }
00042
00043 boost::shared_ptr<Leaf> CoreContext::New(const std::string& className, const std::string& pathStr)
00044 {
00045
00046 shared_ptr<Object> instance = mCore->New(className);
00047
00048 if (instance.get())
00049 {
00050
00051 shared_ptr<Leaf> leaf = shared_static_cast<Leaf>(instance);
00052
00053 if (leaf.get() != NULL)
00054 {
00055
00056
00057 if (Install(leaf, pathStr) == true)
00058 {
00059 mObject = leaf;
00060 return leaf;
00061 }
00062 }
00063 }
00064
00065
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
00098 Path path(pathStr);
00099
00100 if(! isNamed)
00101 {
00102
00103 if (path.IsEmpty())
00104 {
00105 return false;
00106 }
00107
00108 leaf->SetName(path.Back());
00109 path.PopBack();
00110
00111 }
00112
00113 shared_ptr<Leaf> current;
00114
00115
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 }