BAOS SDK v2  1.0.1
An SDK providing access to IP-BAOS devices through BAOS binary protocol version 2.x
Response.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2021 WEINZIERL ENGINEERING GmbH
3 // All rights reserved.
4 //
5 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
8 // SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY,
9 // WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
10 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
11 //
12 
13 #ifndef __BAOSLIB_BAOSRESPONSE_H__
14 #define __BAOSLIB_BAOSRESPONSE_H__
15 
16 #include "baos/Exception.h"
17 #include "baos/protocol/Defines.h"
18 
20 #include <future>
21 #include <memory>
22 #include <sstream>
24 
30 namespace wz::baos::protocol
31 {
32 
35 {
36  using SPtr = std::shared_ptr<BaseResponse>;
37  BaseResponse() = default;
38  virtual ~BaseResponse() = default;
39 };
40 
41 
44 {
45  using SPtr =
46  std::shared_ptr<BaosBaseResponse>;
47 
48  BaosBaseResponse() = default;
49 
56  virtual ~BaosBaseResponse() = default;
57 
59 };
60 
61 
63 template<class T>
65 {
66  BaosResponse() = default;
73  BaosResponse(protocol::BaosResponseCode code_, T&& items) : BaosBaseResponse(code_), payload_{items} {};
74 
75  virtual ~BaosResponse() = default;
76  using SPtr = std::shared_ptr<BaosResponse>;
77 
80  {
81  return payload_;
82  } // getter
83 
85  void setPayload(T&& value)
86  {
87  payload_ = value;
88  } // setter
89 private:
90  T payload_;
91 };
92 
93 
94 using ResponsePromise = std::promise<BaseResponse::SPtr>;
96 using ResponseFuture = std::future<BaseResponse::SPtr>;
98 
99 
107 template<class T>
109 {
110  const auto response = fut.get(); // Wait for the response to become ready
111  auto responseBaos = std::dynamic_pointer_cast<BaosBaseResponse>(
112  response); // get the value from the future and cast it to the base type
113  if (responseBaos->code == protocol::BaosResponseCode::NoItemFound)
114  {
115  return T(); // Return empty result
116  }
117  else if (responseBaos->code != protocol::BaosResponseCode::Success)
118  {
119  std::ostringstream oss;
120  oss << "Error response from BAOS " << protocol::baosResponseCodeToString(responseBaos->code);
121  throw wz::baos::WzResponseException(WZ_SL, oss.str().c_str(), static_cast<std::uint32_t>(responseBaos->code));
122  }
123  else
124  {
125  auto responseDerived = std::dynamic_pointer_cast<BaosResponse<T>>(
126  response); // get the value from the future and cast it to the actual type
127  return responseDerived->getPayload();
128  }
129 }
130 
133 
134 } // namespace wz::baos::protocol
135 
136 #endif
Global BAOS protocol defines and types.
Custom exceptions for the SDK.
Groups BAOS binary protocol specific types , defines and classes for Indications, Responses etc.
Definition: Defines.h:32
BaosResponseCode
These are the BaosServerErrorCodes.
Definition: Defines.h:128
std::promise< BaseResponse::SPtr > ResponsePromise
Definition: Response.h:95
std::future< BaseResponse::SPtr > ResponseFuture
Definition: Response.h:97
void waitAndHandleEmptyBaosResponse(ResponseFuture &&fut)
Handle responses with no payload.
std::string baosResponseCodeToString(BaosResponseCode code)
Convert the enum value to a human readable string.
T waitAndHandleBaosResponse(ResponseFuture &&fut)
Helper function to wait for a ResponseFuture to become ready and handle it.
Definition: Response.h:108
Baseclass for BAOS Responses. Containes the response code and optional in derived classes the payload...
Definition: Response.h:44
BaosBaseResponse(protocol::BaosResponseCode code_)
Construct a new BaosBaseResponse object.
Definition: Response.h:55
protocol::BaosResponseCode code
holds the BaosResponse code
Definition: Response.h:58
An actual BAOS response with payload of template parameter T.
Definition: Response.h:65
void setPayload(T &&value)
Setter for payload.
Definition: Response.h:85
BaosResponse(protocol::BaosResponseCode code_, T &&items)
Construct a new BaosResponse object.
Definition: Response.h:73
T getPayload()
Getter for payload.
Definition: Response.h:79
Baseclass for generic responses.
Definition: Response.h:35
std::shared_ptr< BaseResponse > SPtr
An alias for a std::shared pointer holding an BaseResponse class.
Definition: Response.h:36