Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / description_buffer.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6
7 #include <ostream>
8 #include <memory>
9 #include "ie_common.h"
10 #include <string>
11
12 namespace InferenceEngine {
13 struct DescriptionBuffer : public std::basic_streambuf<char, std::char_traits<char> > {
14     std::unique_ptr<std::ostream> stream;
15     StatusCode err = GENERAL_ERROR;
16
17     DescriptionBuffer(StatusCode err, ResponseDesc *desc)
18             : err(err) {
19         init(desc);
20     }
21
22     explicit DescriptionBuffer(StatusCode err)
23         : err(err) {
24     }
25
26     explicit DescriptionBuffer(ResponseDesc *desc) {
27         init(desc);
28     }
29
30     DescriptionBuffer(char *pBuffer, size_t len) {
31         init(pBuffer, len);
32     }
33
34     DescriptionBuffer(StatusCode err, char *pBuffer, size_t len)
35             : err(err) {
36         init(pBuffer, len);
37     }
38
39
40     template<class T>
41     DescriptionBuffer &operator<<(const T &obj) {
42         if (!stream)
43             return *this;
44         (*stream.get()) << obj;
45
46         return *this;
47     }
48
49     operator StatusCode() const {
50         if (stream)
51             stream->flush();
52         return err;
53     }
54
55 private:
56     void init(ResponseDesc *desc) {
57         if (desc == nullptr)
58             return;
59         init(desc->msg, sizeof(desc->msg) / sizeof(desc->msg[0]));
60     }
61
62     void init(char *ptr, size_t len) {
63         if (nullptr != ptr && len > 0) {
64             // set the "put" pointer the start of the buffer and record it's length.
65             setp(ptr, ptr + len - 1);
66         }
67         stream.reset(new std::ostream(this));
68
69         if (nullptr != ptr && len > 0) {
70             ptr[len - 1] = 0;
71             (*stream.get()) << ptr;
72         }
73     }
74 };
75 }  // namespace InferenceEngine