1 // ==========================================================================
2 //                 SeqAn - The Library for Sequence Analysis
3 // ==========================================================================
4 // Copyright (c) 2006-2018, Knut Reinert, FU Berlin
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 //
10 //     * Redistributions of source code must retain the above copyright
11 //       notice, this list of conditions and the following disclaimer.
12 //     * Redistributions in binary form must reproduce the above copyright
13 //       notice, this list of conditions and the following disclaimer in the
14 //       documentation and/or other materials provided with the distribution.
15 //     * Neither the name of Knut Reinert or the FU Berlin nor the names of
16 //       its contributors may be used to endorse or promote products derived
17 //       from this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
23 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29 // DAMAGE.
30 //
31 // ==========================================================================
32 // Author: Manuel Holtgrewe <manuel.holtgrewe@fu-berlin.de>
33 // ==========================================================================
34 // This is a simple example of template subclassing.
35 //
36 // Referenced by TemplateSubclassing.
37 // ==========================================================================
38 
39 #include <iostream>
40 
41 // Setup class hierarchy by using inheritance for the specialization tags.
42 
43 struct SpecA {};
44 struct SpecB {};
45 struct SpecC :
46     SpecB {};
47 struct SpecD :
48     SpecB {};
49 
50 // Base class -- most generic and thus a fallback.
51 template <typename TSpec>
52 struct MyClass
53 {
54     int x;
55 
MyClassMyClass56     MyClass() :
57         x(0)
58     {}
59 };
60 
61 // Specialization for classes B and D.  C automatically inherits everything
62 // from B and does not overwrite anything.
63 template <>
64 struct MyClass<SpecB>
65 {
66     int x;
67 
MyClassMyClass68     MyClass() :
69         x(1)
70     {}
71 };
72 
73 template <>
74 struct MyClass<SpecD>
75 {
76     int x;
77 
MyClassMyClass78     MyClass() :
79         x(2)
80     {}
81 };
82 
83 // Define some functions that demostrate overloading.
84 
85 // Most generic case, "base implementation".
86 template <typename TSpec>
foo(MyClass<TSpec> const & obj)87 void foo(MyClass<TSpec> const & obj)
88 {
89     std::cout << "foo(MyClass<typename TSpec> const & obj) called!  obj.x == " << obj.x << "\n";
90 }
91 
92 // This function overwrites the generic implementation of foo() for the
93 // specialization B.
94 template <>
foo(MyClass<SpecB> const & obj)95 void foo(MyClass<SpecB> const & obj)
96 {
97     std::cout << "foo(MyClass<SpecB> const & obj) called!  obj.x == " << obj.x << "\n";
98 }
99 
100 // This function overwrites the generic implementation of foo() for the
101 // specialization C.
102 template <>
foo(MyClass<SpecC> const & obj)103 void foo(MyClass<SpecC> const & obj)
104 {
105     std::cout << "foo(MyClass<SpecC> const & obj) called!  obj.x == " << obj.x << "\n";
106 }
107 
108 // The main function calls the functions from above.
main()109 int main()
110 {
111     std::cout << "Template Subclassing Demo\n";
112 
113     MyClass<SpecA> a;
114     MyClass<SpecB> b;
115     MyClass<SpecC> c;
116     MyClass<SpecD> d;
117 
118     foo(a);  // => foo(MyClass<typename TSpec> const & obj) called!  obj.x == 0
119     foo(b);  // => foo(MyClass<SpecB> const & obj) called!  obj.x == 1
120     foo(c);  // => foo(MyClass<SpecC> const & obj) called!  obj.x == 0
121     foo(d);  // => foo(MyClass<typename TSpec> const & obj) called!  obj.x == 2
122 
123     return 0;
124 }
125