KernelConfigTypedValue.cpp 2.7 KB

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