1
2
3
4
5
6 #ifndef SOULNG_UTIL_HANDLE_INCLUDED
7 #define SOULNG_UTIL_HANDLE_INCLUDED
8 #include <soulng/util/UtilApi.hpp>
9 #if defined(_WIN32)
10 #include <io.h>
11 #elif defined(__linux) || defined(__posix) || defined(__unix)
12 #include <unistd.h>
13 #else
14 #error unknown platform
15 #endif
16
17 namespace soulng { namespace util {
18
19 class Handle
20 {
21 public:
22 Handle(int handle_) : handle(handle_)
23 {
24 }
25 ~Handle()
26 {
27 if (handle != -1)
28 {
29 close(handle);
30 }
31 }
32 Handle(Handle&& that)
33 {
34 handle = that.handle;
35 that.handle = -1;
36 }
37 void operator=(Handle&& that)
38 {
39 if (handle != -1)
40 {
41 close(handle);
42 }
43 handle = that.handle;
44 that.handle = -1;
45 }
46 operator int() const { return handle; }
47 private:
48 int handle;
49 };
50
51 } }
52
53 #endif // SOULNG_UTIL_HANDLE_INCLUDED