Author: sanel.z
Date: 2008-01-29 07:09:41 -0800 (Tue, 29 Jan 2008)
New Revision: 444
Log:
Replaced vector with own linked list.
Documentation/general style update.


Modified:
   trunk/Fl_CallbackPlus/Fl_CallbackPlus.h

Modified: trunk/Fl_CallbackPlus/Fl_CallbackPlus.h
===================================================================
--- trunk/Fl_CallbackPlus/Fl_CallbackPlus.h     2007-08-23 05:02:33 UTC (rev 
443)
+++ trunk/Fl_CallbackPlus/Fl_CallbackPlus.h     2008-01-29 15:09:41 UTC (rev 
444)
@@ -1,7 +1,7 @@
 /*
  * Fl_CallbackPlus, FLTK's callback wrapper.
  *
- * Copyright 2005-2006 by Sanel Zukan.
+ * Copyright 2005-2008 by Sanel Zukan.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -21,73 +21,69 @@
 #ifndef __FL_CALLBACKPLUS_H__
 #define __FL_CALLBACKPLUS_H__
 
-#include <vector>
 #include <assert.h>
 
 /**
-  \file Fl_CallbackPlus.h
-  \brief Extended callback mechanism
-*/
+ * \file Fl_CallbackPlus.h
+ * \brief Extended callback mechanism
+ */
 
 /**
-  \class Fl_CallbackBase
-  \brief Base for all callbacks
-
-  Fl_CallbackBase is pure virtual class used
-  as base for extended callbacks. Later all callbacks
-  can be registered using Fl_AddCallback function.
-
-  \see Fl_AddCallback
-
-  Here is sample how to use it:
-  \code
-  template <typename T>
-  class YourCommand : public Fl_CallbackBase
-  {
-       private:
-       typedef void (T::*Action)();
-       Action *method;
-       T *someobj;
-       public:
-       YourCommand(T* obj, Action m) : someobj(obj), method(m) { }
-       ~YourCommand() { }
-       virtual void Execute() 
-       {
-               // your code...
-               (someobj->*method)();
-               // your code...
-       }
-  };
-  \endcode
-
-  Later you use it as:
-  \code
-  YourCommand<App> *cmd = YourCommand<App>(app_obj, &App::Method);
-  Fl_AddCallback(cmd, SomeWidgetFromFLTK);
-  \endcode
-*/
-class Fl_CallbackBase
-{
+ * \class Fl_CallbackBase
+ * \brief Base for all callbacks
+ *
+ * Fl_CallbackBase is pure virtual class used
+ * as base for extended callbacks. Later all callbacks
+ * can be registered using Fl_AddCallback function.
+ *
+ * \see Fl_AddCallback
+ *
+ *  Here is the sample how to use it:
+ * \code
+ * template <typename T>
+ * class YourCommand : public Fl_CallbackBase  {
+ *        private:
+ *       typedef void (T::*Action)();
+ *           Action *method;
+ *       T *someobj;
+ *        public:
+ *           YourCommand(T* obj, Action m) : someobj(obj), method(m) { }
+ *           ~YourCommand() { }
+ *
+ *       virtual void Execute() {
+ *          // your code...
+ *          (someobj->*method)();
+ *              // your code...
+ *       }
+ * };
+ *\endcode
+ *
+ * Later you use it as:
+ * \code
+ * YourCommand<App> *cmd = YourCommand<App>(app_obj, &App::Method);
+ * Fl_AddCallback(cmd, SomeWidgetFromFLTK);
+ * \endcode
+ */
+class Fl_CallbackBase {
        protected:
                Fl_CallbackBase() { }
        public:
                virtual ~Fl_CallbackBase() { }
                /**
-                 This is empty method that should be implemented
-                 by interited classes.
-               */
+                * This is empty method that should be implemented
+                * by interited classes.
+                */
                virtual void Execute() = 0;
 };
 
-/** 
-  \class Fl_CallbackCommand
-  \brief Fl_CallbackPlus wrapper around standard FLTK callback.
-
-  This is templatized wrapper around FLTK callback function.
-*/
+/**
+ * \class Fl_CallbackCommand
+ * \brief Fl_CallbackPlus wrapper around standard FLTK callback.
+ * 
+ * This is templatized wrapper around FLTK callback function.
+ */
 template <typename ReceiverType>
-class Fl_CallbackCommand : public Fl_CallbackBase
-{
+class Fl_CallbackCommand : public Fl_CallbackBase {
        private:
                typedef void (ReceiverType::*Action)();
                ReceiverType *recv;
@@ -96,114 +92,133 @@
                Fl_CallbackCommand& operator=(const Fl_CallbackCommand&);
        public:
                /**
-                 Main callback driver.
-                 \param receiver Parent receiver.
-                 \param action Method that Parent should receive.
-                       
-                 This method will wrap FLTK callback function, suitable for 
-                 Fl_AddCallback method.
-
-                 It can be used as:
-                 \code
-                  Fl_CallbackCommand<MyApp> *cmd = new 
Fl_CallbackCommand<MyApp>(this, &MyApp::Method);
-                  Fl_Button *b = // ...
-                  Fl_AddCallback(cmd, b);
-                 \endcode
-
-                 \see Fl_AddCallback for more information.
-               */
+                * Main callback driver.
+                * \param receiver Parent receiver.
+                * \param action Method that Parent should receive.
+                * 
+                * This method will wrap FLTK callback function, suitable for 
+                * Fl_AddCallback method.
+                * 
+                * It can be used as:
+                * \code
+                * Fl_CallbackCommand<MyApp> *cmd = new 
Fl_CallbackCommand<MyApp>(this, &MyApp::Method);
+                * Fl_Button *b = // ...
+                * Fl_AddCallback(cmd, b);
+                * \endcode
+                * 
+                * \see Fl_AddCallback for more information.
+                */
                Fl_CallbackCommand(ReceiverType *receiver, Action action) : 
recv(receiver), func(action) { }
 
                ~Fl_CallbackCommand() { }
+
                virtual void Execute() { (recv->*func)(); }
 };
 
 
-static void private_function(Fl_Widget* w, void *c)
-{
+static void private_function(Fl_Widget* w, void *c) {
        assert(c != 0);
        Fl_CallbackBase *cmd = static_cast<Fl_CallbackBase*>(c);
        cmd->Execute();
 }
 
 /**
-  \fn void Fl_AddCallback(Fl_CallbackBase* command, WidgetType* widget)
-  \brief Function that will do proper callback registration.
-  \param command Wrapped object with enough information for callback execution
-  \param widget Widget that do callback
-
-  Fl_AddCallback is used to register callback with FLTK <i>callback</i> 
function passing
-  command parameter.
-*/
+ * \fn void Fl_AddCallback(Fl_CallbackBase* command, WidgetType* widget)
+ * \brief Function that will do proper callback registration.
+ * \param command Wrapped object with enough information for callback execution
+ * \param widget Widget that do callback
+ * 
+ * Fl_AddCallback is used to register callback with FLTK <i>callback</i> 
function passing
+ * command parameter.
+ */
 template <typename WidgetType>
-void Fl_AddCallback(Fl_CallbackBase* command, WidgetType* widget)
-{
+void Fl_AddCallback(Fl_CallbackBase* command, WidgetType* widget) {
        widget->callback(private_function, command);
 }
 
 /**
-  \class Fl_CallbackList
-  \brief Handy container with automatic deallocation
-
-  This container is used to remove needs for object alocation-deallocation
-  mechanism, providing automatic destruction of registered callbacks.
-*/
+ * \class Fl_CallbackList
+ * \brief Handy container with automatic deallocation
+ * 
+ * This container is used to remove needs for object alocation-deallocation
+ * mechanism, providing automatic destruction of registered callbacks.
+ */
 template <typename T>
-class Fl_CallbackList
-{
+class Fl_CallbackList {
        private:
                typedef Fl_CallbackCommand<T>* Fl_CallbackCommandType;
-               std::vector<Fl_CallbackCommandType> lst;
+
+               struct ListNode {
+                       ListNode* next;
+                       Fl_CallbackCommandType cb;
+               };
+
+               ListNode* lst;
+               ListNode* head;
+
                Fl_CallbackList(const Fl_CallbackList&);
                Fl_CallbackList& operator=(const Fl_CallbackList&);
        public:
                typedef void (T::*Method)();
 
                /**
-                 Method responsible for proper callback registration.
-                 \param obj Parent widget pointer
-                 \param method Method accesible with widget pointer
-                 \param w Widget that will do callback
-
-                 Method is used to register callback and add it to
-                 Fl_CallbackList.
-               */
+                * Method responsible for proper callback registration.
+                * \param obj Parent widget pointer
+                * \param method Method accesible with widget pointer
+                * \param w Widget that will do callback
+                * 
+                * Method is used to register callback and add it to
+                * Fl_CallbackList.
+                */
                template <typename Widget>
-                       void Fl_Callback(T* obj, Method method, Widget* w)
-                       {
-                               assert(obj != 0);
-                               assert(method != 0);
-                               assert(w != 0);
+               void Fl_Callback(T* obj, Method method, Widget* w) {
+                       assert(obj != 0);
+                       assert(method != 0);
+                       assert(w != 0);
 
-                               Fl_CallbackCommand<T> *c = new 
Fl_CallbackCommand<T>(obj, method);
-                               Fl_AddCallback(c, w);
-                               lst.push_back(c);
+                       Fl_CallbackCommand<T> *c = new 
Fl_CallbackCommand<T>(obj, method);
+                       Fl_AddCallback(c, w);
+
+                       ListNode* new_cb = new ListNode;
+                       new_cb->next = 0;
+
+                       if(!head) {
+                               head = new_cb;
+                               lst = head;
+                       } else {
+                               lst->next = new_cb;
+                               lst = lst->next;
                        }
+               }
 
                /**
-                 Wrapper around Fl_Callback method
-               */
+                * Wrapper around Fl_Callback method
+                */
                template <typename Widget>
-                       void operator()(T* obj, Method method, Widget* w)
-                       {
-                               Fl_Callback(obj, method, w);
-                       }
+               void operator()(T* obj, Method method, Widget* w) { 
Fl_Callback(obj, method, w); }
 
                /**
-                 Qt-like wrapper for Fl_Callback method
-               */
+                * Qt-like wrapper for Fl_Callback method
+                */
                template <typename Widget>
-                       void operator()(T* obj, Widget* w, Method method)
-                       {
-                               operator()(obj, method, w);
+               void operator()(T* obj, Widget* w, Method method) { 
operator()(obj, method, w); }
+
+               /**
+                * List constructor
+                */
+               Fl_CallbackList() : lst(0), head(0) { }
+
+               /**
+                * List destructor
+                */
+               ~Fl_CallbackList() {
+                       if(!head)
+                               return;
+
+                       for(ListNode* next; head; head = next) {
+                               next = head->next;
+                               delete head;
                        }
-
-               Fl_CallbackList() { }
-               ~Fl_CallbackList()
-               {
-                       unsigned int sz = lst.size();
-                       for(unsigned int i = 0; i < sz; i++)
-                               delete lst[i];
                }
 };
 

_______________________________________________
fltk-commit mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-commit

Reply via email to