xref: /aoo41x/main/cosv/inc/cosv/openclose.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef CSV_OPENCLOSE_HXX
29 #define CSV_OPENCLOSE_HXX
30 
31 
32 namespace csv
33 {
34 
35 // Open modes for storages:
36 enum E_RWMode
37 {
38 	rwDefault	= 0x0000,		//	Keep old settings. If there are none, set default.
39 	rwRead		= 0x0001,		//  Reads only
40 	rwWrite		= 0x0002,       //  Writes only
41 	rwReadWrite	= 0x0003        //  Reads and writes.
42 };
43 
44 enum E_OpenMode
45 {
46 	omCreateIfNecessary 	= 0x0000,   // Creates a new file only, if file does not exist.
47 	omCreateNot 			= 0x0010,   // Open fails, if file does not exist.
48 	omCreate	 			= 0x0020 	// Existing file will be deleted.
49 };
50 enum E_ShareMode
51 {
52 	shmShareNot		= 0x0000,		// Allow others nothing
53 	shmShareRead	= 0x0004,		// Allow others to read
54 	shmShareAll		= 0x000C      	// Allow others to read and write
55 };
56 
57 /** Constants for filemode combinations
58     These combinations are the only ones, guaranteed to be supported.
59 */
60 const UINT32	CFM_RW 		= rwReadWrite;
61 const UINT32	CFM_CREATE 	=
62     static_cast< UINT32 >(rwReadWrite) | static_cast< UINT32 >(omCreate);
63 const UINT32	CFM_READ 	=
64     static_cast< UINT32 >(rwRead) | static_cast< UINT32 >(omCreateNot) |
65     static_cast< UINT32 >(shmShareRead);
66 
67 
68 
69 class OpenClose
70 {
71   public:
72     virtual ~OpenClose() {}
73 
74     bool   	            open(
75 							UINT32			in_nOpenModeInfo = 0 ); /// Combination of values of E_RWMode and E_ShareMode und E_OpenMode. 0 := Keep existing mode.
76     void   	            close();
77 
78     bool                is_open() const;
79 
80   private:
81     virtual bool   	    do_open(
82 							UINT32			in_nOpenModeInfo ) = 0;
83     virtual void   	    do_close() = 0;
84     virtual bool        inq_is_open() const = 0;
85 };
86 
87 
88 
89 class OpenCloseGuard
90 {
91   public:
92 						OpenCloseGuard(
93 							OpenClose &			i_rOpenClose,
94 							UINT32				i_nOpenModeInfo = 0 );
95 						~OpenCloseGuard();
96 						operator bool() const;
97 
98   private:
99     // Forbidden:
100 						OpenCloseGuard(OpenCloseGuard&);
101     OpenCloseGuard &    operator=(OpenCloseGuard&);
102 
103     // DATA
104 	OpenClose &			rOpenClose;
105 };
106 
107 
108 // IMPLEMENTATION
109 
110 inline bool
111 OpenClose::open( UINT32 i_nOpenModeInfo )
112     { return do_open(i_nOpenModeInfo); }
113 inline void
114 OpenClose::close()
115     { do_close(); }
116 inline bool
117 OpenClose::is_open() const
118     { return inq_is_open(); }
119 
120 inline
121 OpenCloseGuard::OpenCloseGuard( OpenClose &	i_rOpenClose,
122 							    UINT32		i_nOpenModeInfo )
123     :	rOpenClose(i_rOpenClose)
124     { rOpenClose.open(i_nOpenModeInfo); }
125 inline
126 OpenCloseGuard::~OpenCloseGuard()
127     { if (rOpenClose.is_open()) rOpenClose.close(); }
128 inline
129 OpenCloseGuard::operator bool() const
130     { return rOpenClose.is_open(); }
131 
132 
133 
134 
135 }   // namespace csv
136 
137 
138 
139 
140 
141 
142 #endif
143 
144 
145