Wexus2 0.20
wexus2.src/wexus/VarPath.h
00001 
00002 /*
00003  * Copyright (c) 2011 Aleksander B. Demko
00004  * This source code is distributed under the MIT license.
00005  * See the accompanying file LICENSE.MIT.txt for details.
00006  */
00007 
00008 #ifndef __INCLUDED_WEXUS_VARPATH_H__
00009 #define __INCLUDED_WEXUS_VARPATH_H__
00010 
00011 #include <QVariant>
00012 
00013 namespace wexus
00014 {
00015   class VarPath;
00016 
00017   /**
00018    * This is a powerful function that lets you
00019    * access the QVariantMap map within the v
00020    * (no copies!).
00021    *
00022    * Uses internal Qt access - might need
00023    * to be updated in the future.
00024    *
00025    * @author Aleksander Demko
00026    */ 
00027   QVariantMap & asVariantMap(QVariant &v);
00028 
00029   /**
00030    * This is a powerful function that lets you
00031    * access the QVariantMap map within the v
00032    * (no copies!).
00033    *
00034    * Uses internal Qt access - might need
00035    * to be updated in the future.
00036    *
00037    * @author Aleksander Demko
00038    */ 
00039   const QVariantMap & asVariantMap(const QVariant &v);
00040 
00041   /**
00042    * This prodices a simply QVariantMap QVariant
00043    * that consists of a key with the given value.
00044    *
00045    * @author Aleksander Demko
00046    */ 
00047   QVariantMap key(const QString &k, const QVariant &v);
00048 }
00049 
00050 /**
00051  * Merges the two QVariantMap together and returns the new, merged
00052  * one. Throws if left or right arent maps.
00053  *
00054  * @author Aleksander Demko
00055  */ 
00056 QVariantMap operator + (const QVariant &left, const QVariant &right);
00057 
00058 /**
00059  * A helper to make accessing and setting QVariantMap
00060  * easier.
00061  *
00062  * @author Aleksander Demko
00063  */ 
00064 class wexus::VarPath
00065 {
00066   public:
00067     class Exception : public std::exception {
00068       virtual const char* what() const throw ()
00069       { return "VarPath::Exception"; }
00070     };
00071     class NotFoundException : public Exception {};
00072     class MapRequiredException : public Exception {};
00073     class VariantRequiredException : public Exception {};
00074 
00075   public:
00076     /**
00077      * Createa a reference from a map
00078      * @author Aleksander Demko
00079      */ 
00080     VarPath(QVariantMap &srcmap);
00081 
00082     /**
00083      * Creates a reference from a variable.
00084      *
00085      * @author Aleksander Demko
00086      */ 
00087     VarPath(QVariant &v);
00088 
00089     /**
00090      * Returns a sub node. This reference must be set
00091      * to a map or a variant that is a map.
00092      *
00093      * @author Aleksander Demko
00094      */ 
00095     VarPath operator [](const QString &key);
00096 
00097     /**
00098      * Same as [], but Throws if field is not found.
00099      *
00100      * @author Aleksander Demko
00101      */ 
00102     VarPath operator ()(const QString &key);
00103 
00104     /**
00105      * Assignment operator.
00106      *
00107      * This reference must be set to a variant.
00108      *
00109      * @author Aleksander Demko
00110      */ 
00111     VarPath & operator = (const QVariant &v);
00112 
00113     /**
00114      * Returns this reference as a note.
00115      * This reference must be set to a variant.
00116      *
00117      * @author Aleksander Demko
00118      */ 
00119     QVariant & asVariant(void);
00120 
00121     /**
00122      * Returns this reference as a map.
00123      * This reference must be set to a map.
00124      *
00125      * @author Aleksander Demko
00126      */ 
00127     QVariantMap & asMap(void);
00128 
00129   protected:
00130     QVariantMap *dm_map;
00131     QVariant *dm_node;
00132 };
00133 
00134 #endif
00135 
00136 
 All Classes Namespaces Functions Variables Enumerations Enumerator