1 /*
2  * Copyright (c) 2003-2018, John Wiegley.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in the
13  *   documentation and/or other materials provided with the distribution.
14  *
15  * - Neither the name of New Artisans LLC nor the names of its
16  *   contributors may be used to endorse or promote products derived from
17  *   this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /**
33  * @addtogroup data
34  */
35 
36 /**
37  * @file   compare.h
38  * @author John Wiegley
39  *
40  * @ingroup data
41  */
42 #ifndef _COMPARE_H
43 #define _COMPARE_H
44 
45 #include "expr.h"
46 
47 namespace ledger {
48 
49 class post_t;
50 class account_t;
51 class report_t;
52 
53 void push_sort_value(std::list<sort_value_t>& sort_values,
54                      expr_t::ptr_op_t node, scope_t& scope);
55 
56 template <typename T>
57 class compare_items
58 {
59   expr_t sort_order;
60   report_t& report;
61 
62   compare_items();
63 
64 public:
compare_items(const expr_t & _sort_order,report_t & _report)65   compare_items(const expr_t& _sort_order, report_t& _report) :
66     sort_order(_sort_order), report(_report) {
67     TRACE_CTOR(compare_items, "const value_expr&, report_t&");
68   }
compare_items(const compare_items & other)69   compare_items(const compare_items& other) :
70     sort_order(other.sort_order), report(other.report) {
71     TRACE_CTOR(compare_items, "copy");
72   }
throw()73   ~compare_items() throw() {
74     TRACE_DTOR(compare_items);
75   }
76 
77   void find_sort_values(std::list<sort_value_t>& sort_values, scope_t& scope);
78 
79   bool operator()(T * left, T * right);
80 };
81 
82 sort_value_t calc_sort_value(const expr_t::ptr_op_t op);
83 
84 template <typename T>
operator()85 bool compare_items<T>::operator()(T * left, T * right)
86 {
87   assert(left); assert(right);
88   return sort_value_is_less_than(find_sort_values(left),
89                                  find_sort_values(right));
90 }
91 
92 template <>
93 bool compare_items<post_t>::operator()(post_t * left, post_t * right);
94 template <>
95 bool compare_items<account_t>::operator()(account_t * left,
96                                           account_t * right);
97 
98 } // namespace ledger
99 
100 #endif // _COMPARE_H
101