User Manual, Developers Guide and API Documentation

View.hpp

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 #ifndef WNS_PYCONFIG_VIEW_HPP
00029 #define WNS_PYCONFIG_VIEW_HPP
00030 
00031 #include <WNS/pyconfig/Sequence.hpp>
00032 #include <WNS/pyconfig/Object.hpp>
00033 #include <WNS/TypeInfo.hpp>
00034 
00035 #include <sstream>
00036 #include <string>
00037 
00038 namespace wns { namespace pyconfig {
00039 
00040     namespace tests {
00041         class ViewTest;
00042     }
00043 
00048     class View
00049     {
00050         friend class tests::ViewTest;
00051 
00052     public:
00053         View(const View& other, const std::string& newViewExpression);
00054         View(const View& other, const std::string& newViewExpression, int at);
00055         View(const Sequence& other, int at);
00056 
00057         virtual
00058         ~View();
00059 
00065         pyconfig::View
00066         getView(const std::string& newViewExpression) const;
00067 
00074         pyconfig::Sequence
00075         getSequence(const std::string& sequenceExpression) const;
00076 
00082         View
00083         getView(const std::string& newViewExpression, int at) const;
00084 
00090         template <typename S>
00091         S
00092         getSequence(const std::string& optionExpression) const
00093         {
00094             S container;
00095             for(int i = 0; i < len(optionExpression); ++i) {
00096                 container.push_back(get<typename S::value_type>(optionExpression, i));
00097             }
00098             return container;
00099         } // get
00100 
00101 
00102         template <typename T>
00103         bool
00104         get(T& value, const std::string& optionExpression) const
00105         {
00106             Object o = getObject(optionExpression);
00107             if(o.isNull())
00108             {
00109                 return false;
00110             }
00111 
00112             convert(value, o, optionExpression);
00113 
00114             o.decref();
00115             return true;
00116         } // get
00117 
00118         template <typename T>
00119         bool
00120         get(T& value, const std::string& optionExpression, int at) const
00121         {
00122             Object o = getObject(optionExpression, at);
00123             if(o.isNull())
00124             {
00125                 return false;
00126             }
00127 
00128             convert(value, o, optionExpression);
00129 
00130             o.decref();
00131             return true;
00132         } // get[at]
00133 
00134 
00135         bool
00136         get(View& value, const std::string& optionExpression) const
00137         {
00138             Object o = getObject(optionExpression);
00139             if(o.isNull())
00140             {
00141                 return false;
00142             }
00143 
00144             checkIsNotNone<View>(o, optionExpression);
00145 
00146             o.decref();
00147 
00148             value = View(*this, optionExpression);
00149             return true;
00150         } // get
00151 
00152 
00153         bool
00154         get(View& value, const std::string& optionExpression, int at) const
00155         {
00156             Object o = getObject(optionExpression, at);
00157             if(o.isNull())
00158             {
00159                 return false;
00160             }
00161 
00162             o.decref();
00163 
00164             std::stringstream element;
00165             element << optionExpression << "[" << at << "]";
00166             checkIsNotNone<View>(o, element.str());
00167 
00168             value = View(*this, optionExpression, at);
00169             return true;
00170         } // get
00171 
00172 
00173         template <typename T>
00174         T
00175         get(const std::string& optionExpression) const
00176         {
00177             T t;
00178             if(!get(t, optionExpression)) {
00179                 couldntRetrieve(optionExpression);
00180                 showdown("Unknown thang in pyconfig::View::get.");
00181             }
00182 
00183             return t;
00184         } // get
00185 
00186 
00187         template <typename T>
00188         T
00189         get(const std::string& optionExpression, int at) const
00190         {
00191             T t;
00192             if(!get(t, optionExpression, at)) {
00193                 couldntRetrieve(optionExpression);
00194                 showdown("Unknown thang in pyconfig::View::get.");
00195             }
00196 
00197             return t;
00198         } // get[at]
00199 
00200         pyconfig::View
00201         get(const std::string& optionExpression) const
00202         {
00203             return get<View>(optionExpression);
00204         } // get
00205 
00206 
00207         pyconfig::View
00208         get(const std::string& optionExpression, int at) const
00209         {
00210             return get<View>(optionExpression, at);
00211         } // get[at]
00212 
00213         bool
00214         len(int& result, const std::string& optionExpression) const;
00215 
00216         int
00217         len(const std::string& optionExpression) const;
00218 
00222         long long int
00223         getId() const;
00224 
00225         bool
00226         knows(const std::string& optionExpression) const;
00227 
00228         bool
00229         isNone(const std::string& expression) const;
00230 
00231         bool
00232         isSequence(const std::string& expression) const;
00233 
00234         std::string
00235         context() const;
00236 
00237         void
00238         patch(const std::string& expression);
00239 
00240         // reference counting copy constructor / assignment operator
00241         View(const View& other);
00242         View& operator=(const View& other);
00243 
00244         // Operators
00245         bool
00246         operator==(const View& other) const;
00247 
00248         bool
00249         operator!=(const View& other) const;
00250 
00251         bool
00252         operator<(const View& other) const;
00253 
00254         friend std::ostream&
00255         operator<<(std::ostream& str, const View& other)
00256         {
00257             str << other.context() << ":\n"
00258                 << other.asString();
00259             return str;
00260         }
00261 
00262         Object
00263         getObject(const std::string& optionExpression) const;
00264 
00265         Object
00266         getObject(const std::string& optionExpression, int at) const;
00267 
00268         protected:
00269 
00270                 void
00271                 init(View& other, const std::string& viewExpression);
00272 
00273         template <typename T>
00274         void
00275         checkIsNotNone(Object o, const std::string& optionExpression) const
00276         {
00277             if(o.isNone()) {
00278                 Exception e;
00279                 e << "Tried to convert "
00280                   << this->context() << "::"
00281                   << optionExpression
00282                   << " to " << TypeInfo::create<T>()
00283                   << ", but value is 'None'\n";
00284                 throw e;
00285             }
00286         }
00287 
00288         template <typename T>
00289         bool
00290         convert(T& value, Object o, const std::string& optionExpression) const
00291         {
00292             checkIsNotNone<T>(o, optionExpression);
00293             return this->doConvert(value, o);
00294         }
00295 
00296         template <typename T>
00297         bool
00298         doConvert(T &value, Object o) const
00299         {
00300             if (!o.isConvertibleToString())
00301             {
00302                 return false;
00303             }
00304 
00305             std::istringstream os(o.toString());
00306             os >> value;
00307 
00308             return true;
00309         } // convert
00310 
00311         // boolean converter
00312         bool
00313         doConvert(bool& value, Object o) const;
00314 
00315         // std::string converter
00316         bool
00317         doConvert(std::string& value, Object o) const;
00318 
00323         void
00324         couldntRetrieve(const std::string& optionExpression) const;
00325 
00332         void
00333         showdown(const std::string& reason, bool raise = true) const;
00334 
00335 
00339         std::string
00340         asString() const;
00341 
00342         Object dict;
00343 
00344         std::string viewExpression;
00345 
00346         View();
00347 
00348     private:
00352         void
00353         initializePython();
00354 
00358         void
00359         finalizePython();
00360 
00365         int&
00366         getCount();
00367     };
00368 }}
00369 
00370 
00371 #endif // NOT defined WNS_PYCONFIG_VIEW_HPP
00372 
00373 
00374 

Generated on Sat May 26 03:31:48 2012 for openWNS by  doxygen 1.5.5