GCC Code Coverage Report


Directory: ./
File: src/msrproto/expat_wrapper.h
Date: 2024-06-11 13:26:06
Exec Total Coverage
Lines: 8 8 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 /*****************************************************************************
2 *
3 * Copyright (C) 2021 Bjarne von Horn (vh at igh dot de)
4 *
5 * This file is part of the PdCom library.
6 *
7 * The PdCom library is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * The PdCom library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with the PdCom library. If not, see <http://www.gnu.org/licenses/>.
19 *
20 *****************************************************************************/
21
22 #ifndef MSRPROTO_EXPAT_WRAPPER_H
23 #define MSRPROTO_EXPAT_WRAPPER_H
24
25 #include <exception>
26 #include <functional>
27 #include <memory>
28
29 extern "C" {
30 struct XML_ParserStruct;
31 }
32
33 namespace PdCom { namespace impl { namespace MsrProto {
34 struct XmlParserDeleter
35 {
36 void operator()(XML_ParserStruct *) noexcept;
37 };
38
39 template <
40 typename Handler,
41 bool useExpatBuffer,
42 bool setReparseDeferralEnabled = true>
43 128 class ExpatWrapper
44 {
45 public:
46 ExpatWrapper(const char *encoding = "UTF-8");
47
48 protected:
49 template <std::size_t Size>
50 class ExpatBuffer
51 {
52 void *buffer_;
53
54 public:
55 explicit ExpatBuffer(ExpatWrapper &);
56 ExpatBuffer(const ExpatBuffer &) = delete;
57 425 ExpatBuffer(ExpatBuffer &&other) noexcept : buffer_(other.buffer_)
58 {
59 425 other.buffer_ = nullptr;
60 425 }
61
62 ExpatBuffer &operator=(const ExpatBuffer &) = delete;
63 ExpatBuffer &operator=(ExpatBuffer &&other) noexcept
64 {
65 ExpatBuffer copy(std::move(other));
66 std::swap(other.buffer_, buffer_);
67 return *this;
68 }
69
70
71 template <typename T = void>
72 436 T *fill() &
73 {
74 436 return static_cast<T *>(buffer_);
75 }
76
77 369 std::size_t size() const { return Size; }
78 };
79
80 template <typename T>
81 typename std::enable_if<
82 !useExpatBuffer && std::is_same<T, char>::value,
83 bool>::type
84 parse(const T *s, std::size_t n, bool final = false);
85 template <size_t buffersize>
86 typename std::enable_if<(useExpatBuffer && buffersize > 0), bool>::type
87 parse(ExpatBuffer<buffersize> buffer, std::size_t n, bool final = false);
88
89 void reset(const char *encoding = "UTF-8");
90 867 int level() const noexcept { return level_; }
91
92 private:
93 struct CallbackWrapper;
94
95 std::unique_ptr<XML_ParserStruct, XmlParserDeleter> parser_;
96 std::exception_ptr pending_exception_ = nullptr;
97 int level_ = 0;
98 };
99
100
101 }}} // namespace PdCom::impl::MsrProto
102
103
104 #endif // MSRPROTO_EXPAT_WRAPPER_H
105