2019-12-09 11:57:37 +00:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-12-09 11:57:37 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <vapours.hpp>
|
|
|
|
|
|
|
|
namespace ams::kvdb {
|
|
|
|
|
|
|
|
class AutoBuffer {
|
|
|
|
NON_COPYABLE(AutoBuffer);
|
|
|
|
private:
|
2021-10-10 08:14:06 +01:00
|
|
|
u8 *m_buffer;
|
|
|
|
size_t m_size;
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
2021-10-10 08:14:06 +01:00
|
|
|
AutoBuffer() : m_buffer(nullptr), m_size(0) { /* ... */ }
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
~AutoBuffer() {
|
|
|
|
this->Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoBuffer(AutoBuffer &&rhs) {
|
2021-10-10 08:14:06 +01:00
|
|
|
m_buffer = rhs.m_buffer;
|
|
|
|
m_size = rhs.m_size;
|
|
|
|
rhs.m_buffer = nullptr;
|
|
|
|
rhs.m_size = 0;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 06:40:46 +01:00
|
|
|
AutoBuffer &operator=(AutoBuffer &&rhs) {
|
2020-03-08 08:06:23 +00:00
|
|
|
AutoBuffer(std::move(rhs)).Swap(*this);
|
2019-12-09 11:57:37 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Swap(AutoBuffer &rhs) {
|
2021-10-10 08:14:06 +01:00
|
|
|
std::swap(m_buffer, rhs.m_buffer);
|
|
|
|
std::swap(m_size, rhs.m_size);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Reset() {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (m_buffer != nullptr) {
|
|
|
|
delete[] m_buffer;
|
|
|
|
m_buffer = nullptr;
|
|
|
|
m_size = 0;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 *Get() const {
|
2021-10-10 08:14:06 +01:00
|
|
|
return m_buffer;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetSize() const {
|
2021-10-10 08:14:06 +01:00
|
|
|
return m_size;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result Initialize(size_t size) {
|
|
|
|
/* Check that we're not already initialized. */
|
2021-10-10 08:14:06 +01:00
|
|
|
AMS_ABORT_UNLESS(m_buffer == nullptr);
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
/* Allocate a buffer. */
|
2021-10-10 08:14:06 +01:00
|
|
|
m_buffer = new (std::nothrow) u8[size];
|
|
|
|
R_UNLESS(m_buffer != nullptr, kvdb::ResultAllocationFailed());
|
2020-03-08 08:06:23 +00:00
|
|
|
|
2021-10-10 08:14:06 +01:00
|
|
|
m_size = size;
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result Initialize(const void *buf, size_t size) {
|
|
|
|
/* Create a new buffer of the right size. */
|
|
|
|
R_TRY(this->Initialize(size));
|
|
|
|
|
|
|
|
/* Copy the input data in. */
|
2021-10-10 08:14:06 +01:00
|
|
|
std::memcpy(m_buffer, buf, size);
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|