xref: /aoo41x/main/cosv/inc/cosv/openclose.hxx (revision ac3d0c65)
1*ac3d0c65SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ac3d0c65SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ac3d0c65SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ac3d0c65SAndrew Rist  * distributed with this work for additional information
6*ac3d0c65SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ac3d0c65SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ac3d0c65SAndrew Rist  * "License"); you may not use this file except in compliance
9*ac3d0c65SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ac3d0c65SAndrew Rist  *
11*ac3d0c65SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ac3d0c65SAndrew Rist  *
13*ac3d0c65SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ac3d0c65SAndrew Rist  * software distributed under the License is distributed on an
15*ac3d0c65SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ac3d0c65SAndrew Rist  * KIND, either express or implied.  See the License for the
17*ac3d0c65SAndrew Rist  * specific language governing permissions and limitations
18*ac3d0c65SAndrew Rist  * under the License.
19*ac3d0c65SAndrew Rist  *
20*ac3d0c65SAndrew Rist  *************************************************************/
21*ac3d0c65SAndrew Rist 
22*ac3d0c65SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef CSV_OPENCLOSE_HXX
25cdf0e10cSrcweir #define CSV_OPENCLOSE_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir namespace csv
29cdf0e10cSrcweir {
30cdf0e10cSrcweir 
31cdf0e10cSrcweir // Open modes for storages:
32cdf0e10cSrcweir enum E_RWMode
33cdf0e10cSrcweir {
34cdf0e10cSrcweir 	rwDefault	= 0x0000,		//	Keep old settings. If there are none, set default.
35cdf0e10cSrcweir 	rwRead		= 0x0001,		//  Reads only
36cdf0e10cSrcweir 	rwWrite		= 0x0002,       //  Writes only
37cdf0e10cSrcweir 	rwReadWrite	= 0x0003        //  Reads and writes.
38cdf0e10cSrcweir };
39cdf0e10cSrcweir 
40cdf0e10cSrcweir enum E_OpenMode
41cdf0e10cSrcweir {
42cdf0e10cSrcweir 	omCreateIfNecessary 	= 0x0000,   // Creates a new file only, if file does not exist.
43cdf0e10cSrcweir 	omCreateNot 			= 0x0010,   // Open fails, if file does not exist.
44cdf0e10cSrcweir 	omCreate	 			= 0x0020 	// Existing file will be deleted.
45cdf0e10cSrcweir };
46cdf0e10cSrcweir enum E_ShareMode
47cdf0e10cSrcweir {
48cdf0e10cSrcweir 	shmShareNot		= 0x0000,		// Allow others nothing
49cdf0e10cSrcweir 	shmShareRead	= 0x0004,		// Allow others to read
50cdf0e10cSrcweir 	shmShareAll		= 0x000C      	// Allow others to read and write
51cdf0e10cSrcweir };
52cdf0e10cSrcweir 
53cdf0e10cSrcweir /** Constants for filemode combinations
54cdf0e10cSrcweir     These combinations are the only ones, guaranteed to be supported.
55cdf0e10cSrcweir */
56cdf0e10cSrcweir const UINT32	CFM_RW 		= rwReadWrite;
57cdf0e10cSrcweir const UINT32	CFM_CREATE 	=
58cdf0e10cSrcweir     static_cast< UINT32 >(rwReadWrite) | static_cast< UINT32 >(omCreate);
59cdf0e10cSrcweir const UINT32	CFM_READ 	=
60cdf0e10cSrcweir     static_cast< UINT32 >(rwRead) | static_cast< UINT32 >(omCreateNot) |
61cdf0e10cSrcweir     static_cast< UINT32 >(shmShareRead);
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 
65cdf0e10cSrcweir class OpenClose
66cdf0e10cSrcweir {
67cdf0e10cSrcweir   public:
~OpenClose()68cdf0e10cSrcweir     virtual ~OpenClose() {}
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     bool   	            open(
71cdf0e10cSrcweir 							UINT32			in_nOpenModeInfo = 0 ); /// Combination of values of E_RWMode and E_ShareMode und E_OpenMode. 0 := Keep existing mode.
72cdf0e10cSrcweir     void   	            close();
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     bool                is_open() const;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir   private:
77cdf0e10cSrcweir     virtual bool   	    do_open(
78cdf0e10cSrcweir 							UINT32			in_nOpenModeInfo ) = 0;
79cdf0e10cSrcweir     virtual void   	    do_close() = 0;
80cdf0e10cSrcweir     virtual bool        inq_is_open() const = 0;
81cdf0e10cSrcweir };
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 
85cdf0e10cSrcweir class OpenCloseGuard
86cdf0e10cSrcweir {
87cdf0e10cSrcweir   public:
88cdf0e10cSrcweir 						OpenCloseGuard(
89cdf0e10cSrcweir 							OpenClose &			i_rOpenClose,
90cdf0e10cSrcweir 							UINT32				i_nOpenModeInfo = 0 );
91cdf0e10cSrcweir 						~OpenCloseGuard();
92cdf0e10cSrcweir 						operator bool() const;
93cdf0e10cSrcweir 
94cdf0e10cSrcweir   private:
95cdf0e10cSrcweir     // Forbidden:
96cdf0e10cSrcweir 						OpenCloseGuard(OpenCloseGuard&);
97cdf0e10cSrcweir     OpenCloseGuard &    operator=(OpenCloseGuard&);
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     // DATA
100cdf0e10cSrcweir 	OpenClose &			rOpenClose;
101cdf0e10cSrcweir };
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 
104cdf0e10cSrcweir // IMPLEMENTATION
105cdf0e10cSrcweir 
106cdf0e10cSrcweir inline bool
open(UINT32 i_nOpenModeInfo)107cdf0e10cSrcweir OpenClose::open( UINT32 i_nOpenModeInfo )
108cdf0e10cSrcweir     { return do_open(i_nOpenModeInfo); }
109cdf0e10cSrcweir inline void
close()110cdf0e10cSrcweir OpenClose::close()
111cdf0e10cSrcweir     { do_close(); }
112cdf0e10cSrcweir inline bool
is_open() const113cdf0e10cSrcweir OpenClose::is_open() const
114cdf0e10cSrcweir     { return inq_is_open(); }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir inline
OpenCloseGuard(OpenClose & i_rOpenClose,UINT32 i_nOpenModeInfo)117cdf0e10cSrcweir OpenCloseGuard::OpenCloseGuard( OpenClose &	i_rOpenClose,
118cdf0e10cSrcweir 							    UINT32		i_nOpenModeInfo )
119cdf0e10cSrcweir     :	rOpenClose(i_rOpenClose)
120cdf0e10cSrcweir     { rOpenClose.open(i_nOpenModeInfo); }
121cdf0e10cSrcweir inline
~OpenCloseGuard()122cdf0e10cSrcweir OpenCloseGuard::~OpenCloseGuard()
123cdf0e10cSrcweir     { if (rOpenClose.is_open()) rOpenClose.close(); }
124cdf0e10cSrcweir inline
125cdf0e10cSrcweir OpenCloseGuard::operator bool() const
126cdf0e10cSrcweir     { return rOpenClose.is_open(); }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 
131cdf0e10cSrcweir }   // namespace csv
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 
138cdf0e10cSrcweir #endif
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 
141