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