1 /*
2  * Copyright (C) 2015 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 #ifndef AAPT_CONFIG_DESCRIPTION_H
18 #define AAPT_CONFIG_DESCRIPTION_H
19 
20 #include "StringPiece.h"
21 
22 #include <androidfw/ResourceTypes.h>
23 #include <ostream>
24 
25 namespace aapt {
26 
27 /*
28  * Subclass of ResTable_config that adds convenient
29  * initialization and comparison methods.
30  */
31 struct ConfigDescription : public android::ResTable_config {
32     /*
33      * Parse a string of the form 'fr-sw600dp-land' and fill in the
34      * given ResTable_config with resulting configuration parameters.
35      *
36      * The resulting configuration has the appropriate sdkVersion defined
37      * for backwards compatibility.
38      */
39     static bool parse(const StringPiece& str, ConfigDescription* out = nullptr);
40 
41     /**
42      * If the configuration uses an axis that was added after
43      * the original Android release, make sure the SDK version
44      * is set accordingly.
45      */
46     static void applyVersionForCompatibility(ConfigDescription* config);
47 
48     ConfigDescription();
49     ConfigDescription(const android::ResTable_config& o);
50     ConfigDescription(const ConfigDescription& o);
51     ConfigDescription(ConfigDescription&& o);
52 
53     ConfigDescription& operator=(const android::ResTable_config& o);
54     ConfigDescription& operator=(const ConfigDescription& o);
55     ConfigDescription& operator=(ConfigDescription&& o);
56 
57     bool operator<(const ConfigDescription& o) const;
58     bool operator<=(const ConfigDescription& o) const;
59     bool operator==(const ConfigDescription& o) const;
60     bool operator!=(const ConfigDescription& o) const;
61     bool operator>=(const ConfigDescription& o) const;
62     bool operator>(const ConfigDescription& o) const;
63 };
64 
ConfigDescription()65 inline ConfigDescription::ConfigDescription() {
66     memset(this, 0, sizeof(*this));
67     size = sizeof(android::ResTable_config);
68 }
69 
ConfigDescription(const android::ResTable_config & o)70 inline ConfigDescription::ConfigDescription(const android::ResTable_config& o) {
71     *static_cast<android::ResTable_config*>(this) = o;
72     size = sizeof(android::ResTable_config);
73 }
74 
ConfigDescription(const ConfigDescription & o)75 inline ConfigDescription::ConfigDescription(const ConfigDescription& o) {
76     *static_cast<android::ResTable_config*>(this) = o;
77 }
78 
ConfigDescription(ConfigDescription && o)79 inline ConfigDescription::ConfigDescription(ConfigDescription&& o) {
80     *this = o;
81 }
82 
83 inline ConfigDescription& ConfigDescription::operator=(const android::ResTable_config& o) {
84     *static_cast<android::ResTable_config*>(this) = o;
85     size = sizeof(android::ResTable_config);
86     return *this;
87 }
88 
89 inline ConfigDescription& ConfigDescription::operator=(const ConfigDescription& o) {
90     *static_cast<android::ResTable_config*>(this) = o;
91     return *this;
92 }
93 
94 inline ConfigDescription& ConfigDescription::operator=(ConfigDescription&& o) {
95     *this = o;
96     return *this;
97 }
98 
99 inline bool ConfigDescription::operator<(const ConfigDescription& o) const {
100     return compare(o) < 0;
101 }
102 
103 inline bool ConfigDescription::operator<=(const ConfigDescription& o) const {
104     return compare(o) <= 0;
105 }
106 
107 inline bool ConfigDescription::operator==(const ConfigDescription& o) const {
108     return compare(o) == 0;
109 }
110 
111 inline bool ConfigDescription::operator!=(const ConfigDescription& o) const {
112     return compare(o) != 0;
113 }
114 
115 inline bool ConfigDescription::operator>=(const ConfigDescription& o) const {
116     return compare(o) >= 0;
117 }
118 
119 inline bool ConfigDescription::operator>(const ConfigDescription& o) const {
120     return compare(o) > 0;
121 }
122 
123 inline ::std::ostream& operator<<(::std::ostream& out, const ConfigDescription& o) {
124     return out << o.toString().string();
125 }
126 
127 } // namespace aapt
128 
129 #endif // AAPT_CONFIG_DESCRIPTION_H
130