OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/ntp_snippets/category.h" | 5 #include "components/ntp_snippets/category.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 | 8 |
9 namespace ntp_snippets { | 9 namespace ntp_snippets { |
10 | 10 |
| 11 // static |
| 12 Category Category::FromKnownCategory(KnownCategories known_category) { |
| 13 return FromIDValue(static_cast<int>(known_category)); |
| 14 } |
| 15 |
| 16 // static |
| 17 Category Category::FromRemoteCategory(int remote_category) { |
| 18 DCHECK_GT(remote_category, 0); |
| 19 return Category(static_cast<int>(KnownCategories::REMOTE_CATEGORIES_OFFSET) + |
| 20 remote_category); |
| 21 } |
| 22 |
| 23 // static |
| 24 Category Category::FromIDValue(int id) { |
| 25 DCHECK_GE(id, 0); |
| 26 DCHECK(id < static_cast<int>(KnownCategories::LOCAL_CATEGORIES_COUNT) || |
| 27 id > static_cast<int>(KnownCategories::REMOTE_CATEGORIES_OFFSET)); |
| 28 return Category(id); |
| 29 } |
| 30 |
11 Category::Category(int id) : id_(id) {} | 31 Category::Category(int id) : id_(id) {} |
12 | 32 |
13 bool Category::IsKnownCategory(KnownCategories known_category) const { | 33 bool Category::IsKnownCategory(KnownCategories known_category) const { |
14 DCHECK_NE(known_category, KnownCategories::LOCAL_CATEGORIES_COUNT); | 34 DCHECK_NE(known_category, KnownCategories::LOCAL_CATEGORIES_COUNT); |
15 DCHECK_NE(known_category, KnownCategories::REMOTE_CATEGORIES_OFFSET); | 35 DCHECK_NE(known_category, KnownCategories::REMOTE_CATEGORIES_OFFSET); |
16 return id_ == static_cast<int>(known_category); | 36 return id_ == static_cast<int>(known_category); |
17 } | 37 } |
18 | 38 |
19 bool operator==(const Category& left, const Category& right) { | 39 bool operator==(const Category& left, const Category& right) { |
20 return left.id() == right.id(); | 40 return left.id() == right.id(); |
21 } | 41 } |
22 | 42 |
23 bool operator!=(const Category& left, const Category& right) { | 43 bool operator!=(const Category& left, const Category& right) { |
24 return !(left == right); | 44 return !(left == right); |
25 } | 45 } |
26 | 46 |
27 bool Category::CompareByID::operator()(const Category& left, | 47 bool Category::CompareByID::operator()(const Category& left, |
28 const Category& right) const { | 48 const Category& right) const { |
29 return left.id() < right.id(); | 49 return left.id() < right.id(); |
30 } | 50 } |
31 | 51 |
32 std::ostream& operator<<(std::ostream& os, const Category& obj) { | 52 std::ostream& operator<<(std::ostream& os, const Category& obj) { |
33 os << obj.id(); | 53 os << obj.id(); |
34 return os; | 54 return os; |
35 } | 55 } |
36 | 56 |
37 } // namespace ntp_snippets | 57 } // namespace ntp_snippets |
OLD | NEW |