forked from Video2P/Video2P
0
0
Fork 0

move deleted, scoped_ptr added

master
segfault 2025-01-13 13:13:41 +05:00
parent 919bd73544
commit 93df99e8a7
Signed by untrusted user: segfault
GPG Key ID: 8B1121D84DAB1BBF
5 changed files with 145 additions and 106 deletions

View File

@ -36,6 +36,4 @@ public:
static void free(void *ptr) throw();
};
#endif // VIDEO2P_MEMORY_HPP

View File

@ -1,57 +0,0 @@
/*
Video2P -- video hosting engine for I2P network.
Copyright (C) 2023 - 2025 Video2P Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef VIDEO2P_MOVE_HPP
#define VIDEO2P_MOVE_HPP
#define VIDEO2P_MOVE_REF(TYPE) ::V2PMove::Ref<TYPE>&
#define VIDEO2P_MOVE_IMPLEMENTATION(TYPE) \
public: \
inline operator ::V2PMove::Ref<TYPE>&() \
{ \
return *reinterpret_cast< ::V2PMove::Ref<TYPE>*>(this); \
} \
\
inline operator const ::V2PMove::Ref<TYPE>&() const \
{ \
return *reinterpret_cast<const ::V2PMove::Ref<TYPE>*>(this); \
} \
private:
class V2PMove {
public:
template<class T>
class Ref : public T {
private:
Ref();
~Ref() throw();
Ref(const Ref&);
void operator=(const Ref&);
};
template<class T>
static Ref<T>&
move(T &object)
{
return *reinterpret_cast<Ref<T>*>(&object);
}
};
#endif // VIDEO2P_MOVE_HPP

View File

@ -121,54 +121,30 @@ V2PMySQLResultSet::V2PMySQLResultSet(MYSQL_RES *result)
{
}
V2PMySQLResultSet::V2PMySQLResultSet(VIDEO2P_MOVE_REF(V2PMySQLResultSet) other)
throw()
: m_result(other.m_result)
, m_num_rows(other.m_num_rows)
, m_cur_row_index(other.m_cur_row_index)
{
other.m_result = NULL;
other.m_num_rows = 0;
other.m_cur_row_index = 0;
}
V2PMySQLResultSet&
V2PMySQLResultSet::operator=(VIDEO2P_MOVE_REF(V2PMySQLResultSet) other)
throw()
{
m_result = other.m_result;
m_num_rows = other.m_num_rows;
m_cur_row_index = other.m_cur_row_index;
other.m_result = NULL;
other.m_num_rows = 0;
other.m_cur_row_index = 0;
return *this;
}
V2PMySQLResultSet::~V2PMySQLResultSet()
throw()
{
mysql_free_result(m_result);
}
V2PMySQLResultRow
V2PMySQLResultRow*
V2PMySQLResultSet::getNextRow()
throw(V2PMySQLResultException)
{
if (m_cur_row_index++ < m_num_rows) {
return V2PMySQLResultRow(mysql_fetch_row(m_result),
return new V2PMySQLResultRow(mysql_fetch_row(m_result),
mysql_num_fields(m_result));
} else {
throw V2PMySQLResultException("ResultSet out of range");
}
}
V2PMySQLResultRow
V2PMySQLResultRow*
V2PMySQLResultSet::getRowByIndex(size_t index)
throw(V2PMySQLResultException)
{
if (index < m_num_rows) {
return V2PMySQLResultRow(mysql_fetch_row(m_result),
return new V2PMySQLResultRow(mysql_fetch_row(m_result),
mysql_num_fields(m_result));
} else {
throw V2PMySQLResultException("ResultSet out of range");
@ -266,7 +242,7 @@ V2PMySQLConnection::ping() const
return mysql_ping(m_connection);
}
VIDEO2P_MOVE_REF(V2PMySQLResultSet)
V2PMySQLResultSet*
V2PMySQLConnection::query(const char *sql)
throw(V2PMySQLQueryException, V2PMySQLResultException)
{
@ -279,6 +255,5 @@ V2PMySQLConnection::query(const char *sql)
throw V2PMySQLResultException(mysql_error(m_connection));
}
V2PMySQLResultSet rs(result);
return V2PMove::move(rs);
return new V2PMySQLResultSet(result);
}

View File

@ -21,7 +21,6 @@
#include "exception.hpp"
#include "string.hpp"
#include "move.hpp"
#include <mysql/mysql.h>
class V2PMySQLException : public V2PException {
@ -69,35 +68,37 @@ public:
// Valid only util valid V2PMySQLResultSet
class V2PMySQLResultRow {
public:
typedef unsigned index_t;
private:
MYSQL_ROW m_row;
unsigned m_num_columns;
index_t m_num_columns;
public:
V2PMySQLResultRow(MYSQL_ROW row, unsigned num_columns) throw();
private:
V2PMySQLResultRow(const V2PMySQLResultRow&);
V2PMySQLResultRow& operator=(const V2PMySQLResultRow&);
public:
size_t getSize() const throw();
const char *getColumn(size_t index) const throw();
};
class V2PMySQLResultSet {
VIDEO2P_MOVE_IMPLEMENTATION(V2PMySQLResultSet)
public:
typedef uint64_t index_t;
private:
MYSQL_RES *m_result;
unsigned m_num_rows;
unsigned m_cur_row_index;
index_t m_num_rows;
index_t m_cur_row_index;
public:
V2PMySQLResultSet(MYSQL_RES *result) throw();
private:
V2PMySQLResultSet(const V2PMySQLResultSet&);
V2PMySQLResultSet& operator=(const V2PMySQLResultSet&);
public:
V2PMySQLResultSet(VIDEO2P_MOVE_REF(V2PMySQLResultSet) other)
throw();
V2PMySQLResultSet& operator=(VIDEO2P_MOVE_REF(V2PMySQLResultSet) other)
throw();
~V2PMySQLResultSet() throw();
V2PMySQLResultRow getNextRow() throw(V2PMySQLResultException);
V2PMySQLResultRow getRowByIndex(size_t index)
V2PMySQLResultRow* getNextRow() throw(V2PMySQLResultException);
V2PMySQLResultRow* getRowByIndex(size_t index)
throw(V2PMySQLResultException);
size_t getSize() const throw();
size_t getCurrentIndex() const throw();
@ -140,10 +141,8 @@ public:
void reconnect()
throw(V2PMySQLInitException, V2PMySQLConnectionException);
int ping() const throw();
VIDEO2P_MOVE_REF(V2PMySQLResultSet) query(const char *sql)
V2PMySQLResultSet* query(const char *sql)
throw(V2PMySQLQueryException, V2PMySQLResultException);
friend class V2PMySQLResultSet;
};

124
src/scoped_ptr.hpp 100644
View File

@ -0,0 +1,124 @@
/*
Video2P -- video hosting engine for I2P network.
Copyright (C) 2023 - 2025 Video2P Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef VIDEO2P_SCOPED_PTR_HPP
#define VIDEO2P_SCOPED_PTR_HPP
#include <stddef.h>
template<class T>
class V2PScopedPtr {
private:
T *m_ptr;
public:
V2PScopedPtr(T *ptr)
throw()
: m_ptr(ptr)
{
}
private:
V2PScopedPtr(const V2PScopedPtr&);
V2PScopedPtr& operator=(const V2PScopedPtr&);
public:
~V2PScopedPtr()
throw()
{
delete m_ptr;
}
T*
moveFrom()
throw()
{
T *ret = m_ptr;
m_ptr = NULL;
return ret;
}
T*
get() const
throw()
{
return m_ptr;
}
T&
operator*() const
throw()
{
return *m_ptr;
}
T*
operator->() const
throw()
{
return m_ptr;
}
};
template<class T>
class V2PScopedPtr<T[]> {
private:
T *m_ptr;
public:
V2PScopedPtr(T *ptr)
: m_ptr(ptr)
{
}
private:
V2PScopedPtr(const V2PScopedPtr&);
V2PScopedPtr& operator=(const V2PScopedPtr&);
public:
~V2PScopedPtr()
{
delete[] m_ptr;
}
T*
moveFrom()
{
T *ret = m_ptr;
m_ptr = NULL;
return ret;
}
T*
get() const
throw()
{
return m_ptr;
}
T&
operator*() const
throw()
{
return *m_ptr;
}
T&
operator[](size_t index) const
throw()
{
return m_ptr[index];
}
};
#endif // VIDEO2P_SCOPED_PTR_HPP