forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdescription_buffer.hpp
122 lines (105 loc) · 3.02 KB
/
description_buffer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
/**
* @brief Defines Description buffer to conviniently works with StatusCode and ResponseDesc
* @file description_buffer.hpp
*/
#pragma once
#include <memory>
#include <ostream>
#include <string>
#include "ie_common.h"
namespace InferenceEngine {
/**
* @brief A description buffer wrapping StatusCode and ResponseDesc
* @ingroup ie_dev_api_error_debug
*/
struct DescriptionBuffer : public std::basic_streambuf<char, std::char_traits<char>> {
/**
* @brief Creeates a description buffer with parameters
*
* @param[in] err The error code
* @param desc The response desc to write an error message to
*/
DescriptionBuffer(StatusCode err, ResponseDesc* desc) : err(err) {
init(desc);
}
/**
* @brief Constructs with StatusCode
*
* @param[in] err The StatusCode value
*/
explicit DescriptionBuffer(StatusCode err) : err(err) {}
/**
* @brief Constructs with ResponseDesc
*
* @param desc The ResponseDesc pointer
*/
explicit DescriptionBuffer(ResponseDesc* desc) {
init(desc);
}
/**
* @brief Constructs with parameters
*
* @param pBuffer The buffer to wrtie to.
* @param[in] len The length of `pBuffer`
*/
DescriptionBuffer(char* pBuffer, size_t len) {
init(pBuffer, len);
}
/**
* @brief Constructs with parameters
*
* @param[in] err The StatusCode value
* @param pBuffer The buffer to wrtie to.
* @param[in] len The length of `pBuffer`
*/
DescriptionBuffer(StatusCode err, char* pBuffer, size_t len) : err(err) {
init(pBuffer, len);
}
/**
* @brief Writes to ResponseDesc stream
*
* @param[in] obj The object to write to stream
* @tparam T An object type
*
* @return A reference to itself
*/
template <class T>
DescriptionBuffer& operator<<(const T& obj) {
if (!stream)
return *this;
(*stream.get()) << obj;
return *this;
}
/**
* @brief Converts to StatusCode
* @return A StatusCode value
*/
operator StatusCode() const {
if (stream)
stream->flush();
return err;
}
private:
std::unique_ptr<std::ostream> stream;
StatusCode err = GENERAL_ERROR;
void init(ResponseDesc* desc) {
if (desc == nullptr)
return;
init(desc->msg, sizeof(desc->msg) / sizeof(desc->msg[0]));
}
void init(char* ptr, size_t len) {
if (nullptr != ptr && len > 0) {
// set the "put" pointer the start of the buffer and record it's length.
setp(ptr, ptr + len - 1);
}
stream.reset(new std::ostream(this));
if (nullptr != ptr && len > 0) {
ptr[len - 1] = 0;
(*stream.get()) << ptr;
}
}
};
} // namespace InferenceEngine