]> git.donarmstrong.com Git - flightcrew.git/blob - src/zipios/zipios++/collcoll.h
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / zipios / zipios++ / collcoll.h
1 #ifndef COLLCOLL_H
2 #define COLLCOLL_H
3
4 #include "zipios++/zipios-config.h"
5
6 #include <string>
7 #include <vector>
8
9 #include "zipios++/fcoll.h"
10
11 namespace zipios {
12
13 using std::string ;
14
15 /** \anchor collcoll_anchor
16     CollectionCollection is a FileCollection that consists of an
17     arbitrary number of FileCollections. With a CollectionCollection
18     the user can use multiple FileCollections transparently, making it
19     easy for a program to keep some of its files in a zip archive and
20     others stored in ordinary files. CollectionCollection can be used
21     to create a simple virtual filesystem, where all collections are
22     mounted in /. If more than one collection contain a file with
23     the same path only the one in the first added collection will
24     be accessible.
25 */
26 class CollectionCollection : public FileCollection {
27 public:
28   /** \anchor collcoll_inst_anchor
29       This static method provides a singleton instance of a CollectionCollection.
30       The instance is instantiated the first time the method is called.
31       @return A pointer to a singleton CollectionCollection instance.
32    */
33   static inline CollectionCollection &inst() ;
34
35   /** Constructor.
36    */
37   explicit CollectionCollection() ;
38
39   /** Copy constructor. */
40   inline CollectionCollection( const CollectionCollection &src ) ;
41
42   /** Copy assignment operator. */
43   inline const CollectionCollection &operator= ( const CollectionCollection &src ) ;
44
45   /** \anchor collcoll_addcoll_anchor
46       Adds a collection.
47       @param collection The collection to add.
48       @return true if the collection was added succesfully and
49       the added collection is valid.
50    */
51   bool addCollection( const FileCollection &collection ) ;
52
53   /** Adds the collection pointed to by collection. The CollectionCollection
54       will call delete on the pointer when it is destructed, so the caller
55       should make absolutely sure to only pass in a collection created with new
56       and be sure to leave it alone after adding it. If the collection is not
57       added false is returned and the caller remains responsible for the
58       collection pointed to by collection.
59       @param collection A pointer to the collection to add.
60       @return true if the collection was added succesfully and
61       the added collection is valid. */
62   bool addCollection( FileCollection *collection ) ;
63
64   virtual void close() ;
65
66   virtual ConstEntries entries() const ;
67
68   virtual ConstEntryPointer getEntry( const string &name, 
69                                       MatchPath matchpath = MATCH ) const ;
70
71   virtual istream *getInputStream( const ConstEntryPointer &entry ) ;
72
73   virtual istream *getInputStream( const string &entry_name, 
74                                    MatchPath matchpath = MATCH ) ;
75
76   /** Returns the number in entries in all collections kept by
77       the CollectionCollection object */
78   virtual int size() const ;
79   
80   virtual FileCollection *clone() const ;
81
82   virtual ~CollectionCollection() ;
83
84 protected:
85   /** A protected getEntry member function, that not only
86       finds an entry that match the name, if such an entry exists
87       in the collection, it also returns, which collection it was found
88       in.
89    */
90   void getEntry( const string &name,
91                  ConstEntryPointer &cep, 
92                  std::vector< FileCollection * >::const_iterator &it, 
93                  MatchPath matchpath = MATCH ) const ;
94   
95   vector< FileCollection * > _collections ;
96 private:
97   static CollectionCollection *_inst ;
98 };
99
100
101 /** Shortcut name for a CollectionCollection. If the static method
102 inst is used, it is often used a lot, so it's handy with a short name for
103 CollectionCollection */
104 typedef CollectionCollection CColl ;
105
106
107 //
108 // Inline (member) functions
109 //
110
111 CollectionCollection &CollectionCollection::inst() {
112   if( _inst != 0 )
113     return *_inst ;
114   else
115     return *( _inst = new CollectionCollection ) ;
116 }
117
118 CollectionCollection::CollectionCollection( const CollectionCollection &src ) 
119   : FileCollection( src )
120 {
121   _collections.reserve( src._collections.size() ) ;
122   std::vector< FileCollection * >::const_iterator it ;
123   for ( it = src._collections.begin() ; it != src._collections.end() ; ++it )
124     _collections.push_back( (*it)->clone() ) ;
125 }
126
127
128 const CollectionCollection &
129 CollectionCollection::operator= ( const CollectionCollection &src ) {
130   this->FileCollection::operator=( src ) ;
131 //    FileCollection::=( static_cast< FileCollection >( src ) ) ; 
132
133   if ( this != &src ) {
134     // Destroy current contents.
135     std::vector< FileCollection * >::const_iterator it ;
136     for ( it = _collections.begin() ; it != _collections.end() ; ++it )
137       delete *it ;
138     //  Then copy src's content.
139     _collections.clear() ;
140     _collections.reserve( src._collections.size() ) ;
141     for ( it = src._collections.begin() ; it != src._collections.end() ; ++it )
142       _collections.push_back( (*it)->clone() ) ;
143   }
144   return *this ;
145 }
146
147 } // namespace
148
149
150
151 #endif
152
153 /** \file
154     Header file that defines CollectionCollection.
155 */
156
157 /*
158   Zipios++ - a small C++ library that provides easy access to .zip files.
159   Copyright (C) 2000  Thomas Søndergaard
160   
161   This library is free software; you can redistribute it and/or
162   modify it under the terms of the GNU Lesser General Public
163   License as published by the Free Software Foundation; either
164   version 2 of the License, or (at your option) any later version.
165   
166   This library is distributed in the hope that it will be useful,
167   but WITHOUT ANY WARRANTY; without even the implied warranty of
168   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
169   Lesser General Public License for more details.
170   
171   You should have received a copy of the GNU Lesser General Public
172   License along with this library; if not, write to the Free Software
173   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
174 */