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