Download code

Jump to: navigation, search

Back to Scoped_handle_(C_Plus_Plus)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

scoped_handle.hpp

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Scoped_handle_(C_Plus_Plus)?oldid=8641
14 */
15 
16 
17 #ifndef SCOPED_HANDLE_HPP_INCLUDE_GUARD
18 #define SCOPED_HANDLE_HPP_INCLUDE_GUARD
19 
20 namespace lp {
21 
22 
23 template<typename T> inline bool handle_valid(T handle, bool invalidate=false) 
24 {
25 	if(invalidate) handle=0;
26 	return (bool)handle;
27 }
28 
29 template<typename T, int (CF)(T), bool (VF)(T, bool)=handle_valid<T> > class scoped_handle
30 {
31 	T handle;
32 public:
33 	scoped_handle(T handle_): handle(handle_) {}
34 	~scoped_handle() {
35 		if(VF(handle)) CF(handle);
36 	}
37 
38 	void close() {
39 		if(VF(handle)) CF(handle);
40 		VF(handle, true);
41 	}
42 	void release() {
43 		VF(handle, true);
44 	}
45 	operator T() {
46 		return handle;
47 	}
48 };
49 
50 }
51 
52 #endif
53 


hijacker
hijacker
hijacker
hijacker

build.log

1 In file included from /tmp/litprog7822523/test.cpp:17:0:
2 /tmp/litprog7822523/scoped_handle.hpp: In destructor 'lp::scoped_handle<T, CF, VF>::~scoped_handle() [with T = _IO_FILE*, int (* CF)(T) = fclose, bool (* VF)(T, bool) = lp::handle_valid [with T = _IO_FILE*]]':
3 /tmp/litprog7822523/test.cpp:24:62:   instantiated from here
4 /tmp/litprog7822523/scoped_handle.hpp:35:3: error: too few arguments to function


hijacker
hijacker
hijacker
hijacker

test.cpp

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Scoped_handle_(C_Plus_Plus)?oldid=8641
14 */
15 
16 
17 #include"scoped_handle.hpp"
18 
19 #include<stdio.h>
20 #include<iostream>
21 
22 int main()
23 {
24 	lp::scoped_handle<FILE*, fclose> file(fopen("test.txt", "w"));
25 	if(!(FILE*)file) {
26 		std::cerr<<"Could not open file \"test.txt\" for writing\n";
27 		return 1;
28 	}
29 
30 	fprintf(file, "Hello\n");
31 	
32 	return 0;
33 }
34 
35 


hijacker
hijacker
hijacker
hijacker