Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Examples

ycpTools.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                                                                      |
00003 |                      __   __    ____ _____ ____                      |
00004 |                      \ \ / /_ _/ ___|_   _|___ \                     |
00005 |                       \ V / _` \___ \ | |   __) |                    |
00006 |                        | | (_| |___) || |  / __/                     |
00007 |                        |_|\__,_|____/ |_| |_____|                    |
00008 |                                                                      |
00009 |                               core system                            |
00010 |                                                        (C) SuSE GmbH |
00011 \----------------------------------------------------------------------/
00012 
00013   File:       ycpTools.h
00014 
00015   Author:     Michael Andres <ma@suse.de>
00016   Maintainer: Michael Andres <ma@suse.de>
00017 
00018   Purpose:
00019 
00020 /-*/
00021 #ifndef ycpTools_h
00022 #define ycpTools_h
00023 
00024 #include <iosfwd>
00025 #include <vector>
00026 #include <list>
00027 #include <string>
00028 
00029 #include <YCP.h>
00030 
00031 #include <y2util/Pathname.h>
00032 #include <y2util/Url.h>
00033 
00035 // convenience functions
00037 
00038 extern std::string asString( YCPValueType obj );
00039 extern std::ostream & operator<<( std::ostream & str, YCPValueType obj );
00040 
00041 extern std::string asString( const YCPValue & obj );
00042 extern std::ostream & operator<<( std::ostream & str, const YCPValue & obj );
00043 
00045 //
00046 //      CLASS NAME : YcpArgLoad
00050 class YcpArgLoad {
00051 
00052   friend std::ostream & operator<<( std::ostream & str, const YcpArgLoad & obj );
00053 
00054   YcpArgLoad & operator=( const YcpArgLoad & );
00055   YcpArgLoad            ( const YcpArgLoad & );
00056 
00057   public:
00058 
00060     //
00061     //  CLASS NAME : YcpArgLoad::YcpArg
00065     class YcpArg {
00066       protected:
00067         YCPValueType _type;
00068         virtual bool assign( const YCPValue & arg_r ) = 0;
00069       protected:
00070         YcpArg( YCPValueType type_r ) : _type( type_r ) {}
00071       public:
00072         virtual ~YcpArg() {}
00073         YCPValueType type() const { return _type; }
00074         enum Result { assigned = 0, wrongtype, badformat };
00075         Result load( const YCPValue & arg_r ) {
00076           if ( arg_r->valuetype() != _type ) {
00077             return wrongtype;
00078           }
00079           if ( ! assign( arg_r ) ) {
00080             return badformat;
00081           }
00082           return assigned;
00083         }
00084     };
00086 
00088     //
00089     //  CLASS NAME : YcpArgLoad::Value
00093     template<YCPValueType Ytype, typename Vtype>
00094     class Value : public YcpArg {
00095       protected:
00096         Vtype _value;
00097         virtual bool assign( const YCPValue & arg_r );
00098       public:
00099         Value()
00100           : YcpArg( Ytype )
00101         {}
00102         Value( const Vtype & value_r )
00103           : YcpArg( Ytype )
00104           , _value( value_r )
00105         {}
00106         virtual ~Value() {}
00107         operator Vtype &() { return _value; }
00108     };
00110 
00111   private:
00112 
00113     std::string          _fnc;
00114     std::vector<YcpArg*> _proto;
00115     unsigned             _optional;
00116 
00117     void append( YcpArg * narg ) {
00118       _proto.reserve( _proto.size() + 1 );
00119       _proto.push_back( narg );
00120     }
00121 
00122   public:
00123 
00124     YcpArgLoad( const std::string & fnc_r = "" )
00125       : _fnc( fnc_r )
00126       , _optional( 0 )
00127     {}
00128 
00129     ~YcpArgLoad() {
00130       for ( unsigned i = 0; i < _proto.size(); ++i ) {
00131         delete _proto[i];
00132       }
00133     }
00134 
00135   public:
00136 
00137     template<YCPValueType Ytype, typename Vtype>
00138     Vtype & arg() {
00139       Value<Ytype,Vtype> * narg = new Value<Ytype,Vtype>();
00140       append( narg );
00141       _optional = _proto.size();
00142       return *narg;
00143     }
00144 
00145     template<YCPValueType Ytype, typename Vtype>
00146     Vtype & arg( const Vtype & d ) {
00147       Value<Ytype,Vtype> * narg = new Value<Ytype,Vtype>( d );
00148       append( narg );
00149       return *narg;
00150     }
00151 
00152   public:
00153 
00154     bool load( const YCPList & args_r );
00155 };
00156 
00158 
00160 //
00161 // Common load templates
00162 //
00164 
00166 // YT_BOOLEAN
00168 inline bool YcpArgLoad::Value<YT_BOOLEAN, bool>::assign( const YCPValue & arg_r ) {
00169   _value = arg_r->asBoolean()->value();
00170   return true;
00171 }
00172 
00174 // YT_INTEGER
00176 inline bool YcpArgLoad::Value<YT_INTEGER, unsigned long long>::assign( const YCPValue & arg_r ) {
00177   _value = arg_r->asInteger()->value();
00178   return true;
00179 }
00180 inline bool YcpArgLoad::Value<YT_INTEGER, long long>::assign( const YCPValue & arg_r ) {
00181   _value = arg_r->asInteger()->value();
00182   return true;
00183 }
00184 inline bool YcpArgLoad::Value<YT_INTEGER, unsigned long>::assign( const YCPValue & arg_r ) {
00185   _value = arg_r->asInteger()->value();
00186   return true;
00187 }
00188 inline bool YcpArgLoad::Value<YT_INTEGER, long>::assign( const YCPValue & arg_r ) {
00189   _value = arg_r->asInteger()->value();
00190   return true;
00191 }
00192 inline bool YcpArgLoad::Value<YT_INTEGER, unsigned>::assign( const YCPValue & arg_r ) {
00193   _value = arg_r->asInteger()->value();
00194   return true;
00195 }
00196 inline bool YcpArgLoad::Value<YT_INTEGER, int>::assign( const YCPValue & arg_r ) {
00197   _value = arg_r->asInteger()->value();
00198   return true;
00199 }
00200 
00202 // YT_STRING
00204 inline bool YcpArgLoad::Value<YT_STRING, std::string>::assign( const YCPValue & arg_r ) {
00205   _value = arg_r->asString()->value();
00206   return true;
00207 }
00208 
00209 inline bool YcpArgLoad::Value<YT_STRING, Pathname>::assign( const YCPValue & arg_r )
00210 {
00211   _value = arg_r->asString()->value();
00212   return true;
00213 }
00214 
00215 inline bool YcpArgLoad::Value<YT_STRING, Url>::assign( const YCPValue & arg_r )
00216 {
00217   _value = arg_r->asString()->value();
00218   return true;
00219 }
00220 
00222 
00224 //
00225 // Common asYCP... functions
00226 //
00228 
00229 inline YCPList asYCPList( const std::list<std::string> & lst ) {
00230   YCPList ret;
00231   for ( std::list<std::string>::const_iterator it = lst.begin(); it != lst.end(); ++it ) {
00232     ret->add( YCPString( *it ) );
00233   }
00234   return ret;
00235 }
00236 
00237 
00239 
00240 #endif // ycpTools_h

Generated on Fri Nov 9 18:15:23 2007 for yast2-core by doxygen 1.3.6