2020-02-13 16:27:06 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 Cppcheck team.
|
2020-02-13 16:27:06 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#ifndef valueptrH
|
|
|
|
#define valueptrH
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "config.h"
|
2023-03-07 12:24:01 +01:00
|
|
|
|
2020-02-13 16:27:06 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2021-08-07 20:51:18 +02:00
|
|
|
template<class T>
|
2020-02-13 16:27:06 +01:00
|
|
|
class CPPCHECKLIB ValuePtr {
|
2021-08-07 20:51:18 +02:00
|
|
|
template<class U>
|
2020-02-13 16:27:06 +01:00
|
|
|
struct cloner {
|
2020-02-13 17:04:05 +01:00
|
|
|
static T* apply(const T* x) {
|
|
|
|
return new U(*static_cast<const U*>(x));
|
|
|
|
}
|
2020-02-13 16:27:06 +01:00
|
|
|
};
|
|
|
|
|
2020-02-13 17:04:05 +01:00
|
|
|
public:
|
2020-02-13 16:27:06 +01:00
|
|
|
using pointer = T*;
|
|
|
|
using element_type = T;
|
|
|
|
using cloner_type = decltype(&cloner<T>::apply);
|
|
|
|
|
|
|
|
ValuePtr() : mPtr(nullptr), mClone() {}
|
|
|
|
|
2021-08-07 20:51:18 +02:00
|
|
|
template<class U>
|
2020-02-13 16:27:06 +01:00
|
|
|
// cppcheck-suppress noExplicitConstructor
|
2022-10-16 13:51:17 +02:00
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
2020-02-13 16:27:06 +01:00
|
|
|
ValuePtr(const U& value) : mPtr(cloner<U>::apply(&value)), mClone(&cloner<U>::apply)
|
|
|
|
{}
|
|
|
|
|
2020-02-13 17:04:05 +01:00
|
|
|
ValuePtr(const ValuePtr& rhs) : mPtr(nullptr), mClone(rhs.mClone) {
|
2020-02-13 16:27:06 +01:00
|
|
|
if (rhs) {
|
|
|
|
mPtr.reset(mClone(rhs.get()));
|
|
|
|
}
|
|
|
|
}
|
2023-03-07 12:24:01 +01:00
|
|
|
ValuePtr(ValuePtr&& rhs) NOEXCEPT : mPtr(std::move(rhs.mPtr)), mClone(std::move(rhs.mClone)) {}
|
2020-02-13 16:27:06 +01:00
|
|
|
|
2022-09-24 13:25:06 +02:00
|
|
|
/**
|
|
|
|
* Releases the shared_ptr's ownership of the managed object using the .reset() function
|
|
|
|
*/
|
|
|
|
void release() {
|
|
|
|
mPtr.reset();
|
2020-02-13 17:04:05 +01:00
|
|
|
}
|
2020-02-13 16:27:06 +01:00
|
|
|
|
2021-08-07 20:51:18 +02:00
|
|
|
T* get() NOEXCEPT {
|
|
|
|
return mPtr.get();
|
|
|
|
}
|
2020-02-13 17:04:05 +01:00
|
|
|
const T* get() const NOEXCEPT {
|
|
|
|
return mPtr.get();
|
|
|
|
}
|
2020-02-13 16:27:06 +01:00
|
|
|
|
2020-02-13 17:04:05 +01:00
|
|
|
T& operator*() {
|
|
|
|
return *get();
|
|
|
|
}
|
|
|
|
const T& operator*() const {
|
|
|
|
return *get();
|
|
|
|
}
|
2020-02-13 16:27:06 +01:00
|
|
|
|
2021-08-07 20:51:18 +02:00
|
|
|
T* operator->() NOEXCEPT {
|
|
|
|
return get();
|
|
|
|
}
|
2020-02-13 17:04:05 +01:00
|
|
|
const T* operator->() const NOEXCEPT {
|
|
|
|
return get();
|
|
|
|
}
|
2020-02-13 16:27:06 +01:00
|
|
|
|
2020-02-13 17:04:05 +01:00
|
|
|
void swap(ValuePtr& rhs) {
|
2020-02-13 16:27:06 +01:00
|
|
|
using std::swap;
|
|
|
|
swap(mPtr, rhs.mPtr);
|
|
|
|
swap(mClone, rhs.mClone);
|
|
|
|
}
|
|
|
|
|
2020-02-13 17:04:05 +01:00
|
|
|
ValuePtr<T>& operator=(ValuePtr rhs) {
|
2020-02-13 16:27:06 +01:00
|
|
|
swap(rhs);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-10-16 13:51:17 +02:00
|
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
2020-02-13 17:04:05 +01:00
|
|
|
operator bool() const NOEXCEPT {
|
|
|
|
return !!mPtr;
|
|
|
|
}
|
2020-02-13 16:27:06 +01:00
|
|
|
|
2020-02-13 17:04:05 +01:00
|
|
|
private:
|
2020-02-13 16:27:06 +01:00
|
|
|
std::shared_ptr<T> mPtr;
|
|
|
|
cloner_type mClone;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|