response.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 The urg_stamped Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SCIP2_RESPONSE_H
18 #define SCIP2_RESPONSE_H
19 
20 #include <boost/asio.hpp>
21 
22 #include <map>
23 #include <string>
24 
27 #include <scip2/response/stream.h>
29 #include <scip2/response/quit.h>
30 
31 namespace scip2
32 {
34 {
35 protected:
36  std::map<std::string, Response::Ptr> responses_;
38  {
39  responses_[response->getCommandCode()] = response;
40  }
41 
42 public:
44  {
52  }
53  void operator()(
54  const boost::posix_time::ptime &time_read,
55  const std::string &echo_back,
56  const std::string &status,
57  std::istream &stream) const
58  {
59  const std::string command_code(echo_back.substr(0, 2));
60  const auto response = responses_.find(command_code);
61  if (response == responses_.end())
62  {
63  std::cerr << "Unknown response " << command_code << std::endl;
64  return;
65  }
66  (*(response->second))(time_read, echo_back, status, stream);
67  }
68  template <typename TResponse>
69  void registerCallback(typename TResponse::Callback cb)
70  {
71  const auto response = responses_.find(TResponse().getCommandCode());
72  assert(response != responses_.end());
73  auto response_downcast = std::dynamic_pointer_cast<TResponse>(response->second);
74  assert(response_downcast);
75 
76  response_downcast->registerCallback(cb);
77  }
78 };
79 
80 } // namespace scip2
81 
82 #endif // SCIP2_RESPONSE_H
void registerResponse(Response::Ptr response)
Definition: response.h:37
std::shared_ptr< Response > Ptr
Definition: abstract.h:29
void registerCallback(typename TResponse::Callback cb)
Definition: response.h:69
std::map< std::string, Response::Ptr > responses_
Definition: response.h:36
void operator()(const boost::posix_time::ptime &time_read, const std::string &echo_back, const std::string &status, std::istream &stream) const
Definition: response.h:53
const std::string response


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Thu Jun 6 2019 19:55:58