OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef SERVICES_FILES_C_LIB_ERRNO_IMPL_H_ | |
6 #define SERVICES_FILES_C_LIB_ERRNO_IMPL_H_ | |
7 | |
8 #include "mojo/public/cpp/system/macros.h" | |
9 | |
10 namespace mojio { | |
11 | |
12 // |ErrnoImpl| is an interface for getting/setting errno values. | |
13 class ErrnoImpl { | |
14 public: | |
15 // When destroyed, this class either preserves the errno value at creation or | |
16 // sets it to an explicit value. (Without this, internal calls may set errno | |
17 // to a value that shouldn't be visible to the caller.) | |
18 class Setter { | |
19 public: | |
20 explicit Setter(ErrnoImpl* errno_impl) | |
21 : errno_impl_(errno_impl), error_(errno_impl_->Get()) {} | |
22 ~Setter() { errno_impl_->Set(error_); } | |
23 | |
24 // If |error| is 0, this does nothing and returns true. Otherwise, this will | |
25 // arrange for the |ErrnoImpl|'s |Set()| to be called with the value of | |
26 // |error| (which should be a valid errno code) on destruction, and returns | |
27 // false. | |
28 bool Set(int error) { | |
29 if (error == 0) | |
30 return true; | |
31 error_ = error; | |
32 return false; | |
33 } | |
34 | |
35 private: | |
36 ErrnoImpl* const errno_impl_; | |
37 int error_; | |
38 | |
39 MOJO_DISALLOW_COPY_AND_ASSIGN(Setter); | |
40 }; | |
41 | |
42 virtual int Get() const = 0; | |
43 virtual void Set(int error) = 0; | |
44 | |
45 protected: | |
46 ErrnoImpl() {} | |
47 // Important: Destructors should not modify (the "real") errno (or any global | |
48 // state that may affect implementations of |ErrnoImpl|). | |
49 virtual ~ErrnoImpl() {} | |
50 | |
51 private: | |
52 MOJO_DISALLOW_COPY_AND_ASSIGN(ErrnoImpl); | |
53 }; | |
54 | |
55 } // namespace mojio | |
56 | |
57 #endif // SERVICES_FILES_C_LIB_ERRNO_IMPL_H_ | |
OLD | NEW |