aboutsummaryrefslogtreecommitdiffhomepage
path: root/td/telegram/StoryListId.h
blob: 6ace0b59e84c38c369a67d61279cdb6ec1d34a41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once

#include "td/telegram/td_api.h"

#include "td/utils/common.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/StringBuilder.h"

namespace td {

class StoryListId {
  enum class Type : int32 { None = -1, Main, Archive };
  Type type_ = Type::None;

  friend struct StoryListIdHash;

  explicit StoryListId(Type type) : type_(type) {
  }

 public:
  StoryListId() = default;

  explicit StoryListId(const td_api::object_ptr<td_api::StoryList> &story_list) {
    if (story_list == nullptr) {
      return;
    }
    switch (story_list->get_id()) {
      case td_api::storyListMain::ID:
        type_ = Type::Main;
        break;
      case td_api::storyListArchive::ID:
        type_ = Type::Archive;
        break;
      default:
        UNREACHABLE();
    }
  }

  static StoryListId main() {
    return StoryListId(Type::Main);
  }

  static StoryListId archive() {
    return StoryListId(Type::Archive);
  }

  td_api::object_ptr<td_api::StoryList> get_story_list_object() const {
    switch (type_) {
      case Type::None:
        return nullptr;
      case Type::Main:
        return td_api::make_object<td_api::storyListMain>();
      case Type::Archive:
        return td_api::make_object<td_api::storyListArchive>();
      default:
        UNREACHABLE();
    }
  }

  bool is_valid() const {
    return type_ == Type::Main || type_ == Type::Archive;
  }

  bool operator==(const StoryListId &other) const {
    return type_ == other.type_;
  }

  bool operator!=(const StoryListId &other) const {
    return type_ != other.type_;
  }
};

struct StoryListIdHash {
  uint32 operator()(StoryListId story_list_id) const {
    return Hash<int32>()(static_cast<int32>(story_list_id.type_));
  }
};

inline StringBuilder &operator<<(StringBuilder &string_builder, StoryListId story_list_id) {
  if (story_list_id == StoryListId::main()) {
    return string_builder << "MainStoryList";
  }
  if (story_list_id == StoryListId::archive()) {
    return string_builder << "ArchiveStoryList";
  }
  return string_builder << "InvalidStoryList";
}

}  // namespace td