1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 #ifndef CSV_TPL_PROCESSOR_HXX
25 #define CSV_TPL_PROCESSOR_HXX
26
27 // USED SERVICES
28
29
30
31
32 namespace csv
33 {
34
35
36 /** Implements an acyclic visitor pattern. This is the abstract
37 base for the classes doing the work (the "visitors").
38 */
39 class ProcessorIfc
40 {
41 public:
~ProcessorIfc()42 virtual ~ProcessorIfc() {}
43 };
44
45
46
47 /** Implements an acyclic visitor pattern. This is the abstract
48 base for the classes to be processed (the "visitables").
49 */
50 class ConstProcessorClient
51 {
52 public:
~ConstProcessorClient()53 virtual ~ConstProcessorClient() {}
54
Accept(ProcessorIfc & io_processor) const55 void Accept(
56 ProcessorIfc & io_processor ) const
57 { do_Accept(io_processor); }
58 private:
59 virtual void do_Accept(
60 ProcessorIfc & io_processor ) const = 0;
61 };
62
63 /** Implements an acyclic visitor pattern. This is the abstract
64 base for the classes to be processed (the "visitables").
65 */
66 class ProcessorClient
67 {
68 public:
~ProcessorClient()69 virtual ~ProcessorClient() {}
70
Accept(ProcessorIfc & io_processor)71 void Accept(
72 ProcessorIfc & io_processor )
73 { do_Accept(io_processor); }
74 private:
75 virtual void do_Accept(
76 ProcessorIfc & io_processor ) = 0;
77 };
78
79
80
81
82
83 /** Typed base for "visitor" classes, leaving the visited
84 object const.
85
86 @see ProcessorIfc
87 @see Processor<>
88 */
89 template <typename X, typename R = void>
90 class ConstProcessor
91 {
92 public:
~ConstProcessor()93 virtual ~ConstProcessor() {}
94
Process(const X & i_object)95 R Process(
96 const X & i_object )
97 { return do_Process(i_object ); }
98 private:
99 virtual R do_Process(
100 const X & i_object ) = 0;
101 };
102
103
104 /** Typed base for "visitor" classes which may change the visited
105 object.
106
107 @see ProcessorIfc
108 @see ConstProcessor<>
109 */
110 template <typename X, typename R = void>
111 class Processor
112 {
113 public:
~Processor()114 virtual ~Processor() {}
115
Process(X & i_object)116 R Process(
117 X & i_object )
118 { return do_Process(i_object ); }
119 private:
120 virtual R do_Process(
121 X & i_object ) = 0;
122 };
123
124
125 template <class C>
126 inline void
CheckedCall(ProcessorIfc & io_processor,const C & i_client)127 CheckedCall( ProcessorIfc & io_processor,
128 const C & i_client )
129 {
130 ConstProcessor<C> *
131 pProcessor = dynamic_cast< csv::ConstProcessor<C> * >
132 (&io_processor);
133 if (pProcessor != 0)
134 pProcessor->Process(i_client);
135 }
136
137 template <class C>
138 inline void
CheckedCall(ProcessorIfc & io_processor,C & io_client)139 CheckedCall( ProcessorIfc & io_processor,
140 C & io_client )
141 {
142 Processor<C> *
143 pProcessor = dynamic_cast< csv::Processor<C> * >
144 (&io_processor);
145 if (pProcessor != 0)
146 pProcessor->Process(io_client);
147 }
148
149 template <class C>
150 inline void
AssertedCall(ProcessorIfc & io_processor,const C & i_client)151 AssertedCall( ProcessorIfc & io_processor,
152 const C & i_client )
153 {
154 ConstProcessor<C> *
155 pProcessor = dynamic_cast< csv::ConstProcessor<C> * >
156 (&io_processor);
157 csv_assert( pProcessor != 0
158 && "csv::AssertedCall() failed. Processed object did not match processor." );
159 pProcessor->Process(i_client);
160 }
161
162 template <class C>
163 inline void
AssertedCall(ProcessorIfc & io_processor,C & io_client)164 AssertedCall( ProcessorIfc & io_processor,
165 C & io_client )
166 {
167 Processor<C> *
168 pProcessor = dynamic_cast< csv::Processor<C> * >
169 (&io_processor);
170 csv_assert( pProcessor != 0
171 && "csv::AssertedCall() failed. Processed object did not match processor." );
172 pProcessor->Process(io_client);
173 }
174
175
176
177
178 } // namespace csv
179 #endif
180