Advertisement
kolbka_

Untitled

Feb 26th, 2022
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. #ifndef BANK_H
  2. #define BANK_H
  3.  
  4. #include <condition_variable>
  5. #include <iostream>
  6. #include <map>
  7. #include <memory>
  8. #include <mutex>
  9. #include <stdexcept>
  10. #include <string>
  11. #include <utility>
  12. #include <vector>
  13. namespace bank {
  14. const int INITIAL_BALANCE = 100;
  15. struct transfer_error : std::runtime_error {
  16.     explicit transfer_error(std::string &&message)
  17.         : std::runtime_error(message) {
  18.     }
  19. };
  20.  
  21. struct not_enough_funds_error : transfer_error {
  22.     explicit not_enough_funds_error(const int a, const int b)
  23.         : transfer_error("Not enough funds: " + std::to_string(a) +
  24.                          " XTS available, " + std::to_string(b) +
  25.                          " XTS requested") {
  26.     }
  27. };
  28.  
  29. struct error_with_transaction : transfer_error {
  30.     explicit error_with_transaction(std::string &&message)
  31.         : transfer_error(std::move(message)) {
  32.     }
  33. };
  34.  
  35. struct error_with_arguments : std::runtime_error {
  36.     explicit error_with_arguments()
  37.         : std::runtime_error("less arguments than must be") {
  38.     }
  39. };
  40. class user;
  41.  
  42. struct transaction {
  43.     // NOLINTNEXTLINE
  44.     const user *const counterparty;
  45.     // NOLINTNEXTLINE
  46.     const int balance_delta_xts;
  47.     // NOLINTNEXTLINE
  48.     const std::string comment;
  49.     // NOLINTNEXTLINE
  50.     transaction(const user *recipient,
  51.                 int amount,
  52.                 // cppcheck-suppress passedByValue
  53.                 std::string comment) noexcept
  54.         : counterparty(recipient),
  55.           balance_delta_xts(amount),
  56.           comment(std::move(comment)) {
  57.     }
  58. };
  59.  
  60. class user_transactions_iterator {
  61. private:
  62.     size_t size_ = 0;
  63.     const user *pointer_on_user_;
  64.  
  65. public:
  66.     user_transactions_iterator(const size_t current_count_transactions,
  67.                                const user *pointer_on_user)
  68.         : size_(current_count_transactions), pointer_on_user_(pointer_on_user) {
  69.     }
  70.     transaction wait_next_transaction() noexcept;
  71. };
  72.  
  73. class user {
  74. private:
  75.     std::string user_name;
  76.     int balance = 0;
  77.     mutable std::mutex user_mutex;
  78.     std::vector<transaction> operations;
  79.     mutable std::condition_variable cond;
  80.  
  81. public:
  82.     explicit user();
  83.     explicit user(const std::string &new_name);
  84.  
  85.     void transfer(user &counterparty,
  86.                   int amount_xts,
  87.                   const std::string &comment);
  88.  
  89.     [[nodiscard]] int balance_xts() const;
  90.  
  91.     [[nodiscard]] user_transactions_iterator monitor() const noexcept;
  92.  
  93.     template <typename T>
  94.     user_transactions_iterator snapshot_transactions(const T &functor) const {
  95.         std::unique_lock ul(user_mutex);
  96.         functor(operations, balance);
  97.         return user_transactions_iterator{operations.size(), this};
  98.     }
  99.     [[nodiscard]] std::string name() const noexcept;
  100.     friend user_transactions_iterator;
  101. };
  102.  
  103. struct ledger {
  104. private:
  105.     std::map<std::string, user> users;
  106.     mutable std::mutex ledger_mutex;
  107.  
  108. public:
  109.     ledger() = default;
  110.     user &get_or_create_user(const std::string &name) noexcept;
  111. };
  112.  
  113. }  // namespace bank
  114.  
  115. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement