| 1 |
#ifndef PROTON_INTERNAL_DATA_HPP |
| 2 |
#define PROTON_INTERNAL_DATA_HPP |
| 3 |
|
| 4 |
/* |
| 5 |
* |
| 6 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 7 |
* or more contributor license agreements. See the NOTICE file |
| 8 |
* distributed with this work for additional information |
| 9 |
* regarding copyright ownership. The ASF licenses this file |
| 10 |
* to you under the Apache License, Version 2.0 (the |
| 11 |
* "License"); you may not use this file except in compliance |
| 12 |
* with the License. You may obtain a copy of the License at |
| 13 |
* |
| 14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 15 |
* |
| 16 |
* Unless required by applicable law or agreed to in writing, |
| 17 |
* software distributed under the License is distributed on an |
| 18 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 19 |
* KIND, either express or implied. See the License for the |
| 20 |
* specific language governing permissions and limitations |
| 21 |
* under the License. |
| 22 |
* |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "../internal/object.hpp" |
| 26 |
#include "../types_fwd.hpp" |
| 27 |
|
| 28 |
struct pn_data_t; |
| 29 |
|
| 30 |
namespace proton { |
| 31 |
|
| 32 |
class value; |
| 33 |
|
| 34 |
namespace internal { |
| 35 |
|
| 36 |
/// @cond INTERNAL |
| 37 |
/// A wrapper for a proton data object. |
| 38 |
class data : public object<pn_data_t> { |
| 39 |
/// Wrap an existing Proton-C data object. |
| 40 |
data(pn_data_t* d) : internal::object<pn_data_t>(d) {} |
| 41 |
|
| 42 |
public: |
| 43 |
/// Create an empty data object. |
| 44 |
data() : internal::object<pn_data_t>(0) {} |
| 45 |
|
| 46 |
/// Create a new data object. |
| 47 |
PN_CPP_EXTERN static data create(); |
| 48 |
|
| 49 |
/// Copy the contents of another data object. |
| 50 |
PN_CPP_EXTERN void copy(const data&); |
| 51 |
|
| 52 |
/// Clear the data. |
| 53 |
PN_CPP_EXTERN void clear(); |
| 54 |
|
| 55 |
/// Rewind current position to the start. |
| 56 |
PN_CPP_EXTERN void rewind(); |
| 57 |
|
| 58 |
/// True if there are no values. |
| 59 |
PN_CPP_EXTERN bool empty() const; |
| 60 |
|
| 61 |
/// Append the contents of another data object. |
| 62 |
PN_CPP_EXTERN int append(data src); |
| 63 |
|
| 64 |
/// Append up to limit items from data object. |
| 65 |
PN_CPP_EXTERN int appendn(data src, int limit); |
| 66 |
|
| 67 |
PN_CPP_EXTERN bool next(); |
| 68 |
PN_CPP_EXTERN const void* point() const; |
| 69 |
PN_CPP_EXTERN void restore(const void* h); |
| 70 |
|
| 71 |
protected: |
| 72 |
void narrow(); |
| 73 |
void widen(); |
| 74 |
|
| 75 |
friend class internal::factory<data>; |
| 76 |
friend struct state_guard; |
| 77 |
friend PN_CPP_EXTERN std::ostream& operator<<(std::ostream&, const data&); |
| 78 |
}; |
| 79 |
/// @endcond |
| 80 |
|
| 81 |
/// **Unsettled API** - Save and restore codec state. |
| 82 |
/// |
| 83 |
/// A state guard saves the state and restores it in the destructor |
| 84 |
/// unless `cancel()` is called. |
| 85 |
struct state_guard { |
| 86 |
/// @cond INTERNAL |
| 87 |
data& data_; |
| 88 |
const void* point_; |
| 89 |
bool cancel_; |
| 90 |
/// @endcond |
| 91 |
|
| 92 |
/// @cond INTERNAL |
| 93 |
state_guard(data& d) : data_(d), point_(data_.point()), cancel_(false) {} |
| 94 |
/// @endcond |
| 95 |
|
| 96 |
~state_guard() { if (!cancel_) data_.restore(point_); } |
| 97 |
|
| 98 |
/// Discard the saved state. |
| 99 |
void cancel() { cancel_ = true; } |
| 100 |
}; |
| 101 |
|
| 102 |
} // internal |
| 103 |
} // proton |
| 104 |
|
| 105 |
#endif // PROTON_INTERNAL_DATA_HPP |