1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "net_caps.h"
17 
18 namespace OHOS {
19 namespace NetManagerStandard {
NetCaps(const std::set<NetCap> & caps)20 NetCaps::NetCaps(const std::set<NetCap> &caps)
21 {
22     for (auto cap : caps) {
23         InsertNetCap(cap);
24     }
25 }
26 
operator ==(const NetCaps & netCaps) const27 bool NetCaps::operator==(const NetCaps &netCaps) const
28 {
29     return (caps_ == netCaps.caps_);
30 }
31 
IsValidNetCap(NetCap cap)32 bool NetCaps::IsValidNetCap(NetCap cap)
33 {
34     return (cap >= NET_CAPABILITY_MMS) && (cap < NET_CAPABILITY_END);
35 }
36 
InsertNetCap(NetCap cap)37 void NetCaps::InsertNetCap(NetCap cap)
38 {
39     if (IsValidNetCap(cap)) {
40         caps_ |= (1 << cap);
41     }
42 }
43 
RemoveNetCap(NetCap cap)44 void NetCaps::RemoveNetCap(NetCap cap)
45 {
46     if (IsValidNetCap(cap)) {
47         caps_ &= ~(1 << cap);
48     }
49 }
50 
HasNetCap(NetCap cap) const51 bool NetCaps::HasNetCap(NetCap cap) const
52 {
53     return (caps_ >> cap) & 1;
54 }
55 
HasNetCaps(const std::set<NetCap> & caps) const56 bool NetCaps::HasNetCaps(const std::set<NetCap> &caps) const
57 {
58     return std::all_of(caps.cbegin(), caps.cend(), [this] (const NetCap &cap) { return HasNetCap(cap); });
59 }
60 
ToSet() const61 std::set<NetCap> NetCaps::ToSet() const
62 {
63     std::set<NetCap> ret;
64     for (auto cap = static_cast<NetCap>(0); cap < NET_CAPABILITY_END; cap = static_cast<NetCap>(cap + 1)) {
65         if (HasNetCap(cap)) {
66             ret.insert(cap);
67         }
68     }
69     return ret;
70 }
71 } // namespace NetManagerStandard
72 } // namespace OHOS