Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. // -*- c++ -*-
  2. /*
  3.  * Copyright 1999 Karl Nelson <kenelson@ece.ucdavis.edu>
  4.  *
  5.  * This library is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU Library General Public
  7.  * License as published by the Free Software Foundation; either
  8.  * version 2 of the License, or (at your option) any later version.
  9.  *
  10.  * This library is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * Library General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Library General Public
  16.  * License along with this library; if not, write to the Free
  17.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19. #ifndef SIGCXX_SCOPE_H
  20. #define SIGCXX_SCOPE_H
  21. #include <sigc++config.h>
  22.  
  23. #ifdef SIGC_CXX_NAMESPACES
  24. namespace SigC
  25. {
  26. #endif
  27.  
  28.  
  29. struct LIBSIGC_API ScopeNode
  30.   {
  31.    mutable ScopeNode *prev_;
  32.    mutable ScopeNode *next_;
  33.  
  34.    // removes self from list
  35.    void remove_self();
  36.  
  37.    // Called to inform the item that it is erased
  38.    virtual void erase();
  39.  
  40.    // inform scopes that invalid requested.
  41.    virtual void disconnect(bool destroy=0);
  42.  
  43.    ScopeNode()
  44. #ifdef LIBSIGC_WIN32
  45.    {prev_=next_=this;}
  46. #else
  47.    :prev_(this),next_(this) {}
  48. #endif
  49.  
  50.    virtual ~ScopeNode();
  51.  
  52.    private:
  53.      ScopeNode& operator=(const ScopeNode&);
  54.      ScopeNode(const ScopeNode&);
  55.   };
  56.  
  57. struct LIBSIGC_API DataNode: public ScopeNode
  58.   {
  59.    virtual void erase();
  60.    virtual ~DataNode();
  61.   };
  62.  
  63. /*******************************************************************
  64. ***** Basis Scope
  65. *******************************************************************/
  66. class ObjectScoped;
  67. class ObjectReferenced;
  68. class Object;
  69. class Scope;
  70.  
  71. class LIBSIGC_API Reference
  72.   {
  73.    protected:
  74.      mutable ObjectReferenced* obj_;
  75.      mutable void* cache_;
  76.  
  77.    public:
  78.      void set_sink();
  79.  
  80.      void init(ObjectReferenced* obj);
  81.      void set(ObjectReferenced* obj,void* cache=0,bool ptr=false);
  82.  
  83.      Reference& operator=(ObjectReferenced *obj) { set(obj); return *this; }
  84.      Reference& operator=(ObjectReferenced &obj) { set(&obj); return *this; }
  85.      Reference& operator=(const Reference& ref)  { set(ref.obj_); return *this; };
  86.  
  87.      ObjectReferenced* object() const {return obj_;}
  88.      void* cache() const {return cache_;}
  89.  
  90.      Reference():obj_(0) {}
  91.      Reference(ObjectReferenced &obj)
  92.        {init(&obj);}
  93.      Reference(const Reference& ref)
  94.        {init(ref.obj_);}
  95.      ~Reference();
  96.   };
  97.      
  98. class LIBSIGC_API Scope:public ScopeNode
  99.   {
  100.    friend class ObjectScoped;
  101.  
  102.      Scope& operator=(const Scope& scope);
  103.      Scope(const Scope& scope);
  104.  
  105.    protected:
  106.      void set(ObjectScoped* obj,void* cache,bool ptr);
  107.      mutable ObjectScoped* obj_;
  108.      mutable void* cache_;
  109.  
  110.      virtual void on_connect()=0;
  111.      virtual void erase();
  112.  
  113.      void register_scope(ObjectScoped *);
  114.      void register_scope(const Scope *parent=0);
  115.      void unregister_scope();
  116.  
  117.    public:
  118.  
  119.      void reference();
  120.      void unreference();
  121.      void set_sink();
  122.  
  123.      ObjectScoped* object() const {return (ObjectScoped*)(obj_);}
  124.      void* cache() const {return cache_;}
  125.  
  126.      // Inform object it should invalidate its list.
  127.      void invalid();
  128.  
  129.      Scope():obj_(0),cache_(0) {}
  130.      virtual ~Scope();
  131.   };
  132.  
  133.  
  134. /******************************************************
  135. **** Common Scopes
  136. *******************************************************
  137.   Available Scopes:
  138.     Uncounted  - non-reference
  139.     Limit      - Limits the lifetime of object to this scope
  140.                  Sinks object.
  141.     Extend     - Extends the lifetime of the object to this scope
  142.                  Sinks object.
  143.     LimitOwned - Conditionally limits the lifetime of object
  144.                  Sinks object.
  145.     FuncRef    - Extends the lifetime, without sink
  146.                  (intended for functions)
  147.     Reference  - Extends the lifetime, with sink
  148.    
  149.     AutoPtr    - Shorthand for auto_ptr like scope.
  150.     RefCount   - Shorthand for ref_ptr like scope.
  151.        
  152. ******************************************************/
  153. struct Scopes
  154. {
  155.  
  156. class LIBSIGC_API Uncounted:public Scope
  157.   {
  158.      Uncounted& operator=(const Uncounted&);
  159.      Uncounted(const Uncounted&);
  160.    public:
  161.      virtual void disconnect(bool level=0);
  162.      Uncounted():Scope() {}
  163.      virtual ~Uncounted();
  164.   };
  165.  
  166. class LIBSIGC_API Extend:public Scope
  167.   {
  168.   public:
  169.      Extend& operator=(const Extend&);
  170.      Extend(const Extend&);
  171.    protected:
  172.      virtual void on_connect();
  173.      virtual void erase();
  174.    public:
  175.      virtual void disconnect(bool level=0);
  176.      void set(ObjectScoped* obj,void* cache,bool ptr);
  177.      Extend():Scope() {}
  178.      virtual ~Extend();
  179.   };
  180.  
  181. class LIBSIGC_API Limit:public Scope
  182.   {
  183.      Limit& operator=(const Limit&);
  184.      Limit(const Limit&);
  185.    protected:
  186.      virtual void on_connect();
  187.      virtual void erase();
  188.    public:
  189.      virtual void disconnect(bool level=0);
  190.      void set(ObjectScoped* obj,void* cache,bool ptr);
  191.      Limit():Scope() {}
  192.      virtual ~Limit();
  193.   };
  194.  
  195. typedef Extend RefCount;
  196. typedef Reference Lock;
  197. };
  198.  
  199. /*************************************************************
  200. ***** Lists
  201. *************************************************************/
  202. // Stub for building polylists
  203.  
  204.  
  205. // Iterator skeleton
  206. struct LIBSIGC_API ScopeIterator_
  207.   {
  208.    typedef ScopeNode NodeType;
  209.    private:
  210.      NodeType *node_;
  211.    public:
  212.  
  213.    inline NodeType* node()             {return node_;}
  214.    inline const NodeType* node() const {return node_;}
  215.  
  216.    inline NodeType& operator*()
  217.      {return *node_;
  218.      }
  219.    inline const NodeType& operator*() const
  220.      {return *node_;
  221.      }
  222.    
  223.    inline bool operator==(const ScopeIterator_& i) const
  224.      {return node_==i.node_;
  225.      }
  226.    inline bool operator!=(const ScopeIterator_& i) const
  227.      {return node_!=i.node_;
  228.      }
  229.  
  230.    inline ScopeIterator_& operator++()
  231.      {
  232.       if (node_)
  233.         node_=(NodeType*)node_->next_;
  234.       return *this;
  235.      }
  236.  
  237.    ScopeIterator_ operator++(int)
  238.      {
  239.       ScopeIterator_ tmp=*this;
  240.       ++*this;
  241.       return tmp;
  242.      }
  243.  
  244.    ScopeIterator_& operator= (const ScopeIterator_& i)
  245.      {
  246.       node_=i.node_;
  247.       return *this;
  248.      }
  249.  
  250.    ScopeIterator_(const ScopeIterator_ &n):node_(n.node_) {}
  251.    ScopeIterator_(NodeType *n):node_(n) {}
  252.    ScopeIterator_():node_(0) {}
  253.   };
  254.  
  255. class LIBSIGC_API ScopeList
  256.   {
  257.    public:
  258.    typedef ScopeNode NodeType;
  259.    typedef ScopeIterator_ Iterator;
  260.  
  261.    ScopeNode node_;  
  262.  
  263.    inline Iterator begin()             {return Iterator(node_.next_);}
  264.    inline Iterator end()               {return Iterator(&node_);}
  265.  
  266.    // insert item directly on list
  267.    Iterator insert_direct(Iterator pos,NodeType *n);
  268.  
  269.    Iterator erase(Iterator pos);
  270.    void erase(Iterator start,Iterator stop)
  271.      { while (start!=stop) start=erase(start); }
  272.    void swap_elements(Iterator p1,Iterator p2);
  273.  
  274.    void clear()
  275.      { erase(begin(),end()); }
  276.  
  277.    bool empty() const {return node_.next_==&node_;}
  278.    
  279.    ScopeList():node_() {}
  280.    ~ScopeList() { clear(); }
  281.  
  282.    private:
  283.      ScopeList(const ScopeList&);
  284.   };
  285.  
  286.  
  287.  
  288. #ifdef SIGC_CXX_NAMESPACES
  289. } // namespace sigc
  290. #endif
  291.  
  292. #endif
  293.