FinanceSim 0.1.0
Financial Simulation Library
Loading...
Searching...
No Matches
account_base.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "core/model.hpp"
4#include "core/event.hpp"
5
6namespace financesim {
7
10 Schedule s;
11 s.rate = 0.0; // Event-driven only, no periodic updates
12 return s;
13}
14
17class AccountBase : public Model {
18public:
19 AccountBase(std::string id, std::string name, std::string routing_tag,
20 double initial_balance = 0.0,
22 virtual ~AccountBase() = default;
23
24 const std::string& id() const override { return id_; }
25 const std::string& name() const override { return name_; }
26 const Schedule& schedule() const override { return schedule_; }
27
29 const std::string& routing_tag() const { return routing_tag_; }
30
32 double balance() const { return balance_; }
33
34 void initialize(EventBus& bus) override;
35 void update(SimTime time) override;
36 void finalize() override;
37 void reset() override;
38
39protected:
40 EventBus& bus() { return *bus_; }
41
43 void deposit(SimTime time, double amount, const std::string& reason);
44
46 void withdraw(SimTime time, double amount, const std::string& reason);
47
49 virtual void on_income(const IncomeEvent& event);
50
52 virtual void on_expense(const ExpenseEvent& event);
53
54 template<typename EventType, typename... Args>
55 void emit(SimTime time, Args&&... args) {
56 if (!bus_) return;
57 std::shared_ptr<const EventType> event = std::make_shared<EventType>(
58 time, id_, std::forward<Args>(args)...
59 );
61 }
62
63private:
64 bool should_handle_event(const std::string& target_account) const;
65
66 std::string id_;
67 std::string name_;
68 std::string routing_tag_;
69 double balance_;
72 EventBus* bus_ = nullptr;
75};
76
77} // namespace financesim
SubscriptionId income_sub_
virtual ~AccountBase()=default
const std::string & name() const override
Human-readable name/description.
const std::string & routing_tag() const
Get the routing tag used to match income/expense events.
virtual void on_income(const IncomeEvent &event)
Called when an income event is routed to this account.
SubscriptionId expense_sub_
void reset() override
Reset model to initial state (for replay)
void deposit(SimTime time, double amount, const std::string &reason)
Deposit funds into the account.
const std::string & id() const override
Unique identifier for this model instance.
void finalize() override
bool should_handle_event(const std::string &target_account) const
void update(SimTime time) override
void emit(SimTime time, Args &&... args)
double balance() const
Get current balance.
virtual void on_expense(const ExpenseEvent &event)
Called when an expense event is routed to this account.
void withdraw(SimTime time, double amount, const std::string &reason)
Withdraw funds from the account.
const Schedule & schedule() const override
Get the model's execution schedule.
void initialize(EventBus &bus) override
void publish(std::shared_ptr< const EventType > event)
Publish an event to all subscribers.
Definition event_bus.hpp:69
Event emitted when an expense occurs.
Definition event.hpp:62
Event emitted when income is received.
Definition event.hpp:39
double SimTime
Represents a point in simulation time (continuous, in days)
Definition time.hpp:6
std::size_t SubscriptionId
Subscription handle for unsubscribing.
Definition event_bus.hpp:16
Schedule make_account_schedule()
Helper to create default schedule for accounts (no periodic updates)