throw std::bad_alloc();
}
-Bundle::KeyInfo::KeyInfo(const bundle_keyval_t* handle, std::string name)
- : impl_(new Impl(this, handle, std::move(name))) {
+Bundle::KeyInfo::KeyInfo(const bundle_keyval_t* handle, std::string name,
+ bool own)
+ : impl_(new Impl(this, handle, std::move(name), own)) {
}
Bundle::KeyInfo::~KeyInfo() {
+ if (impl_ && impl_->handle_ && impl_->own_)
+ bundle_keyval_free(const_cast<bundle_keyval_t*>(impl_->handle_));
}
Bundle::KeyInfo::Impl::~Impl() = default;
+
Bundle::KeyInfo::Impl::Impl(Bundle::KeyInfo* parent,
const bundle_keyval_t* handle,
- std::string name)
- : handle_(handle), name_(name), parent_(parent) {
+ std::string name,
+ bool own)
+ : handle_(handle), name_(name), parent_(parent), own_(own) {
}
Bundle::KeyInfo::Impl::Impl(Bundle::KeyInfo* parent) : parent_(parent) {
Bundle::KeyInfo& Bundle::KeyInfo::operator = (const Bundle::KeyInfo& k) {
if (this != &k) {
+ if (impl_->handle_ && impl_->own_)
+ bundle_keyval_free(const_cast<bundle_keyval_t*>(impl_->handle_));
+
impl_->handle_ = bundle_keyval_dup(k.impl_->handle_);
impl_->name_ = k.impl_->name_;
if (impl_->handle_ == nullptr)
throw std::bad_alloc();
+
+ impl_->own_ = true;
}
return *this;
}
Bundle::KeyInfo::KeyInfo(Bundle::KeyInfo&& k) noexcept {
- impl_ = std::unique_ptr<Impl>(new Impl(this));
- impl_->handle_ = k.impl_->handle_;
- impl_->name_ = k.impl_->name_;
- k.impl_->handle_ = nullptr;
- k.impl_->name_ = "";
+ impl_ = std::move(k.impl_);
+ impl_->parent_ = this;
}
Bundle::KeyInfo& Bundle::KeyInfo::operator = (Bundle::KeyInfo&& k) noexcept {
if (this != &k) {
+ if (impl_->handle_ && impl_->own_)
+ bundle_keyval_free(const_cast<bundle_keyval_t*>(impl_->handle_));
+
impl_->handle_ = k.impl_->handle_;
impl_->name_ = k.impl_->name_;
+ impl_->own_ = k.impl_->own_;
k.impl_->handle_ = nullptr;
k.impl_->name_ = "";
+ k.impl_->own_ = false;
}
return *this;
}
impl_->handle_ = b.impl_->handle_;
b.impl_->handle_ = nullptr;
impl_->own_ = b.impl_->own_;
+ b.impl_->own_ = false;
impl_->copy_ = b.impl_->copy_;
+ b.impl_->copy_ = false;
}
return *this;
}
bundle_foreach(impl_->handle_, [](const char *key, const int type,
const bundle_keyval_t *kv, void *user_data) {
auto* v = static_cast<std::vector<KeyInfo>*>(user_data);
- v->emplace_back(kv, key);
+ v->emplace_back(kv, key, false);
}, &v);
return v;