home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / warphead.zip / H / REFCTCOL.H < prev    next >
C/C++ Source or Header  |  1997-02-28  |  11KB  |  325 lines

  1. //====START_GENERATED_PROLOG======================================
  2. //
  3. //
  4. //   COMPONENT_NAME: odutils
  5. //
  6. //   CLASSES:   ODObjectOrdColl
  7. //        ODRefCntCollection
  8. //        ODRefCntCollectionIterator
  9. //
  10. //   ORIGINS: 82,27
  11. //
  12. //
  13. //   (C) COPYRIGHT International Business Machines Corp. 1995,1996
  14. //   All Rights Reserved
  15. //   Licensed Materials - Property of IBM
  16. //   US Government Users Restricted Rights - Use, duplication or
  17. //   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  18. //       
  19. //   IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  20. //   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21. //   PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  22. //   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  23. //   USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  24. //   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  25. //   OR PERFORMANCE OF THIS SOFTWARE.
  26. //
  27. //====END_GENERATED_PROLOG========================================
  28. //
  29. // @(#) 1.3 com/src/utils/include/RefCtCol.h, odutils, od96os2, odos29646d 7/15/96 18:01:58 [ 11/15/96 15:29:34 ]
  30. /*
  31.     File:        RefCtCol.h
  32.  
  33.     Contains:    OrderedCollection of ODRefCntObject
  34.  
  35.     Owned by:    David McCusker
  36.  
  37.     Copyright:    ⌐ 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  38.  
  39.     
  40.     In Progress:
  41.         
  42. */
  43.  
  44. #ifndef _REFCTCOL_
  45. #define _REFCTCOL_
  46.  
  47. #ifndef _ODTYPES_
  48. #include "ODTypes.h"
  49. #endif
  50.  
  51. #ifndef _ODUTILS_
  52. #include <ODUtils.h>
  53. #endif
  54.  
  55. #ifndef _PLFMDEF_
  56. #include "PlfmDef.h"
  57. #endif
  58.  
  59. #ifndef _ODMEMORY_
  60. #include "ODMemory.h"
  61. #endif
  62.  
  63. #ifndef _ORDCOLL_
  64. #include "OrdColl.h"
  65. #endif
  66.  
  67. //==============================================================================
  68. // Theory of Operation
  69. //==============================================================================
  70.  
  71. // ODRefCntCollection is an ordered collection of ODRefCntObject.  Adding to
  72. // the collection always performs an Acquire(), but removing generally does not
  73. // (presumably you sometimes wish to perform actions on removed elements).
  74. // Duplicates are allowed.
  75. //
  76. // ODRefCntCollection is not intended to be subclassable.
  77.  
  78. //==============================================================================
  79. // Constants
  80. //==============================================================================
  81.  
  82. //==============================================================================
  83. // Scalar Types
  84. //==============================================================================
  85.  
  86. //=====================================================================================
  87. // Classes defined in this interface
  88. //=====================================================================================
  89.  
  90. class ODRefCntCollection;    // An ordered (not sorted) collection of ODRefCntObject
  91. class ODRefCntCollectionIterator;
  92.  
  93. //=====================================================================================
  94. // Classes used by this interface
  95. //=====================================================================================
  96.  
  97. class ODObjectOrdColl : public OrderedCollection
  98. {
  99.     public:
  100.         ODObjectOrdColl() : OrderedCollection()    {}
  101.         ODObjectOrdColl(ODMemoryHeapID where) : OrderedCollection(where) {}
  102.         ~ODObjectOrdColl();
  103.  
  104.         ODMethod ODBoolean ElementsMatch(ElementType v1,ElementType v2) const;
  105.         // match using ODObjectsAreEqual()
  106. };
  107.  
  108. //=====================================================================================
  109. // Global Variables
  110. //=====================================================================================
  111.  
  112. //=====================================================================================
  113. // Class OrderedCollection
  114. //=====================================================================================
  115.  
  116. class ODRefCntCollection
  117. {
  118. public:
  119.  
  120.     ODRefCntCollection(Environment* ev);
  121.     ODRefCntCollection(Environment* ev, ODMemoryHeapID where);
  122.         // The environment is needed by the destructor, so we just pass
  123.         // it in to the constructor rather than to every method that needs
  124.         // to acquire or release.
  125.         
  126.     ~ODRefCntCollection();
  127.  
  128.     ODULong          Count() const;
  129.     
  130.     void             AddFirstAndAcquire(ODRefCntObject* element);
  131.     void             AddLastAndAcquire(ODRefCntObject* element);  
  132.         // AddFirst() and AddLast acquire element.
  133.     
  134.     void             AddBeforeAndAcquire(
  135.                         ODRefCntObject* existing, 
  136.                         ODRefCntObject* tobeadded);
  137.     void             AddAfterAndAcquire(
  138.                         ODRefCntObject* existing, 
  139.                         ODRefCntObject* tobeadded);
  140.         // AddBefore() and AddAfter() acquire tobeadded.
  141.  
  142.     ODRefCntObject*     After(ODRefCntObject* existing) const;
  143.     ODRefCntObject*     Before(ODRefCntObject* existing) const;
  144.  
  145.     ODRefCntObject*     First() const;
  146.     ODRefCntObject*     Last() const;
  147.         // Returns kODNULL if there is no first element.
  148.  
  149.     ODRefCntObject*     RemoveFirst();
  150.     ODRefCntObject*     RemoveLast();  
  151.         // Returns kODNULL if there is no remaming elements.
  152.         // Does not release returned element.
  153.         // You must release what RemoveFirst() or RemoveLast() returns.
  154.         
  155.     void             RemoveAndReleaseAll();
  156.         // Called by the destructor: removes and releases all elements.
  157.         
  158.     ODBoolean        Remove(ODRefCntObject* existing); 
  159.         // Returns true if existing was actually removed.
  160.         // Does not release existing. You must eventually release 
  161.         // existing object if it was actually removed.
  162.     
  163.     ODBoolean        RemoveAndRelease(ODRefCntObject* existing);
  164.         // Returns true if existing was actually removed.
  165.         // If actually removed, existing is also released.
  166.         // (So it is unnecessay to call Contains() to decide
  167.         // whether to release.)
  168.     
  169.     ODBoolean        Contains(ODRefCntObject* existing) const;
  170.     
  171.     // ODRefCntCollectionIterator* CreateIterator();
  172.         // Most of the time you will be better off creating an instance
  173.         // of ODRefCntCollectionIterator on the stack, e.g.:
  174.         // ODRefCntCollectionIterator iter(collection);
  175.     
  176.     ODMemoryHeapID GetHeap() const;
  177.  
  178. private:
  179.     ODObjectOrdColl  fCol;
  180.     Environment*     fEv; 
  181.  
  182.     friend class ODRefCntCollectionIterator;
  183. };
  184.  
  185.  
  186. inline ODMemoryHeapID ODRefCntCollection::GetHeap() const 
  187. {
  188.     return fCol.OrderedCollection::GetHeap( );
  189. }
  190.  
  191. //------------------------------------------------------------------------------
  192. // ODRefCntCollection::Count
  193. //------------------------------------------------------------------------------
  194.  
  195. inline ODULong ODRefCntCollection::Count() const
  196. {
  197.     return fCol.OrderedCollection::Count( );
  198. }
  199.  
  200. //------------------------------------------------------------------------------
  201. // ODRefCntCollection::After
  202. //------------------------------------------------------------------------------
  203.  
  204. inline ODRefCntObject* ODRefCntCollection::After(ODRefCntObject* existing) const
  205. {
  206.     return (ODRefCntObject*) fCol.OrderedCollection::Before(existing);
  207. }
  208.  
  209. //------------------------------------------------------------------------------
  210. // ODRefCntCollection::Before
  211. //------------------------------------------------------------------------------
  212.  
  213. inline ODRefCntObject* ODRefCntCollection::Before(ODRefCntObject* existing) const
  214. {
  215.     return (ODRefCntObject*) fCol.OrderedCollection::Before(existing);
  216. }
  217.  
  218. //------------------------------------------------------------------------------
  219. // ODRefCntCollection::First
  220. //------------------------------------------------------------------------------
  221.  
  222. inline ODRefCntObject* ODRefCntCollection::First() const
  223. {
  224.     return (ODRefCntObject*) fCol.OrderedCollection::First();
  225. }
  226.  
  227. //------------------------------------------------------------------------------
  228. // ODRefCntCollection::Last
  229. //------------------------------------------------------------------------------
  230.  
  231. inline ODRefCntObject* ODRefCntCollection::Last() const
  232. {
  233.     return (ODRefCntObject*) fCol.OrderedCollection::Last();
  234. }
  235.  
  236. //------------------------------------------------------------------------------
  237. // ODRefCntCollection::RemoveFirst
  238. //------------------------------------------------------------------------------
  239.  
  240. inline ODRefCntObject*    ODRefCntCollection::RemoveFirst()
  241. {
  242.     return (ODRefCntObject*) fCol.OrderedCollection::RemoveFirst();
  243. }
  244.  
  245. //------------------------------------------------------------------------------
  246. // ODRefCntCollection::RemoveLast
  247. //------------------------------------------------------------------------------
  248.  
  249. inline ODRefCntObject*    ODRefCntCollection::RemoveLast()
  250. {
  251.     return (ODRefCntObject*) fCol.OrderedCollection::RemoveLast();
  252. }
  253.  
  254. //------------------------------------------------------------------------------
  255. // ODRefCntCollection::Remove
  256. //------------------------------------------------------------------------------
  257.  
  258. inline ODBoolean ODRefCntCollection::Remove(ODRefCntObject* existing)
  259.     // Returns true if existing was actually removed.
  260.     // Does not release existing. You must eventually release 
  261.     // existing object if it was actually removed.
  262. {
  263.     return fCol.OrderedCollection::Remove(existing);
  264. }
  265.  
  266. //------------------------------------------------------------------------------
  267. // ODRefCntCollection::Contains
  268. //------------------------------------------------------------------------------
  269.  
  270. inline ODBoolean ODRefCntCollection::Contains(ODRefCntObject* existing) const
  271. {
  272.     return fCol.OrderedCollection::Contains(existing);
  273. }
  274.  
  275. //=====================================================================================
  276. // Class OrderedCollectionIterator
  277. //=====================================================================================
  278.  
  279. class ODRefCntCollectionIterator {
  280. public:
  281.     ODRefCntCollectionIterator(ODRefCntCollection* collection);
  282.     ~ODRefCntCollectionIterator();
  283.     ODRefCntObject* First();
  284.     ODRefCntObject* Next();
  285.     ODRefCntObject* Last();
  286.     ODRefCntObject* Previous();
  287.     ODBoolean   IsNotComplete();
  288.     void        RemoveCurrent();
  289.     
  290. private:
  291.       OrderedCollectionIterator fIter;
  292. };
  293.  
  294. inline ODRefCntObject* ODRefCntCollectionIterator::First()
  295. {
  296.     return (ODRefCntObject*) fIter.First();
  297. }
  298.  
  299. inline ODRefCntObject* ODRefCntCollectionIterator::Next()
  300. {
  301.     return (ODRefCntObject*) fIter.Next();
  302. }
  303.  
  304. inline ODRefCntObject* ODRefCntCollectionIterator::Last()
  305. {
  306.     return (ODRefCntObject*) fIter.Last();
  307. }
  308.  
  309. inline ODRefCntObject* ODRefCntCollectionIterator::Previous()
  310. {
  311.     return (ODRefCntObject*) fIter.Previous();
  312. }
  313.  
  314. inline ODBoolean ODRefCntCollectionIterator::IsNotComplete()
  315. {
  316.     return fIter.IsNotComplete();
  317. }
  318.  
  319. inline void ODRefCntCollectionIterator::RemoveCurrent()
  320. {
  321.     fIter.RemoveCurrent();
  322. }
  323.  
  324. #endif // _REFCTCOL_
  325.