User Manual, Developers Guide and API Documentation

Context.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 16, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the 
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #include <WNS/Python.hpp>
00029 
00030 #include <WNS/probe/bus/Context.hpp>
00031 #include <WNS/Assure.hpp>
00032 
00033 
00034 using namespace wns::probe::bus;
00035 
00036 
00037 Context::Context():
00038     pyDict_(NULL)
00039 {
00040     assure(Py_IsInitialized(), "Python interpreter is not initialized!");
00041     pyDict_ = wns::pyconfig::Object(PyDict_New());
00042     assure(!pyDict_.isNull(), "Creation of Python Dict Failed");
00043 }
00044 
00045 Context::~Context()
00046 {
00047     assure(Py_IsInitialized(), "Python interpreter is not initialized!");
00048     assure(!pyDict_.isNull(), "No Python Object!");
00049     PyDict_Clear(pyDict_.obj_);
00050     pyDict_.decref();
00051 }
00052 
00053 void
00054 Context::insert(const std::string& key, int value)
00055 {
00056     insertInt(key, value);
00057 }
00058 
00059 void
00060 Context::insert(const std::string& key, const std::string& value)
00061 {
00062     insertString(key,value);
00063 }
00064 
00065 void
00066 Context::insertInt(const std::string& key, int value)
00067 {
00068     assure(Py_IsInitialized(), "Python interpreter is not initialized!");
00069     assure(!pyDict_.isNull(), "Creation of Python Dict Failed");
00070 
00071     // Check if entry with "key" already exists
00072     if ( this->knows(key) == true )
00073     {
00074         throw context::DuplicateKey();
00075     }
00076 
00077     PyObject* pyValue = PyInt_FromLong(value);
00078 
00079 #ifndef NDEBUG
00080     int result =
00081 #endif
00082         PyDict_SetItemString(pyDict_.obj_, key.c_str(), pyValue);
00083 
00084     assure(result==0, "Inserting of element into Dict failed!");
00085     Py_DECREF(pyValue);
00086 }
00087 
00088 void
00089 Context::insertString(const std::string& key, const std::string& value)
00090 {
00091     assure(Py_IsInitialized(), "Python interpreter is not initialized!");
00092     assure(!pyDict_.isNull(), "Creation of Python Dict Failed");
00093 
00094     // Check if entry with "key" already exists
00095     if ( this->knows(key) == true )
00096     {
00097         throw context::DuplicateKey();
00098     }
00099 
00100     PyObject* pyValue = PyString_FromString(value.c_str());
00101 
00102 #ifndef NDEBUG
00103     int result =
00104 #endif
00105         PyDict_SetItemString(pyDict_.obj_, key.c_str(), pyValue);
00106 
00107     assure(result==0, "Inserting of element into Dict failed!");
00108     Py_DECREF(pyValue);
00109 }
00110 
00111 bool
00112 Context::knows(const std::string& key) const
00113 {
00114     assure(Py_IsInitialized(), "Python interpreter is not initialized!");
00115     assure(!pyDict_.isNull(), "Creation of Python Dict Failed");
00116 
00117     PyObject* pyKey = PyString_FromString(key.c_str());
00118     int result = PyDict_Contains(pyDict_.obj_, pyKey);
00119 
00120     assure(result!=-1, "Error in PyDict_Contains occurred");
00121 
00122     Py_DECREF(pyKey);
00123 
00124     return result==1;
00125 }
00126 
00127 bool
00128 Context::isInt(const std::string& key) const
00129 {
00130     assure(Py_IsInitialized(), "Python interpreter is not initialized!");
00131     assure(!pyDict_.isNull(), "Creation of Python Dict Failed");
00132 
00133     PyObject* pyKey = PyString_FromString(key.c_str());
00134 
00135     // PyDict_GetItem returns a "borrowed" reference, which means we are not
00136     // responsible of DECREFING it.
00137     PyObject* pyValue = PyDict_GetItem(pyDict_.obj_, pyKey);
00138     Py_DECREF(pyKey);
00139 
00140     if (pyValue == NULL)
00141         throw context::NotFound();
00142 
00143     return PyInt_Check(pyValue);
00144 }
00145 
00146 int
00147 Context::getInt(const std::string& key) const
00148 {
00149     assure(Py_IsInitialized(), "Python interpreter is not initialized!");
00150     assure(!pyDict_.isNull(), "Creation of Python Dict Failed");
00151 
00152     PyObject* pyKey = PyString_FromString(key.c_str());
00153 
00154     // PyDict_GetItem returns a "borrowed" reference, which means we are not
00155     // responsible of DECREFING it.
00156     PyObject* pyValue = PyDict_GetItem(pyDict_.obj_, pyKey);
00157 
00158     if (pyValue == NULL)
00159         throw context::NotFound();
00160 
00161     if (PyInt_Check(pyValue) != true)
00162     {
00163         context::TypeError up;
00164         up << "Type mismatch. Object with key=" << key << " is not of type int";
00165         throw up;
00166     }
00167 
00168     long int value = PyInt_AsLong(pyValue);
00169 
00170     Py_DECREF(pyKey);
00171 
00172     return value;
00173 }
00174 
00175 bool
00176 Context::isString(const std::string& key) const
00177 {
00178     assure(Py_IsInitialized(), "Python interpreter is not initialized!");
00179     assure(!pyDict_.isNull(), "Creation of Python Dict Failed");
00180 
00181     PyObject* pyKey = PyString_FromString(key.c_str());
00182 
00183     // PyDict_GetItem returns a "borrowed" reference, which means we are not
00184     // responsible of DECREFING it.
00185     PyObject* pyValue = PyDict_GetItem(pyDict_.obj_, pyKey);
00186     Py_DECREF(pyKey);
00187 
00188     if (pyValue == NULL)
00189         throw context::NotFound();
00190 
00191     return PyString_Check(pyValue);
00192 }
00193 
00194 std::string
00195 Context::getString(const std::string& key) const
00196 {
00197     assure(Py_IsInitialized(), "Python interpreter is not initialized!");
00198     assure(!pyDict_.isNull(), "Creation of Python Dict Failed");
00199 
00200     PyObject* pyKey = PyString_FromString(key.c_str());
00201 
00202     // PyDict_GetItem returns a "borrowed" reference, which means we are not
00203     // responsible of DECREFING it.
00204     PyObject* pyValue = PyDict_GetItem(pyDict_.obj_, pyKey);
00205 
00206     if (pyValue == NULL)
00207         throw context::NotFound();
00208 
00209     if (PyString_Check(pyValue) != true)
00210     {
00211         context::TypeError up;
00212         up << "Type mismatch. Object with key=" << key << " is not of type string";
00213         throw up;
00214     }
00215 
00216     std::string value(PyString_AsString(pyValue));
00217 
00218     Py_DECREF(pyKey);
00219 
00220     return value;
00221 }
00222 
00223 std::string
00224 Context::doToString() const
00225 {
00226     assure(!pyDict_.isNull(), "NULL Python Object!");
00227 
00228     PyObject *key, *value;
00229 
00230     Py_ssize_t pos = 0;
00231 
00232     std::stringstream str;
00233     str << "{";
00234     while (PyDict_Next(pyDict_.obj_, &pos, &key, &value))
00235     {
00236         str << PyString_AsString(key) << " : ";
00237         if (PyInt_Check(value) == true)
00238         {
00239             str << PyInt_AsLong(value);
00240         }
00241         else if (PyString_Check(value) == true)
00242         {
00243             str << "'" << PyString_AsString(value) << "'";
00244         }
00245         else
00246         {
00247             str << "Unknown Object Type";
00248         }
00249         str << ",";
00250     }
00251     str << "}";
00252     return str.str();
00253 }

Generated on Tue May 22 03:31:46 2012 for openWNS by  doxygen 1.5.5