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/quit.h>
28 #include <scip2/response/reboot.h>
29 #include <scip2/response/reset.h>
30 #include <scip2/response/stream.h>
32 #include <scip2/logger.h>
33 
34 namespace scip2
35 {
37 {
38 protected:
39  std::map<std::string, Response::Ptr> responses_;
41  {
42  responses_[response->getCommandCode()] = response;
43  }
44 
45 public:
47  {
58  }
59  void operator()(
60  const boost::posix_time::ptime& time_read,
61  const std::string& echo_back,
62  const std::string& status,
63  std::istream& stream) const
64  {
65  const std::string command_code(echo_back.substr(0, 2));
66  const auto response = responses_.find(command_code);
67  if (response == responses_.end())
68  {
69  logger::debug() << "Unknown response " << command_code << std::endl;
70  return;
71  }
72  (*(response->second))(time_read, echo_back, status, stream);
73  }
74  template <typename TResponse>
75  void registerCallback(typename TResponse::Callback cb)
76  {
77  const auto response = responses_.find(TResponse().getCommandCode());
78  assert(response != responses_.end());
79  auto response_downcast = std::dynamic_pointer_cast<TResponse>(response->second);
80  assert(response_downcast);
81 
82  response_downcast->registerCallback(cb);
83  }
84 };
85 
86 } // namespace scip2
87 
88 #endif // SCIP2_RESPONSE_H
void registerResponse(Response::Ptr response)
Definition: response.h:40
std::ostream & debug()
Definition: logger.cpp:93
std::shared_ptr< Response > Ptr
Definition: abstract.h:30
void registerCallback(typename TResponse::Callback cb)
Definition: response.h:75
std::map< std::string, Response::Ptr > responses_
Definition: response.h:39
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:59


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Tue May 11 2021 02:14:05