diff options
author | John Wiegley <johnw@newartisans.com> | 2009-02-21 18:49:43 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-02-21 18:49:43 -0400 |
commit | 1f39d4148e588a5476e96550591131ae65ad0fe2 (patch) | |
tree | 86283fd983d6926c3f008f28e790b1d7ddee43ca /src/xact.cc | |
parent | 45e41b023a197151112199db893df70d21858a92 (diff) | |
download | fork-ledger-1f39d4148e588a5476e96550591131ae65ad0fe2.tar.gz fork-ledger-1f39d4148e588a5476e96550591131ae65ad0fe2.tar.bz2 fork-ledger-1f39d4148e588a5476e96550591131ae65ad0fe2.zip |
Create a new interactive_t helper class
The purpose of this class is much like Emacs' (interactive) form: it
allows a value expression function to declare exactly how many
arguments, and of what type, it intends to receive. It then offers
type-safe access to theese arguments in a consistent manner.
An example value expression function definition in C++:
value_t fn_foo(call_scope_t& scope) {
// We expect a string, an integer, and an optional date
interactive_t args(scope, "sl&d");
std::cout << "String = " << args.get<string>(0)
<< "Integer = " << args.get<long>(1) << std::endl;
if (args.has(2)) // was a date provided?
std::cout << "Date = " << args.get<date_t>(2) << std::endl;
return NULL_VALUE;
}
There is also an in_context_t<T> template, which finds the context type
T in the current scope hierarchy. The in_context_t then also acts as a
smart pointer to reference this context object, in addition to serving
the same duty as interactive_t. This combination of intent is solely
for the sake of brevity.
value_t fn_bar(call_scope_t& scope) {
in_context_t<account_t> env(scope, "sl&d");
std::cout << "Account name = " << env->fullname()
<< "String arg = " << env.get<string>(0)
<< std::endl;
return NULL_VALUE;
}
As you can see here, 'env' acts as a smart pointer to the required
context, and an object to extract the typed arguments.
Diffstat (limited to 'src/xact.cc')
-rw-r--r-- | src/xact.cc | 15 |
1 files changed, 7 insertions, 8 deletions
diff --git a/src/xact.cc b/src/xact.cc index 71fba7a7..ca0b9baf 100644 --- a/src/xact.cc +++ b/src/xact.cc @@ -32,6 +32,7 @@ #include "xact.h" #include "journal.h" #include "account.h" +#include "interactive.h" #include "format.h" namespace ledger { @@ -186,17 +187,15 @@ namespace { value_t get_account(call_scope_t& scope) { - xact_t& xact(find_scope<xact_t>(scope)); + in_context_t<xact_t> env(scope, "&l"); - var_t<long> max_width(scope, 0); + string name = env->reported_account()->fullname(); - string name = xact.reported_account()->fullname(); + if (env.has(0) && env.get<long>(0) > 2) + name = format_t::truncate(name, env.get<long>(0) - 2, true); - if (max_width && *max_width > 2) - name = format_t::truncate(name, *max_width - 2, true); - - if (xact.has_flags(XACT_VIRTUAL)) { - if (xact.must_balance()) + if (env->has_flags(XACT_VIRTUAL)) { + if (env->must_balance()) name = string("[") + name + "]"; else name = string("(") + name + ")"; |