michael@0: /* michael@0: * Copyright (C) 2009 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #ifndef SCOPED_FD_H_included michael@0: #define SCOPED_FD_H_included michael@0: michael@0: #include michael@0: michael@0: // A smart pointer that closes the given fd on going out of scope. michael@0: // Use this when the fd is incidental to the purpose of your function, michael@0: // but needs to be cleaned up on exit. michael@0: class ScopedFd { michael@0: public: michael@0: explicit ScopedFd(int fd) : fd(fd) { michael@0: } michael@0: michael@0: ~ScopedFd() { michael@0: close(fd); michael@0: } michael@0: michael@0: int get() const { michael@0: return fd; michael@0: } michael@0: michael@0: private: michael@0: int fd; michael@0: michael@0: // Disallow copy and assignment. michael@0: ScopedFd(const ScopedFd&); michael@0: void operator=(const ScopedFd&); michael@0: }; michael@0: michael@0: #endif // SCOPED_FD_H_included