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