1 | /* -*- c-basic-offset: 2 -*- */ |
2 | /* Copyright(C) 2011-2015 Brazil |
3 | |
4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License version 2.1 as published by the Free Software Foundation. |
7 | |
8 | This library is distributed in the hope that it will be useful, |
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | Lesser General Public License for more details. |
12 | |
13 | You should have received a copy of the GNU Lesser General Public |
14 | License along with this library; if not, write to the Free Software |
15 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
16 | */ |
17 | |
18 | #include "file.hpp" |
19 | #include "file-impl.hpp" |
20 | |
21 | #include <new> |
22 | |
23 | namespace grn { |
24 | namespace dat { |
25 | |
26 | File::File() : impl_(NULL) {} |
27 | |
28 | File::~File() { |
29 | delete impl_; |
30 | } |
31 | |
32 | void File::create(const char *path, UInt64 size) { |
33 | File new_file; |
34 | new_file.impl_ = new (std::nothrow) FileImpl; |
35 | GRN_DAT_THROW_IF(MEMORY_ERROR, new_file.impl_ == NULL); |
36 | new_file.impl_->create(path, size); |
37 | new_file.swap(this); |
38 | } |
39 | |
40 | void File::open(const char *path) { |
41 | File new_file; |
42 | new_file.impl_ = new (std::nothrow) FileImpl; |
43 | GRN_DAT_THROW_IF(MEMORY_ERROR, new_file.impl_ == NULL); |
44 | new_file.impl_->open(path); |
45 | new_file.swap(this); |
46 | } |
47 | |
48 | void File::close() { |
49 | File().swap(this); |
50 | } |
51 | |
52 | void *File::ptr() const { |
53 | return (impl_ != NULL) ? impl_->ptr() : NULL; |
54 | } |
55 | |
56 | UInt64 File::size() const { |
57 | return (impl_ != NULL) ? impl_->size() : 0; |
58 | } |
59 | |
60 | void File::swap(File *rhs) { |
61 | FileImpl * const temp = impl_; |
62 | impl_ = rhs->impl_; |
63 | rhs->impl_ = temp; |
64 | } |
65 | |
66 | void File::flush() { |
67 | if (impl_) { |
68 | impl_->flush(); |
69 | } |
70 | } |
71 | |
72 | } // namespace dat |
73 | } // namespace grn |
74 | |