1b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes/*
2b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes * Copyright (C) 2009 The Android Open Source Project
3b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes *
4b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes * you may not use this file except in compliance with the License.
6b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes * You may obtain a copy of the License at
7b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes *
8b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes *
10b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes * Unless required by applicable law or agreed to in writing, software
11b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes * See the License for the specific language governing permissions and
14b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes * limitations under the License.
15b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes */
16b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes
17b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes#ifndef SCOPED_FD_H_included
18b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes#define SCOPED_FD_H_included
19b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes
20b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes#include <unistd.h>
21b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes
22b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes// A smart pointer that closes the given fd on going out of scope.
23b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes// Use this when the fd is incidental to the purpose of your function,
24b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes// but needs to be cleaned up on exit.
25b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughesclass ScopedFd {
26b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughespublic:
27b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes    explicit ScopedFd(int fd) : fd(fd) {
28b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes    }
29b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes
30b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes    ~ScopedFd() {
31b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes        close(fd);
32b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes    }
33b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes
34b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes    int get() const {
35b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes        return fd;
36b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes    }
37b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes
38b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughesprivate:
39b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes    int fd;
407ca6fd0dca02f7abdd8808db78357743bbdd23a5Elliott Hughes
417ca6fd0dca02f7abdd8808db78357743bbdd23a5Elliott Hughes    // Disallow copy and assignment.
427ca6fd0dca02f7abdd8808db78357743bbdd23a5Elliott Hughes    ScopedFd(const ScopedFd&);
437ca6fd0dca02f7abdd8808db78357743bbdd23a5Elliott Hughes    void operator=(const ScopedFd&);
44b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes};
45b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes
46b5fc5ecd3fe5315fc2756c0c25adc458cc8c8d91Elliott Hughes#endif  // SCOPED_FD_H_included
47