1 /*
2  * Copyright 2023 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 
17 #pragma once
18 
19 #include <ftl/mixins.h>
20 #include <sys/types.h>
21 #include <string>
22 
23 namespace android::gui {
24 
25 // Type-safe wrapper for a PID.
26 struct Pid : ftl::Constructible<Pid, pid_t>, ftl::Equatable<Pid>, ftl::Orderable<Pid> {
27     using Constructible::Constructible;
28 
29     const static Pid INVALID;
30 
valPid31     constexpr auto val() const { return ftl::to_underlying(*this); }
32 
isValidPid33     constexpr bool isValid() const { return val() >= 0; }
34 
toStringPid35     std::string toString() const { return std::to_string(val()); }
36 };
37 
38 const inline Pid Pid::INVALID{-1};
39 
40 // Type-safe wrapper for a UID.
41 // We treat the unsigned equivalent of -1 as a singular invalid value.
42 struct Uid : ftl::Constructible<Uid, uid_t>, ftl::Equatable<Uid>, ftl::Orderable<Uid> {
43     using Constructible::Constructible;
44 
45     const static Uid INVALID;
46 
valUid47     constexpr auto val() const { return ftl::to_underlying(*this); }
48 
isValidUid49     constexpr bool isValid() const { return val() != static_cast<uid_t>(-1); }
50 
toStringUid51     std::string toString() const { return std::to_string(val()); }
52 };
53 
54 const inline Uid Uid::INVALID{static_cast<uid_t>(-1)};
55 
56 } // namespace android::gui
57