1 /** 2 dtl -- Diff Template Library 3 4 In short, Diff Template Library is distributed under so called "BSD license", 5 6 Copyright (c) 2015 Tatsuhiko Kubo <cubicdaiya@gmail.com> 7 All rights reserved. 8 9 Redistribution and use in source and binary forms, with or without modification, 10 are permitted provided that the following conditions are met: 11 12 * Redistributions of source code must retain the above copyright notice, 13 this list of conditions and the following disclaimer. 14 15 * Redistributions in binary form must reproduce the above copyright notice, 16 this list of conditions and the following disclaimer in the documentation 17 and/or other materials provided with the distribution. 18 19 * Neither the name of the authors nor the names of its contributors 20 may be used to endorse or promote products derived from this software 21 without specific prior written permission. 22 23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 /* If you use this library, you must include dtl.hpp only. */ 37 38 #ifndef DTL_FUNCTORS_H 39 #define DTL_FUNCTORS_H 40 41 namespace dtl { 42 43 /** 44 * printer class template 45 */ 46 template <typename sesElem, typename stream = ostream > 47 class Printer 48 { 49 public : Printer()50 Printer () : out_(cout) {} Printer(stream & out)51 Printer (stream& out) : out_(out) {} ~Printer()52 virtual ~Printer () {} 53 virtual void operator() (const sesElem& se) const = 0; 54 protected : 55 stream& out_; 56 }; 57 58 /** 59 * common element printer class template 60 */ 61 template <typename sesElem, typename stream = ostream > 62 class CommonPrinter : public Printer < sesElem, stream > 63 { 64 public : CommonPrinter()65 CommonPrinter () : Printer < sesElem, stream > () {} CommonPrinter(stream & out)66 CommonPrinter (stream& out) : Printer < sesElem, stream > (out) {} ~CommonPrinter()67 ~CommonPrinter () {} operator ()(const sesElem & se) const68 void operator() (const sesElem& se) const { 69 this->out_ << SES_MARK_COMMON << se.first << endl; 70 } 71 }; 72 73 /** 74 * ses element printer class template 75 */ 76 template <typename sesElem, typename stream = ostream > 77 class ChangePrinter : public Printer < sesElem, stream > 78 { 79 public : ChangePrinter()80 ChangePrinter () : Printer < sesElem, stream > () {} ChangePrinter(stream & out)81 ChangePrinter (stream& out) : Printer < sesElem, stream > (out) {} ~ChangePrinter()82 ~ChangePrinter () {} operator ()(const sesElem & se) const83 void operator() (const sesElem& se) const { 84 switch (se.second.type) { 85 case SES_ADD: 86 this->out_ << SES_MARK_ADD << se.first << endl; 87 break; 88 case SES_DELETE: 89 this->out_ << SES_MARK_DELETE << se.first << endl; 90 break; 91 case SES_COMMON: 92 this->out_ << SES_MARK_COMMON << se.first << endl; 93 break; 94 } 95 } 96 }; 97 98 /** 99 * unified format element printer class template 100 */ 101 template <typename sesElem, typename stream = ostream > 102 class UniHunkPrinter 103 { 104 public : UniHunkPrinter()105 UniHunkPrinter () : out_(cout) {} UniHunkPrinter(stream & out)106 UniHunkPrinter (stream& out) : out_(out) {} ~UniHunkPrinter()107 ~UniHunkPrinter () {} operator ()(const uniHunk<sesElem> & hunk) const108 void operator() (const uniHunk< sesElem >& hunk) const { 109 out_ << "@@" 110 << " -" << hunk.a << "," << hunk.b 111 << " +" << hunk.c << "," << hunk.d 112 << " @@" << endl; 113 114 for_each(hunk.common[0].begin(), hunk.common[0].end(), CommonPrinter< sesElem, stream >(out_)); 115 for_each(hunk.change.begin(), hunk.change.end(), ChangePrinter< sesElem, stream >(out_)); 116 for_each(hunk.common[1].begin(), hunk.common[1].end(), CommonPrinter< sesElem, stream >(out_)); 117 } 118 private : 119 stream& out_; 120 }; 121 122 /** 123 * compare class template 124 */ 125 template <typename elem> 126 class Compare 127 { 128 public : Compare()129 Compare () {} ~Compare()130 virtual ~Compare () {} impl(const elem & e1,const elem & e2) const131 virtual inline bool impl (const elem& e1, const elem& e2) const { 132 return e1 == e2; 133 } 134 }; 135 } 136 137 #endif // DTL_FUNCTORS_H 138