d4d41fd78fedfc52c21a6e44f5a46c8fe76749d5
[libdcp.git] / src / language_tag.h
1 /*
2     Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 #ifndef LIBDCP_LANGUAGE_TAG_H
35 #define LIBDCP_LANGUAGE_TAG_H
36
37
38 #include <boost/optional.hpp>
39 #include <string>
40 #include <vector>
41
42
43 namespace dcp {
44
45
46 class LanguageTag
47 {
48 public:
49         std::string to_string () const;
50         std::string description () const;
51
52         struct SubtagData
53         {
54                 SubtagData () {}
55
56                 SubtagData (std::string subtag_, std::string description_)
57                         : subtag(subtag_)
58                         , description(description_)
59                 {}
60
61                 std::string subtag;
62                 std::string description;
63
64                 bool operator== (SubtagData const& other) {
65                         return subtag == other.subtag && description == other.description;
66                 }
67         };
68
69         enum SubtagType
70         {
71                 LANGUAGE,
72                 SCRIPT,
73                 REGION,
74                 VARIANT,
75                 EXTLANG,
76         };
77
78         class Subtag
79         {
80         public:
81                 virtual ~Subtag () {}
82
83                 std::string subtag () const {
84                         return _subtag;
85                 }
86
87                 virtual SubtagType type () const = 0;
88
89                 bool operator== (Subtag const& other) {
90                         return _subtag == other._subtag;
91                 }
92
93         protected:
94                 Subtag (std::string subtag, SubtagType type);
95
96         private:
97                 std::string _subtag;
98         };
99
100         class LanguageSubtag : public Subtag
101         {
102         public:
103                 LanguageSubtag (std::string subtag)
104                         : Subtag(subtag, LANGUAGE) {}
105                 LanguageSubtag (char const* subtag)
106                         : Subtag(subtag, LANGUAGE) {}
107
108                 SubtagType type () const {
109                         return LANGUAGE;
110                 }
111         };
112
113         class ScriptSubtag : public Subtag
114         {
115         public:
116                 ScriptSubtag (std::string subtag)
117                         : Subtag(subtag, SCRIPT) {}
118                 ScriptSubtag (char const* subtag)
119                         : Subtag(subtag, SCRIPT) {}
120
121                 SubtagType type () const {
122                         return SCRIPT;
123                 }
124         };
125
126         class RegionSubtag : public Subtag
127         {
128         public:
129                 RegionSubtag (std::string subtag)
130                         : Subtag(subtag, REGION) {}
131                 RegionSubtag (char const* subtag)
132                         : Subtag(subtag, REGION) {}
133
134                 SubtagType type () const {
135                         return REGION;
136                 }
137         };
138
139         class VariantSubtag : public Subtag
140         {
141         public:
142                 VariantSubtag (std::string subtag)
143                         : Subtag(subtag, VARIANT) {}
144                 VariantSubtag (char const* subtag)
145                         : Subtag(subtag, VARIANT) {}
146
147                 SubtagType type () const {
148                         return VARIANT;
149                 }
150
151                 bool operator== (VariantSubtag const& other) const;
152                 bool operator< (VariantSubtag const& other) const;
153         };
154
155
156         class ExtlangSubtag : public Subtag
157         {
158         public:
159                 ExtlangSubtag (std::string subtag)
160                         : Subtag(subtag, EXTLANG) {}
161                 ExtlangSubtag (char const* subtag)
162                         : Subtag(subtag, EXTLANG) {}
163
164                 SubtagType type () const {
165                         return EXTLANG;
166                 }
167
168                 bool operator== (ExtlangSubtag const& other) const;
169                 bool operator< (ExtlangSubtag const& other) const;
170         };
171
172         LanguageTag () {}
173         LanguageTag (std::string tag);
174
175         boost::optional<LanguageSubtag> language() {
176                 return _language;
177         }
178
179         void set_language (LanguageSubtag language);
180
181         boost::optional<ScriptSubtag> script() const {
182                 return _script;
183         }
184
185         void set_script (ScriptSubtag script);
186
187         boost::optional<RegionSubtag> region() const {
188                 return _region;
189         }
190
191         void set_region (RegionSubtag region);
192
193         std::vector<VariantSubtag> variants() const {
194                 return _variants;
195         }
196
197         void set_variants (std::vector<VariantSubtag> variants);
198         void add_variant (VariantSubtag variant);
199
200         std::vector<ExtlangSubtag> extlangs() const {
201                 return _extlangs;
202         }
203
204         void set_extlangs (std::vector<ExtlangSubtag> extlangs);
205         void add_extlang (ExtlangSubtag extlang);
206
207         std::vector<std::pair<SubtagType, SubtagData> > subtags () const;
208
209         static std::vector<SubtagData> get_all (SubtagType type);
210         static std::string subtag_type_name (SubtagType type);
211
212         static boost::optional<std::string> get_subtag_description (SubtagType, std::string subtag);
213         static boost::optional<SubtagData > get_subtag_data (SubtagType, std::string subtag);
214
215         template <class T>
216         static boost::optional<std::string> get_subtag_description (T s) {
217                 return get_subtag_description (s.type(), s.subtag());
218         }
219
220         template <class T>
221         static boost::optional<SubtagData> get_subtag_data (T s) {
222                 return get_subtag_data (s.type(), s.subtag());
223         }
224
225 private:
226
227         boost::optional<LanguageSubtag> _language;
228         boost::optional<ScriptSubtag> _script;
229         boost::optional<RegionSubtag> _region;
230         std::vector<VariantSubtag> _variants;
231         std::vector<ExtlangSubtag> _extlangs;
232 };
233
234 extern bool operator==(dcp::LanguageTag const& a, dcp::LanguageTag const& b);
235 extern std::ostream& operator<<(std::ostream& os, dcp::LanguageTag const& tag);
236
237 }
238
239 #endif