1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef CUTTLEFISH_COMMON_COMMON_LIBS_FS_SHARED_SELECT_H_ 17 #define CUTTLEFISH_COMMON_COMMON_LIBS_FS_SHARED_SELECT_H_ 18 19 #include <set> 20 21 #include "common/libs/fs/shared_fd.h" 22 23 namespace cvd { 24 /** 25 * The SharedFD version of fdset for the Select call. 26 * 27 * There are two types of methods. STL inspired methods and types use 28 * all_lowercase_underscore notation. 29 * 30 * Methods that are inspired by POSIX Use UpperCamelCase. 31 * 32 * Assume that any mutation invalidates all iterators. 33 */ 34 class SharedFDSet { 35 public: 36 // These methods and types have more to do with the STL than POSIX, 37 // so I'm using STL-compatible notation. 38 typedef std::set<SharedFD>::iterator iterator; 39 typedef std::set<SharedFD>::const_iterator const_iterator; 40 begin()41 iterator begin() { return value_.begin(); } end()42 iterator end() { return value_.end(); } begin()43 const_iterator begin() const { return value_.begin(); } end()44 const_iterator end() const { return value_.end(); } 45 swap(SharedFDSet * rhs)46 void swap(SharedFDSet* rhs) { 47 value_.swap(rhs->value_); 48 } 49 Clr(const SharedFD & in)50 void Clr(const SharedFD& in) { 51 value_.erase(in); 52 } 53 IsSet(const SharedFD & in)54 bool IsSet(const SharedFD& in) const { 55 return value_.count(in) != 0; 56 } 57 Set(const SharedFD & in)58 void Set(const SharedFD& in) { 59 value_.insert(in); 60 } 61 Zero()62 void Zero() { 63 value_.clear(); 64 } 65 66 private: 67 std::set<SharedFD> value_; 68 }; 69 70 /** 71 * SharedFD version of select. 72 * 73 * read_set, write_set, and timeout are in/out parameters. This caller should keep 74 * a copy of the original values if it wants to preserve them. 75 */ 76 int Select(SharedFDSet* read_set, SharedFDSet* write_set, 77 SharedFDSet* error_set, struct timeval* timeout); 78 79 } // namespace cvd 80 81 #endif // CUTTLEFISH_COMMON_COMMON_LIBS_FS_SHARED_SELECT_H_ 82