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/scope.h | |
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/scope.h')
-rw-r--r-- | src/scope.h | 97 |
1 files changed, 1 insertions, 96 deletions
diff --git a/src/scope.h b/src/scope.h index b49e3ebe..1cc2858e 100644 --- a/src/scope.h +++ b/src/scope.h @@ -158,13 +158,9 @@ public: } void set_args(const value_t& _args) { - if (_args.is_sequence()) - args = _args; - else - args = _args.to_sequence(); + args = _args; } value_t& value() { - assert(args.is_null() || args.is_sequence()); return args; } @@ -291,97 +287,6 @@ public: T * operator->() { return value; } }; -/** - * @brief Brief - * - * Long. - */ -template <typename T> -class var_t : public noncopyable -{ - optional<value_t> value; - - var_t(); - -public: - var_t(scope_t& scope, const string& name) - { - TRACE_CTOR(var_t, "scope_t&, const string&"); - - try { - value = scope.resolve(name); - } - catch (...) { - DEBUG("scope.var_t", "Failed lookup var_t(\"" << name << "\")"); - value = none; - } - } - - var_t(call_scope_t& scope, const std::size_t idx) - { - TRACE_CTOR(var_t, "call_scope_t&, const std::size_t"); - - if (idx < scope.size()) - value = scope[idx]; - else - value = none; - } - - ~var_t() throw() { - TRACE_DTOR(var_t); - } - - operator bool() { return value; } - - T operator *(); - T operator *() const; - - T * operator->() { - return &**this; - } - const T * operator->() const { - return &**this; - } -}; - -template <> -inline bool var_t<bool>::operator *() { - return value->to_boolean(); -} -template <> -inline bool var_t<bool>::operator *() const { - return value->to_boolean(); -} - -template <> -inline long var_t<long>::operator *() { - return value->to_long(); -} -template <> -inline long var_t<long>::operator *() const { - return value->to_long(); -} - -template <> -inline string var_t<string>::operator *() { - return value->to_string(); -} -template <> -inline string var_t<string>::operator *() const { - return value->to_string(); -} - -template <> -inline datetime_t var_t<datetime_t>::operator *() { - return value->to_datetime(); -} -template <> -inline datetime_t var_t<datetime_t>::operator *() const { - return value->to_datetime(); -} - -string join_args(call_scope_t& args); - } // namespace ledger #endif // _SCOPE_H |