| 1 | |
| 2 | // vim:sw=2:ai |
| 3 | |
| 4 | /* |
| 5 | * Copyright (C) 2010-2011 DeNA Co.,Ltd.. All rights reserved. |
| 6 | * See COPYRIGHT.txt for details. |
| 7 | */ |
| 8 | |
| 9 | #ifndef DENA_AUTO_FILE_HPP |
| 10 | #define DENA_AUTO_FILE_HPP |
| 11 | |
| 12 | /* |
| 13 | #ifndef __WIN__ |
| 14 | #include <dirent.h> |
| 15 | #endif |
| 16 | */ |
| 17 | |
| 18 | #include "util.hpp" |
| 19 | |
| 20 | namespace dena { |
| 21 | |
| 22 | struct auto_file : private noncopyable { |
| 23 | auto_file() : fd(-1) { } |
| 24 | ~auto_file() { |
| 25 | reset(); |
| 26 | } |
| 27 | int get() const { return fd; } |
| 28 | int close() { |
| 29 | if (fd < 0) { |
| 30 | return 0; |
| 31 | } |
| 32 | const int r = ::close(fd); |
| 33 | fd = -1; |
| 34 | return r; |
| 35 | } |
| 36 | void reset(int x = -1) { |
| 37 | if (fd >= 0) { |
| 38 | this->close(); |
| 39 | } |
| 40 | fd = x; |
| 41 | } |
| 42 | private: |
| 43 | int fd; |
| 44 | }; |
| 45 | |
| 46 | /* |
| 47 | struct auto_dir : private noncopyable { |
| 48 | auto_dir() : dp(0) { } |
| 49 | ~auto_dir() { |
| 50 | reset(); |
| 51 | } |
| 52 | DIR *get() const { return dp; } |
| 53 | void reset(DIR *d = 0) { |
| 54 | if (dp != 0) { |
| 55 | closedir(dp); |
| 56 | } |
| 57 | dp = d; |
| 58 | } |
| 59 | private: |
| 60 | DIR *dp; |
| 61 | }; |
| 62 | */ |
| 63 | |
| 64 | }; |
| 65 | |
| 66 | #endif |
| 67 | |
| 68 | |