Commit | Line | Data |
---|---|---|
b3f060ed PP |
1 | /* |
2 | * Copyright (c) 2023 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: MIT | |
5 | */ | |
6 | ||
7 | #ifndef BABELTRACE_CPP_COMMON_BT2_RAW_VALUE_PROXY_HPP | |
8 | #define BABELTRACE_CPP_COMMON_BT2_RAW_VALUE_PROXY_HPP | |
9 | ||
10 | #include <string> | |
11 | ||
e7f0f07b SM |
12 | #include "cpp-common/bt2c/c-string-view.hpp" |
13 | ||
b3f060ed PP |
14 | namespace bt2 { |
15 | ||
16 | template <typename ObjT> | |
17 | class RawValueProxy | |
18 | { | |
19 | private: | |
20 | using _RawVal = typename ObjT::Value; | |
21 | ||
22 | public: | |
23 | explicit RawValueProxy(const ObjT obj) : _mObj {obj} | |
24 | { | |
25 | } | |
26 | ||
27 | RawValueProxy& operator=(const _RawVal& rawVal) | |
28 | { | |
29 | _mObj.value(rawVal); | |
30 | return *this; | |
31 | } | |
32 | ||
33 | operator _RawVal() const noexcept | |
34 | { | |
35 | return _mObj.value(); | |
36 | } | |
37 | ||
38 | private: | |
39 | ObjT _mObj; | |
40 | }; | |
41 | ||
42 | template <typename ObjT> | |
43 | class RawStringValueProxy final : public RawValueProxy<ObjT> | |
44 | { | |
45 | public: | |
46 | explicit RawStringValueProxy(const ObjT obj) : RawValueProxy<ObjT> {obj} | |
47 | { | |
48 | } | |
49 | ||
e7f0f07b SM |
50 | RawStringValueProxy& operator=(const char * const rawVal) |
51 | { | |
52 | RawValueProxy<ObjT>::operator=(bt2c::CStringView {rawVal}); | |
53 | return *this; | |
54 | } | |
55 | ||
b3f060ed PP |
56 | RawStringValueProxy& operator=(const std::string& rawVal) |
57 | { | |
e7f0f07b | 58 | RawValueProxy<ObjT>::operator=(bt2c::CStringView {rawVal.data()}); |
b3f060ed PP |
59 | return *this; |
60 | } | |
61 | }; | |
62 | ||
63 | } /* namespace bt2 */ | |
64 | ||
65 | #endif /* BABELTRACE_CPP_COMMON_BT2_RAW_VALUE_PROXY_HPP */ |