aboutsummaryrefslogtreecommitdiffhomepage
path: root/td/telegram/CurrencyAmount.cpp
blob: be9fa4d4f5cd9b0dd17bf8f6fa972a2f72b30db2 (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
//
// 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)
//
#include "td/telegram/CurrencyAmount.h"

namespace td {

CurrencyAmount::CurrencyAmount(telegram_api::object_ptr<telegram_api::StarsAmount> &&amount_ptr, bool allow_negative) {
  if (amount_ptr == nullptr) {
    return;
  }
  switch (amount_ptr->get_id()) {
    case telegram_api::starsAmount::ID: {
      auto star_amount =
          StarAmount(telegram_api::move_object_as<telegram_api::starsAmount>(amount_ptr), allow_negative);
      if (star_amount == StarAmount()) {
        return;
      }
      type_ = Type::Star;
      star_amount_ = star_amount;
      break;
    }
    case telegram_api::starsTonAmount::ID: {
      auto ton_amount =
          TonAmount(telegram_api::move_object_as<telegram_api::starsTonAmount>(amount_ptr), allow_negative);
      if (ton_amount == TonAmount()) {
        return;
      }
      type_ = Type::Ton;
      ton_amount_ = ton_amount;
      break;
    }
    default:
      UNREACHABLE();
  }
}

bool operator==(const CurrencyAmount &lhs, const CurrencyAmount &rhs) {
  return lhs.type_ == rhs.type_ && lhs.star_amount_ == rhs.star_amount_ && lhs.ton_amount_ == rhs.ton_amount_;
}

bool operator!=(const CurrencyAmount &lhs, const CurrencyAmount &rhs) {
  return !(lhs == rhs);
}

StringBuilder &operator<<(StringBuilder &string_builder, const CurrencyAmount &amount) {
  switch (amount.type_) {
    case CurrencyAmount::Type::None:
      return string_builder << "[Free]";
    case CurrencyAmount::Type::Star:
      return string_builder << '[' << amount.star_amount_ << " Stars]";
    case CurrencyAmount::Type::Ton:
      return string_builder << '[' << amount.ton_amount_ << " nanograms]";
    default:
      UNREACHABLE();
      return string_builder;
  }
}

}  // namespace td