Android-cuttlefish cvd tool
flag_base.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025 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 <stddef.h>
20
21#include <string>
22#include <utility>
23#include <vector>
24
25namespace cuttlefish {
26
27template <typename T>
28class FlagBase {
29 public:
30 T ForIndex(const size_t index) const {
31 if (index < values_.size()) {
32 return values_[index];
33 } else {
34 return values_[0];
35 }
36 }
37
38 bool IsDefault() const { return is_default_; }
39
40 bool IsDefaultForIndex(const size_t index) const {
41 if (is_default_values_.empty()) {
42 return IsDefault();
43 } else if (index < is_default_values_.size()) {
44 return is_default_values_[index];
45 } else {
46 return is_default_values_[0];
47 }
48 }
49
50 size_t Size() const { return values_.size(); }
51 const std::vector<T>& AsVector() const { return values_; }
52
53 protected:
54 explicit FlagBase(std::vector<T> flag_values, bool is_default,
55 // TODO(chadreynolds): remove this default value if we
56 // update all implementations
57 std::vector<bool> is_default_values = {})
58 : values_(std::move(flag_values)),
59 is_default_(is_default),
60 is_default_values_(is_default_values) {}
61 virtual ~FlagBase() = 0;
62
63 private:
64 std::vector<T> values_;
66 std::vector<bool> is_default_values_;
67};
68
69template <typename T>
71
72extern template class FlagBase<bool>;
73extern template class FlagBase<int>;
74extern template class FlagBase<std::string>;
75
76} // namespace cuttlefish
Definition: flag_base.h:28
virtual ~FlagBase()=0
Definition: flag_base.h:70
bool IsDefault() const
Definition: flag_base.h:38
bool IsDefaultForIndex(const size_t index) const
Definition: flag_base.h:40
const std::vector< T > & AsVector() const
Definition: flag_base.h:51
bool is_default_
Definition: flag_base.h:65
size_t Size() const
Definition: flag_base.h:50
T ForIndex(const size_t index) const
Definition: flag_base.h:30
std::vector< bool > is_default_values_
Definition: flag_base.h:66
std::vector< T > values_
Definition: flag_base.h:64
FlagBase(std::vector< T > flag_values, bool is_default, std::vector< bool > is_default_values={})
Definition: flag_base.h:54
Definition: alloc_driver.h:21