Cambridge SMT System
taskinterface.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use these files except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 // Copyright 2012 - Gonzalo Iglesias, AdriĆ  de Gispert, William Byrne
14 
21 #ifndef TASKINTERFACE_HPP
22 #define TASKINTERFACE_HPP
23 
24 namespace ucam {
25 namespace util {
26 
36 template <class Data>
38  private:
40  TaskInterface *next_;
41 
42  public:
44  TaskInterface() : next_ ( NULL ) { };
45 
46  virtual bool run ( Data& d ) = 0;
47  virtual ~TaskInterface() {
48  if ( next_ ) delete next_;
49  };
50 
58  inline bool chainrun ( Data& d ) {
59  bool r = run ( d );
60  if ( !r && next_ ) {
61  return next_->chainrun ( d );
62  };
63  return r;
64  };
65  inline bool operator() ( Data& d ) {
66  bool r = run ( d );
67  if ( !r && next_ ) {
68  return next_->chainrun ( d );
69  };
70  return r;
71  };
72 
79  if ( t == NULL ) return *this;
80  if ( !next_ ) {
81  next_ = t;
82  return *next_;
83  } else return ( *next_ ) ( t );
84  };
86  if ( t == NULL ) return *this;
87  if ( !next_ ) {
88  next_ = t;
89  return *next_;
90  } else return ( *next_ ) ( t );
91  };
92 
97  inline TaskInterface *getTask() {
98  return next_;
99  };
100  inline TaskInterface *next() {
101  return next_;
102  };
103 
104 };
105 
112 template<class Data>
113 class TaskFunctor {
114  private:
115  Data *d_;
116  TaskInterface<Data> * task_;
117  public:
119  : d_ (NULL)
120  , task_ (NULL) {
121  };
123  : d_ ( d )
124  , task_ ( ti ) {
125  };
126 
128  : d_ ( tf.d_)
129  , task_ (tf.task_) {
130  };
131 
132  void operator() () {
133  if ( USER_CHECK ( task_ != NULL && d_ != NULL
134  , "TaskFunctor not properly initialized with a task and a data objects" ) ) {
135  task_->chainrun ( *d_ );
136  delete task_;
137  delete d_;
138  task_ = NULL;
139  d_ = NULL;
140  }
141  return;
142  };
143 
144 };
145 
146 }
147 } // end namespaces
148 
149 #endif
TaskFunctor(TaskFunctor< Data > const &tf)
TaskInterface()
Constructor.
bool chainrun(Data &d)
Implements chain of responsability. Calls run method and, if there is another task, call its run method too.
Templated (hybrid) Interface for Task classes.
Simple functor that accepts an interface and pointer to the data object in which it will have to run ...
TaskFunctor(TaskInterface< Data > *ti, Data *d)
TaskInterface & appendTask(TaskInterface *t)
Appends a task class. If there is no task, append here, otherwise delegate in next task...
virtual bool run(Data &d)=0
#define USER_CHECK(exp, comment)
Tests whether exp is true. If not, comment is printed and program ends.
TaskInterface * getTask()
Return appended task.
Definition: bleu.hpp:14