User Manual, Developers Guide and API Documentation

ConfigGetter.hpp

Go to the documentation of this file.
00001 /******************************************************************************
00002  * WiFiMac                                                                    *
00003  * This file is part of openWNS (open Wireless Network Simulator)
00004  * _____________________________________________________________________________
00005  *
00006  * Copyright (C) 2004-2007
00007  * Chair of Communication Networks (ComNets)
00008  * Kopernikusstr. 16, D-52074 Aachen, Germany
00009  * phone: ++49-241-80-27910,
00010  * fax: ++49-241-80-22242
00011  * email: info@openwns.org
00012  * www: http://www.openwns.org
00013  * _____________________________________________________________________________
00014  *
00015  * openWNS is free software; you can redistribute it and/or modify it under the
00016  * terms of the GNU Lesser General Public License version 2 as published by the
00017  * Free Software Foundation;
00018  *
00019  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00020  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00021  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00022  * details.
00023  *
00024  * You should have received a copy of the GNU Lesser General Public License
00025  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00026  *
00027  ******************************************************************************/
00028 
00029 #ifndef WIFIMAC_MANAGEMENT_PROTOCOLCALCULATORPLUGINS_CONFIGGETTER_HPP
00030 #define WIFIMAC_MANAGEMENT_PROTOCOLCALCULATORPLUGINS_CONFIGGETTER_HPP
00031 
00032 // must be the first include!
00033 #include <WNS/Python.hpp>
00034 #include <WNS/simulator/Bit.hpp>
00035 #include <WNS/Assure.hpp>
00036 
00037 namespace wifimac { namespace management { namespace protocolCalculatorPlugins {
00038 
00039     class ConfigGetter
00040     {
00041     public:
00042         ConfigGetter(PyObject* config_) :
00043             config(config_)
00044             {
00045                 //Py_XINCREF(config);
00046             };
00047 
00048         ~ConfigGetter()
00049             {
00050                 //Py_XDECREF(config);
00051             }
00052 
00053         template <class T>
00054         T
00055         get(const char* varName, const char* format) const
00056             {
00057                 T t;
00058                 assure(PyObject_HasAttrString(this->config, varName),
00059                        "Attribute " << varName << " not found");
00060                 PyObject* obj = PyObject_GetAttrString(this->config, varName);
00061                 if(not PyArg_Parse(obj, format, &t))
00062                 {
00063                     Py_DECREF(obj);
00064                     return 0;
00065                 }
00066                 Py_DECREF(obj);
00067                 return t;
00068             };
00069 
00070         ConfigGetter
00071         get(const char* varName) const
00072             {
00073                 assure(PyObject_HasAttrString(this->config, varName),
00074                        "Attribute " << varName << " not found");
00075                 PyObject* obj = PyObject_GetAttrString(this->config, varName);
00076                 ConfigGetter c(obj);
00077                 Py_DECREF(obj);
00078                 return c;
00079             };
00080 
00081         ConfigGetter
00082         get(const char* varName, int i) const
00083             {
00084                 assure(PyObject_HasAttrString(this->config, varName),
00085                        "Attribute " << varName << " not found");
00086                 PyObject* obj = PyObject_GetAttrString(this->config, varName);
00087                 int n = PyList_Size(obj);
00088                 assure(i < n, "List has size " << n << ", cannot get element " << i);
00089                 PyObject* item = PyList_GetItem(obj, i);
00090                 ConfigGetter c(item);
00091                 Py_DECREF(obj);
00092                 return c;
00093             };
00094 
00095         int
00096         length(const char* varName) const
00097             {
00098                 assure(PyObject_HasAttrString(this->config, varName),
00099                        "Attribute " << varName << " not found");
00100                 PyObject* obj = PyObject_GetAttrString(this->config, varName);
00101                 int n = PyList_Size(obj);
00102                 Py_DECREF(obj);
00103                 return n;
00104             }
00105 
00106     private:
00107         PyObject* config;
00108     };
00109 } // protocolCalculatorPlugins
00110 } // management
00111 } // wifimac
00112 
00113 #endif

Generated on Tue May 22 03:32:08 2012 for openWNS by  doxygen 1.5.5