|
| 1 | +// |
| 2 | +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2025 |
| 3 | +// |
| 4 | +// Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +// |
| 7 | +#include "td/telegram/CurrencyAmount.h" |
| 8 | + |
| 9 | +#include "td/utils/logging.h" |
| 10 | + |
| 11 | +namespace td { |
| 12 | + |
| 13 | +CurrencyAmount::CurrencyAmount(telegram_api::object_ptr<telegram_api::StarsAmount> &&amount_ptr, bool allow_negative) { |
| 14 | + if (amount_ptr == nullptr) { |
| 15 | + return; |
| 16 | + } |
| 17 | + switch (amount_ptr->get_id()) { |
| 18 | + case telegram_api::starsAmount::ID: { |
| 19 | + auto star_amount = |
| 20 | + StarAmount(telegram_api::move_object_as<telegram_api::starsAmount>(amount_ptr), allow_negative); |
| 21 | + if (star_amount == StarAmount()) { |
| 22 | + return; |
| 23 | + } |
| 24 | + type_ = Type::Star; |
| 25 | + star_amount_ = star_amount; |
| 26 | + break; |
| 27 | + } |
| 28 | + case telegram_api::starsTonAmount::ID: { |
| 29 | + auto ton_amount = |
| 30 | + TonAmount(telegram_api::move_object_as<telegram_api::starsTonAmount>(amount_ptr), allow_negative); |
| 31 | + if (ton_amount == TonAmount()) { |
| 32 | + return; |
| 33 | + } |
| 34 | + type_ = Type::Ton; |
| 35 | + ton_amount_ = ton_amount; |
| 36 | + break; |
| 37 | + } |
| 38 | + default: |
| 39 | + UNREACHABLE(); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +bool operator==(const CurrencyAmount &lhs, const CurrencyAmount &rhs) { |
| 44 | + return lhs.type_ == rhs.type_ && lhs.star_amount_ == rhs.star_amount_ && lhs.ton_amount_ == rhs.ton_amount_; |
| 45 | +} |
| 46 | + |
| 47 | +bool operator!=(const CurrencyAmount &lhs, const CurrencyAmount &rhs) { |
| 48 | + return !(lhs == rhs); |
| 49 | +} |
| 50 | + |
| 51 | +StringBuilder &operator<<(StringBuilder &string_builder, const CurrencyAmount &amount) { |
| 52 | + switch (amount.type_) { |
| 53 | + case CurrencyAmount::Type::None: |
| 54 | + return string_builder << "[Free]"; |
| 55 | + case CurrencyAmount::Type::Star: |
| 56 | + return string_builder << '[' << amount.star_amount_ << " Stars]"; |
| 57 | + case CurrencyAmount::Type::Ton: |
| 58 | + return string_builder << '[' << amount.ton_amount_ << " nanotoncoins]"; |
| 59 | + default: |
| 60 | + UNREACHABLE(); |
| 61 | + return string_builder; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +} // namespace td |
0 commit comments