1 /*
2  * stringCopyright (C) 2017 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 #include "KernelConfigTypedValue.h"
18 
19 #include "parse_string.h"
20 
21 #include <android-base/logging.h>
22 
23 namespace android {
24 namespace vintf {
25 
26 // static
27 const KernelConfigTypedValue KernelConfigTypedValue::gMissingConfig{Tristate::NO};
28 
KernelConfigTypedValue()29 KernelConfigTypedValue::KernelConfigTypedValue()
30         : KernelConfigTypedValue("") {
31 }
32 
KernelConfigTypedValue(std::string && s)33 KernelConfigTypedValue::KernelConfigTypedValue(std::string &&s){
34     mType = KernelConfigType::STRING;
35     std::swap(mStringValue, s);
36 }
37 
KernelConfigTypedValue(KernelConfigIntValue v)38 KernelConfigTypedValue::KernelConfigTypedValue(KernelConfigIntValue v){
39     mType = KernelConfigType::INTEGER;
40     mIntegerValue = v;
41 }
42 
KernelConfigTypedValue(KernelConfigRangeValue && v)43 KernelConfigTypedValue::KernelConfigTypedValue(KernelConfigRangeValue &&v){
44     mType = KernelConfigType::RANGE;
45     std::swap(mRangeValue, v);
46 }
47 
KernelConfigTypedValue(Tristate t)48 KernelConfigTypedValue::KernelConfigTypedValue(Tristate t){
49     mType = KernelConfigType::TRISTATE;
50     mTristateValue = t;
51 }
52 
operator ==(const KernelConfigTypedValue & other) const53 bool KernelConfigTypedValue::operator==(const KernelConfigTypedValue &other) const {
54     if (mType != other.mType) {
55         return false;
56     }
57     switch(mType) {
58         case KernelConfigType::STRING:
59             return mStringValue == other.mStringValue;
60         case KernelConfigType::INTEGER:
61             return mIntegerValue == other.mIntegerValue;
62         case KernelConfigType::RANGE:
63             return mRangeValue == other.mRangeValue;
64         case KernelConfigType::TRISTATE:
65             return mTristateValue == other.mTristateValue;
66     }
67 }
68 
69 
matchValue(const std::string & s) const70 bool KernelConfigTypedValue::matchValue(const std::string &s) const {
71     switch(mType) {
72         case KernelConfigType::STRING:
73             return ("\"" + mStringValue + "\"") == s;
74         case KernelConfigType::INTEGER: {
75             KernelConfigIntValue iv;
76             return parseKernelConfigInt(s, &iv) && iv == mIntegerValue;
77         }
78         case KernelConfigType::RANGE: {
79             KernelConfigRangeValue range;
80             return parseRange(s, &range) && range == mRangeValue;
81         }
82         case KernelConfigType::TRISTATE: {
83             Tristate tristate;
84             return parse(s, &tristate) && tristate == mTristateValue;
85         }
86     }
87 }
88 
89 } // namespace vintf
90 } // namespace android
91