Microsoft Information Protection (MIP) SDK for C++: Reference 1.15
Doxygen-generated documentation for MIP SDK written in C++
Loading...
Searching...
No Matches
delegate_response.h
Go to the documentation of this file.
1/*
2 *
3 * Copyright (c) Microsoft Corporation.
4 * All rights reserved.
5 *
6 * This code is licensed under the MIT License.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files(the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions :
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 *
26 */
27/**
28 * @brief A simple framework to create a response to delegate calls that can result in error
29 *
30 * @file delegate_response.h
31 */
32
33#ifndef API_MIP_DELEGATE_RESPONSE
34#define API_MIP_DELEGATE_RESPONSE
35
36#include <memory>
37
38#include "mip/error.h"
39#include "mip/mip_namespace.h"
40
41MIP_NAMESPACE_BEGIN
42
43/**
44 * @brief Base class for all DelegateResponse classes
45 */
46class DelegateResponseBase {
47 /** @cond DOXYGEN_HIDE */
48protected:
49 DelegateResponseBase() : mReserved(0) {}
50 // For internal purpose only
51 long mReserved;
52 static long GetReserved(const DelegateResponseBase& response) {
53 return response.mReserved;
54 }
55 /** DOXYGEN_HIDE*/
56};
57
58/**
59 * @brief Template class to return delegate operation results
60 */
61template <class T> class DelegateResponse : public DelegateResponseBase {
62public:
63 /**
64 * @brief Create a DelegateResponse to wrap returned objects or exceptions
65 *
66 * @param data The type of data that the delegate has created to return wrapped in a shared pointer
67 */
68 DelegateResponse(std::shared_ptr<T> data) : mData(data), mDelegateResponseError(nullptr) {
69 if (!mData) {
70 throw std::invalid_argument("Created an empty DelegateResponse");
71 }
72 }
73
74 /**
75 * @brief Create a DelegateResponse to wrap returned objects or exceptions
76 *
77 * @param error The error that was encountered while processing the delegate
78 */
79 DelegateResponse(mip::Error& error) : mData(nullptr), mDelegateResponseError(new DelegateResponseError(error)) {
80 if (mDelegateResponseError->GetMessage().empty()) {
81 throw std::invalid_argument("Created an empty DelegateResponse");
82 }
83 }
84
85 /**
86 * @brief Create a DelegateResponse to wrap returned objects or exceptions
87 *
88 * @param exception If the delegate encountered an exception it will populate this field with a exception in a shared pointer
89 */
90 explicit DelegateResponse(const std::exception_ptr& exception)
91 : mData(nullptr),
92 mDelegateResponseError(exception ? new DelegateResponseError(exception) : nullptr) {
93 if (!mDelegateResponseError) {
94 throw std::invalid_argument("Created an empty DelegateResponse");
95 }
96 }
97
98 /**
99 * @brief Returns the requested data, nullptr if call triggered an exception.
100 *
101 * @return The data in a shared pointer of the templated type or nullptr.
102 */
103 std::shared_ptr<T> GetData() const { return mData; }
104
105 /**
106 * @brief Return any error generated during the call, nullptr if call completed successfully.
107 *
108 * @return Exception pointer due to any failure in the delegate call or nullptr.
109 */
110 std::shared_ptr<mip::DelegateResponseError> GetError() const {
111 return mDelegateResponseError;
112 }
113
114 /** @cond DOXYGEN_HIDE */
115 virtual ~DelegateResponse() {}
116protected:
117 DelegateResponse() {}
118 /** @endcond */
119
120private:
121 std::shared_ptr<T> mData;
122 std::shared_ptr<mip::DelegateResponseError> mDelegateResponseError;
123};
124
125MIP_NAMESPACE_END
126
127#endif // API_MIP_DELEGATE_RESPONSE
Delegate Response Error.
Definition error.h:315
A file containing the MIP SDK error types.
MIP namespace macros.