[vlc-devel] commit: Skins2: Replace some theme macros by templated code. (JP Dinger )

git version control git at videolan.org
Sat Dec 5 22:35:11 CET 2009


vlc | branch: master | JP Dinger <jpd at videolan.org> | Sat Nov 21 19:26:22 2009 +0100| [354811a1209fd23699749daf8ca3b3e68d45b061] | committer: JP Dinger 

Skins2: Replace some theme macros by templated code.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=354811a1209fd23699749daf8ca3b3e68d45b061
---

 modules/gui/skins2/src/theme.cpp |   94 ++++++++++++--------------------------
 modules/gui/skins2/src/theme.hpp |   39 +++++++++++-----
 2 files changed, 56 insertions(+), 77 deletions(-)

diff --git a/modules/gui/skins2/src/theme.cpp b/modules/gui/skins2/src/theme.cpp
index af7c2f9..97e38ea 100644
--- a/modules/gui/skins2/src/theme.cpp
+++ b/modules/gui/skins2/src/theme.cpp
@@ -17,9 +17,9 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 #include "theme.hpp"
@@ -159,77 +159,41 @@ void Theme::saveConfig()
 }
 
 
-// Useful macro
-#define FIND_OBJECT( mapData, mapName ) \
-    map<string, mapData>::const_iterator it; \
-    it = mapName.find( id ); \
-    if( it == mapName.end() ) \
-    { \
-        return NULL; \
-    } \
-    return (*it).second.get();
-
-// This macro takes an ID of the form "id1;id2;id3", and returns the object
+// Takes an ID of the form "id1;id2;id3", and returns the object
 // corresponding to the first valid ID. If no ID is valid, it returns NULL.
-// XXX: should we use a template method instead?
-#define FIND_FIRST_OBJECT( mapDataPtr, mapName ) \
-    string rightPart = id; \
-    string::size_type pos; \
-    do \
-    { \
-        pos = rightPart.find( ";" ); \
-        string leftPart = rightPart.substr( 0, pos ); \
-        map<string, mapDataPtr>::const_iterator it = mapName.find( leftPart ); \
-        if( it != mapName.end() ) \
-        { \
-            return (*it).second.get(); \
-            break; \
-        } \
- \
-        if( pos != string::npos ) \
-        { \
-            rightPart = rightPart.substr( pos, rightPart.size() ); \
-            rightPart = \
-                rightPart.substr( rightPart.find_first_not_of( " \t;" ), \
-                                  rightPart.size() ); \
-        } \
-    } \
-    while( pos != string::npos ); \
-    return NULL;
-
-GenericBitmap *Theme::getBitmapById( const string &id ) const
-{
-    FIND_FIRST_OBJECT( GenericBitmapPtr, m_bitmaps );
-}
-
-GenericFont *Theme::getFontById( const string &id ) const
-{
-    FIND_FIRST_OBJECT( GenericFontPtr, m_fonts );
-}
-
-Popup *Theme::getPopupById( const string &id ) const
+// XXX The string handling here probably could be improved.
+template<class T> typename T::pointer
+Theme::IDmap<T>::find_first_object( const string &id ) const
 {
-    FIND_OBJECT( PopupPtr, m_popups );
-}
+    string rightPart = id;
+    string::size_type pos;
+    do
+    {
+        pos = rightPart.find( ";" );
+        string leftPart = rightPart.substr( 0, pos );
 
-TopWindow *Theme::getWindowById( const string &id ) const
-{
-    FIND_OBJECT( TopWindowPtr, m_windows );
-}
+        typename T::pointer p = find_object( leftPart );
+        if( p ) return p;
 
-GenericLayout *Theme::getLayoutById( const string &id ) const
-{
-    FIND_OBJECT( GenericLayoutPtr, m_layouts );
+        if( pos != string::npos )
+        {
+            rightPart = rightPart.substr( pos, rightPart.size() );
+            rightPart =
+                rightPart.substr( rightPart.find_first_not_of( " \t;" ),
+                                  rightPart.size() );
+        }
+    }
+    while( pos != string::npos );
+    return NULL;
 }
 
-CtrlGeneric *Theme::getControlById( const string &id ) const
+GenericBitmap *Theme::getBitmapById( const string &id ) const
 {
-    FIND_OBJECT( CtrlGenericPtr, m_controls );
+    m_bitmaps.find_first_object( id );
 }
 
-Position *Theme::getPositionById( const string &id ) const
+GenericFont *Theme::getFontById( const string &id ) const
 {
-    FIND_OBJECT( PositionPtr, m_positions );
+    m_fonts.find_first_object( id );
 }
 
-
diff --git a/modules/gui/skins2/src/theme.hpp b/modules/gui/skins2/src/theme.hpp
index 8d11773..67eb601 100644
--- a/modules/gui/skins2/src/theme.hpp
+++ b/modules/gui/skins2/src/theme.hpp
@@ -58,29 +58,44 @@ public:
 
     GenericBitmap *getBitmapById( const string &id ) const;
     GenericFont *getFontById( const string &id ) const;
-    Popup *getPopupById( const string &id ) const;
-    TopWindow *getWindowById( const string &id ) const;
-    GenericLayout *getLayoutById( const string &id ) const;
-    CtrlGeneric *getControlById( const string &id ) const;
-    Position *getPositionById( const string &id ) const;
+
+#   define ObjByID( var ) ( const string &id ) const \
+        { return var.find_object( id ); }
+    Popup         *getPopupById    ObjByID( m_popups    )
+    TopWindow     *getWindowById   ObjByID( m_windows   )
+    GenericLayout *getLayoutById   ObjByID( m_layouts   )
+    CtrlGeneric   *getControlById  ObjByID( m_controls  )
+    Position      *getPositionById ObjByID( m_positions )
+#   undef  ObjById
 
     WindowManager &getWindowManager() { return m_windowManager; }
 
 private:
+    template<class T> class IDmap: public std::map<string, T> {
+    private:
+        typedef typename std::map<string, T> parent;
+    public:
+        typename T::pointer find_object(const string &id) const
+        {
+            typename parent::const_iterator it = parent::find( id );
+            return it!=parent::end() ? it->second.get() : NULL;
+        }
+        typename T::pointer find_first_object(const string &id) const;
+    };
     /// Store the bitmaps by ID
-    map<string, GenericBitmapPtr> m_bitmaps;
+    IDmap<GenericBitmapPtr> m_bitmaps;
     /// Store the fonts by ID
-    map<string, GenericFontPtr> m_fonts;
+    IDmap<GenericFontPtr> m_fonts;
     /// Store the popups by ID
-    map<string, PopupPtr> m_popups;
+    IDmap<PopupPtr> m_popups;
     /// Store the windows by ID
-    map<string, TopWindowPtr> m_windows;
+    IDmap<TopWindowPtr> m_windows;
     /// Store the layouts by ID
-    map<string, GenericLayoutPtr> m_layouts;
+    IDmap<GenericLayoutPtr> m_layouts;
     /// Store the controls by ID
-    map<string, CtrlGenericPtr> m_controls;
+    IDmap<CtrlGenericPtr> m_controls;
     /// Store the panel positions by ID
-    map<string, PositionPtr> m_positions;
+    IDmap<PositionPtr> m_positions;
     /// Store the commands
     list<CmdGenericPtr> m_commands;
     /// Store the Bezier curves




More information about the vlc-devel mailing list