summaryrefslogtreecommitdiff
path: root/ledger.h
diff options
context:
space:
mode:
Diffstat (limited to 'ledger.h')
-rw-r--r--ledger.h61
1 files changed, 57 insertions, 4 deletions
diff --git a/ledger.h b/ledger.h
index 8f56e950..e52a1e83 100644
--- a/ledger.h
+++ b/ledger.h
@@ -16,8 +16,9 @@
#include <ctime>
#include <iostream>
-#include "debug.h"
#include "amount.h"
+#include "value.h"
+#include "debug.h"
namespace ledger {
@@ -64,6 +65,61 @@ class transaction_t
bool valid() const;
};
+inline
+balance_pair_t& add_transaction_to(const transaction_t& xact,
+ balance_pair_t& bal_pair)
+{
+ if (xact.cost && ! bal_pair.cost)
+ bal_pair.cost = new balance_t(bal_pair.quantity);
+
+ bal_pair.quantity += xact.amount;
+
+ if (bal_pair.cost)
+ *bal_pair.cost += xact.cost ? *xact.cost : xact.amount;
+
+ return bal_pair;
+}
+
+inline
+value_t& add_transaction_to(const transaction_t& xact, value_t& value)
+{
+ switch (value.type) {
+ case value_t::BOOLEAN:
+ case value_t::INTEGER:
+ value.cast(value_t::AMOUNT);
+
+ case value_t::AMOUNT:
+ if (xact.cost) {
+ value.cast(value_t::BALANCE_PAIR);
+ return add_transaction_to(xact, value);
+ }
+ else if (((amount_t *) value.data)->commodity() !=
+ xact.amount.commodity()) {
+ value.cast(value_t::BALANCE);
+ return add_transaction_to(xact, value);
+ }
+ *((amount_t *) value.data) += xact.amount;
+ break;
+
+ case value_t::BALANCE:
+ if (xact.cost) {
+ value.cast(value_t::BALANCE_PAIR);
+ return add_transaction_to(xact, value);
+ }
+ *((balance_t *) value.data) += xact.amount;
+ break;
+
+ case value_t::BALANCE_PAIR:
+ add_transaction_to(xact, *((balance_pair_t *) value.data));
+ break;
+
+ default:
+ assert(0);
+ break;
+ }
+
+ return value;
+}
typedef std::deque<transaction_t *> transactions_list;
@@ -211,9 +267,6 @@ class journal_t
extern const std::string version;
-void initialize();
-void shutdown();
-
} // namespace ledger
#endif // _LEDGER_H