BAOS SDK v2  1.2.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) 2002-2026 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_{std::move(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_ = std::move(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  auto ownedFuture = std::move(fut);
111  const auto response = ownedFuture.get(); // Wait for the response to become ready
112  auto responseBaos = std::dynamic_pointer_cast<BaosBaseResponse>(
113  response); // get the value from the future and cast it to the base type
114  if (responseBaos->code == protocol::BaosResponseCode::NoItemFound)
115  {
116  return T(); // Return empty result
117  }
118  else if (responseBaos->code != protocol::BaosResponseCode::Success)
119  {
120  std::ostringstream oss;
121  oss << "Error response from BAOS " << protocol::baosResponseCodeToString(responseBaos->code);
122  throw wz::baos::WzResponseException(WZ_SL, oss.str().c_str(), static_cast<std::uint32_t>(responseBaos->code));
123  }
124  else
125  {
126  auto responseDerived = std::dynamic_pointer_cast<BaosResponse<T>>(
127  response); // get the value from the future and cast it to the actual type
128  return responseDerived->getPayload();
129  }
130 }
131 
134 
135 } // namespace wz::baos::protocol
136 
137 #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:33
BAOSLIB_EXPORT void waitAndHandleEmptyBaosResponse(ResponseFuture &&fut)
Handle responses with no payload.
BaosResponseCode
These are the BaosServerErrorCodes.
Definition: Defines.h:129
std::promise< BaseResponse::SPtr > ResponsePromise
Definition: Response.h:95
BAOSLIB_EXPORT std::string baosResponseCodeToString(BaosResponseCode code)
Convert the enum value to a human readable string.
std::future< BaseResponse::SPtr > ResponseFuture
Definition: Response.h:97
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