0023846: A crash on reading of a VRML file with wrong indices
[occt.git] / src / NCollection / NCollection_BaseCollection.hxx
1 // Created on: 2002-04-09
2 // Created by: Alexander KARTOMIN (akm)
3 // Copyright (c) 2002-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21 #ifndef NCollection_BaseCollection_HeaderFile
22 #define NCollection_BaseCollection_HeaderFile
23
24 #include <NCollection_IncAllocator.hxx>
25 #include <NCollection_DefineAlloc.hxx>
26
27 /**
28 * Purpose:     NCollection_BaseCollection  is the base  abstract  class for 
29 *              all collection templates of this package.
30 *              The set  of collections is similar  to that  of TCollection.
31 *              Also  the methods of classes  have mostly the same names for
32 *              easy switch from TCollection <-> NCollection containers.
33 *              
34 *              NCollection is a nocdlpack, thus it is compiled without WOK.
35 *              BaseCollection allows assigning the collections of different
36 *              kinds  (the  items  type  being the same) with a few obvious
37 *              exclusions - one can not  assign any  collection to  the map 
38 *              having double data (two keys or a key  plus value). Only the 
39 *              maps of the very same type may be assigned through operator=
40 *              Said maps are: DoubleMap,
41 *                             DataMap,
42 *                             IndexedDataMap
43 *              
44 *              For the  users  needing  control  over the memory  usage the
45 *              allocators were added (see NCollection_BaseAllocator header)
46 *              Others may forget it -  BaseAllocator is used by default and
47 *              then memory is managed through Standard::Allocate/::Free.
48 */              
49 template<class TheItemType> class NCollection_BaseCollection
50 {
51  public:
52   // **************** The interface for iterating over collections
53   class Iterator 
54   {
55   public:
56     //! Query if the end of collection is reached by iterator
57     virtual Standard_Boolean More(void) const=0;
58     //! Make a step along the collection
59     virtual void Next(void)=0;
60     //! Value inquiry
61     virtual const TheItemType& Value(void) const=0;
62     //! Value change access
63     virtual TheItemType& ChangeValue(void) const=0;
64   public:
65     DEFINE_STANDARD_ALLOC
66     DEFINE_NCOLLECTION_ALLOC
67   protected:
68     //! Empty constructor
69     Iterator (void) {}
70     //! Virtual destructor is necessary for classes with virtual methods
71     virtual ~Iterator (void) {}
72   protected:
73     //! operator= is prohibited
74     const Iterator& operator= (const Iterator&);
75     //! Copy constructor **
76     Iterator (const Iterator&) {}
77   }; // End of nested class Iterator
78   
79  public:
80   // ---------- PUBLIC METHODS ------------
81
82   //! Common for all collections constructor takes care of theAllocator
83   NCollection_BaseCollection
84     (const Handle(NCollection_BaseAllocator)& theAllocator=0L) 
85   {
86     if (theAllocator.IsNull())
87       myAllocator = NCollection_BaseAllocator::CommonBaseAllocator();
88     else
89       myAllocator = theAllocator;
90   }
91
92   //! Number of items
93   virtual Standard_Integer Size(void) const = 0;
94
95   //! Virtual assignment
96   virtual void Assign 
97     (const NCollection_BaseCollection& theOther)=0;
98
99   //! Method to create iterators for base collections
100   virtual Iterator& CreateIterator(void) const=0;
101
102   //! Destructor - must be implemented to release the memory
103   virtual ~NCollection_BaseCollection (void) {}
104
105   //! Returns attached allocator
106   const Handle(NCollection_BaseAllocator)& Allocator() const { return myAllocator; }
107
108  protected:
109   // --------- PROTECTED METHOD -----------
110   const Handle(NCollection_BaseAllocator)& IterAllocator(void) const
111   { 
112     if (myIterAllocator.IsNull())
113       (Handle_NCollection_BaseAllocator&) myIterAllocator =
114         new NCollection_IncAllocator(64);
115     return myIterAllocator;
116   }
117
118  protected:
119   // --------- PROTECTED FIELDS -----------
120   Handle(NCollection_BaseAllocator)      myAllocator;
121  private:
122   // ---------- PRIVATE FIELDS ------------
123   Handle(NCollection_BaseAllocator)      myIterAllocator;
124
125 };
126
127 #endif