User Manual, Developers Guide and API Documentation

Sequence.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/pyconfig/Sequence.hpp>
00031 #include <sstream>
00032 
00033 using namespace wns::pyconfig;
00034 
00035 Sequence::Sequence(Object _sequence) :
00036     sequence(_sequence),
00037     pathName("")
00038 
00039 {
00040     assure(_sequence.isSequence(), "Object is no sequence!");
00041     sequence.incref();
00042 } // Sequence
00043 
00044 Sequence::Sequence(Object _sequence,
00045            const std::string& _pathName) :
00046     sequence(_sequence),
00047     pathName(_pathName)
00048 
00049 {
00050     assure(_sequence.isSequence(), "Object is no sequence!");
00051     sequence.incref();
00052 } // Sequence
00053 
00054 Sequence::Sequence(const Sequence& other) :
00055     sequence(other.sequence),
00056     pathName(other.pathName)
00057 {
00058     sequence.incref();
00059 } // copy constructor
00060 
00061 
00062 Sequence::~Sequence()
00063 {
00064     sequence.decref();
00065 } // ~Sequence
00066 
00067 
00068 Sequence&
00069 Sequence::operator=(const Sequence& other)
00070 {
00071     sequence = other.sequence;
00072     sequence.incref();
00073 
00074     return *this;
00075 } // =
00076 
00077 
00078 bool
00079 Sequence::empty() const
00080 {
00081     return !PySequence_Length(sequence.obj_);
00082 } // empty
00083 
00084 
00085 int
00086 Sequence::size() const
00087 {
00088     return PySequence_Length(sequence.obj_);
00089 } // size
00090 
00091 
00092 bool
00093 Sequence::isSequenceAt(int n) const
00094 {
00095     Object obj = sequence.getItem(n);
00096     assure(!obj.isNull(), "This is the end, my friend.");
00097     if (obj.isSequence())
00098     {
00099         obj.decref();
00100         return true;
00101     }
00102     else
00103     {
00104         obj.decref();
00105         return false;
00106     }
00107 }
00108 
00109 Sequence
00110 Sequence::getSequenceAt(int n) const
00111 {
00112     Object obj = sequence.getItem(n);
00113     assure(isSequenceAt(n), "No sequence at pos:"<<n);
00114 
00115     if (!obj.isSequence())
00116     {
00117         obj.decref();
00118         Exception e;
00119         e << "Sequence: Item at pos:'" << n
00120           << "' is no nested sequence.\n\n";
00121         throw e;
00122     }
00123 
00124     std::stringstream ss;
00125     ss << pathName << "[" << n << "]";
00126     Sequence s(obj, ss.str());
00127     obj.decref();
00128     return s;
00129 } // getSequenceAt
00130 
00131 
00132 //
00133 // IterPolicy
00134 //
00135 
00136 Sequence::IterPolicy::IterPolicy() :
00137         iter(),
00138         nextObject()
00139 {
00140 } // end() constructor
00141 
00142 
00143 Sequence::IterPolicy::IterPolicy(Object sequence) :
00144         iter(),
00145         nextObject()
00146 {
00147     iter = Object(PyObject_GetIter(sequence.obj_));
00148     assure(!iter.isNull(), "Sequence::iterator called without sequence.");
00149 
00150     next();
00151 } // valid constructor
00152 
00153 
00154 Sequence::IterPolicy::IterPolicy(const IterPolicy& other) :
00155         iter(other.iter),
00156         nextObject(other.nextObject)
00157 {
00158     Py_XINCREF(iter.obj_);
00159     Py_XINCREF(nextObject.obj_);
00160 } // copy constructor
00161 
00162 
00163 Sequence::IterPolicy::~IterPolicy()
00164 {
00165     Py_XDECREF(iter.obj_);
00166     Py_XDECREF(nextObject.obj_);
00167 } // ~IterPolicy
00168 
00169 
00170 Sequence::IterPolicy&
00171 Sequence::IterPolicy::operator=(const IterPolicy& other)
00172 {
00173     iter = other.iter;
00174     nextObject = other.nextObject;
00175 
00176     Py_XINCREF(iter.obj_);
00177     Py_XINCREF(nextObject.obj_);
00178 
00179     return *this;
00180 } // =
00181 
00182 
00183 void
00184 Sequence::IterPolicy::next()
00185 {
00186     Py_XDECREF(nextObject.obj_);
00187     nextObject = Object(PyIter_Next(iter.obj_));
00188 } // next
00189 
00190 
00191 Object
00192 Sequence::IterPolicy::obj() const
00193 {
00194     return nextObject;
00195 } // obj
00196 
00197 
00198 Sequence
00199 Sequence::fromString(const std::string& s)
00200 {
00201     assure(!PyErr_Occurred(), "Dirty python context!");
00202 
00203     PyObject* pyDict = PyDict_New();
00204 
00205 #ifndef NDEBUG
00206     int result =
00207 #endif
00208         PyDict_SetItemString(pyDict, "__builtins__", PyImport_ImportModule("__builtin__"));
00209         assure(result==0, "Initializing __builtins__ failed!");
00210 
00211     Object o(PyRun_String(s.c_str(),
00212                   Py_eval_input,
00213                   pyDict,
00214                   pyDict));
00215 
00216     Py_DECREF(pyDict);
00217 
00218     if (o.isNull())
00219     {
00220         Exception e;
00221         e << "Erroneous Python Expression: \n\n" << s;
00222         throw e;
00223     }
00224 
00225     assure(o.isSequence(), "Python expression '" << s << "' is not a sequence!");
00226 
00227     Sequence seq(o, "<string>");
00228     o.decref();
00229     return seq;
00230 }
00231 
00232 

Generated on Fri May 25 03:31:51 2012 for openWNS by  doxygen 1.5.5