ledger/src/item.cc

141 lines
3.7 KiB
C++

/*
* Copyright (c) 2003-2008, John Wiegley. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of New Artisans LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "item.h"
#include "session.h"
#include "report.h"
namespace ledger {
bool item_t::use_effective_date = false;
namespace {
value_t get_status(item_t& item) {
return long(item.state());
}
value_t get_cleared(item_t& item) {
return item.state() == item_t::CLEARED;
}
value_t get_pending(item_t& item) {
return item.state() == item_t::PENDING;
}
value_t get_date(item_t& item) {
if (optional<date_t> date = item.date())
return *date;
else
return 0L;
}
value_t get_note(item_t& item) {
return string_value(item.note ? *item.note : empty_string);
}
value_t get_beg_pos(item_t& item) {
return long(item.beg_pos);
}
value_t get_beg_line(item_t& item) {
return long(item.beg_line);
}
value_t get_end_pos(item_t& item) {
return long(item.end_pos);
}
value_t get_end_line(item_t& item) {
return long(item.end_line);
}
template <value_t (*Func)(item_t&)>
value_t get_wrapper(call_scope_t& scope) {
return (*Func)(find_scope<item_t>(scope));
}
}
expr_t::ptr_op_t item_t::lookup(const string& name)
{
switch (name[0]) {
case 'c':
if (name == "cleared")
return WRAP_FUNCTOR(get_wrapper<&get_cleared>);
break;
case 'd':
if (name[1] == '\0' || name == "date")
return WRAP_FUNCTOR(get_wrapper<&get_date>);
break;
case 'n':
if (name == "note")
return WRAP_FUNCTOR(get_wrapper<&get_note>);
break;
case 'p':
if (name == "pending")
return WRAP_FUNCTOR(get_wrapper<&get_pending>);
break;
case 's':
if (name == "status")
return WRAP_FUNCTOR(get_wrapper<&get_status>);
break;
case 'u':
if (name == "uncleared")
return expr_t::op_t::wrap_value(1L);
break;
case 'X':
if (name[1] == '\0')
return WRAP_FUNCTOR(get_wrapper<&get_cleared>);
break;
case 'Y':
if (name[1] == '\0')
return WRAP_FUNCTOR(get_wrapper<&get_pending>);
break;
}
return session_t::current->current_report->lookup(name);
}
bool item_t::valid() const
{
if (_state != UNCLEARED && _state != CLEARED && _state != PENDING) {
DEBUG("ledger.validate", "item_t: state is bad");
return false;
}
return true;
}
} // namespace ledger