Skip to content

Commit

Permalink
Refactor user pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
hexagonrecursion committed Feb 2, 2025
1 parent 9fdb5b1 commit ba04b1f
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 118 deletions.
3 changes: 2 additions & 1 deletion CBot/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ target_sources(CBot PRIVATE
src/CBot/CBotVar/CBotVarString.h
src/CBot/context/cbot_context.cpp
src/CBot/context/cbot_context.h
src/CBot/context/cbot_user_pointer.h
src/CBot/context/context_observer.h
src/CBot/context/context_owner.h
src/CBot/context/external_call_list_interface.h
Expand All @@ -181,6 +180,8 @@ target_sources(CBot PRIVATE
src/CBot/stdlib/StringFunctions.cpp
src/CBot/stdlib/stdlib.h
src/CBot/stdlib/stdlib_public.h
src/CBot/user_pointer.cpp
src/CBot/user_pointer.h
)

target_include_directories(CBot PUBLIC src)
Expand Down
11 changes: 3 additions & 8 deletions CBot/src/CBot/CBotInstr/CBotFieldExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

#include "CBot/CBotVar/CBotVarClass.h"

#include "CBot/context/cbot_user_pointer.h"

#include <cassert>
#include <sstream>

Expand Down Expand Up @@ -88,13 +86,10 @@ bool CBotFieldExpr::ExecuteVar(CBotVar* &pVar, CBotStack* &pile, CBotToken* prev
return pj->Return(pile);
}

if (const auto& userPtr = pItem->GetUserPointer())
if (pItem->GetUserPointer().GetState() == PtrState::Dead)
{
if (userPtr->GetPointerAs<void>() == nullptr)
{
pile->SetError(CBotErrDeletedPtr, prevToken);
return pj->Return(pile);
}
pile->SetError(CBotErrDeletedPtr, prevToken);
return pj->Return(pile);
}
}

Expand Down
1 change: 0 additions & 1 deletion CBot/src/CBot/CBotStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include "CBot/CBotProgram.h"

#include "CBot/context/cbot_context.h"
#include "CBot/context/cbot_user_pointer.h"

#include <cassert>
#include <cstdint>
Expand Down
13 changes: 7 additions & 6 deletions CBot/src/CBot/CBotVar/CBotVar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@

#include "CBot/CBotEnums.h"

#include "CBot/context/cbot_user_pointer.h"

#include <cassert>
#include <cmath>
#include <cstdio>
Expand Down Expand Up @@ -98,14 +96,17 @@ void CBotVar::Update()
{
}

void CBotVar::SetUserPointer(std::unique_ptr<CBotUserPointer> user)
void CBotVar::SetUserPointer(void* user)
{
}

void CBotVar::KillUserPointer()
{
}

const std::unique_ptr<CBotUserPointer>& CBotVar::GetUserPointer()
VarUserPointer CBotVar::GetUserPointer()
{
const static std::unique_ptr<CBotUserPointer> emptyPtr{nullptr};
return emptyPtr;
return {};
}

void CBotVar::SetUniqNum(long n)
Expand Down
14 changes: 9 additions & 5 deletions CBot/src/CBot/CBotVar/CBotVar.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "CBot/CBotTypResult.h"
#include "CBot/CBotEnums.h"
#include "CBot/CBotUtils.h"
#include "CBot/user_pointer.h"

#include <cstdint>
#include <memory>
Expand All @@ -35,7 +36,6 @@ class CBotClass;
class CBotContext;
class CBotInstr;
class CBotToken;
class CBotUserPointer;
class CBotVar;
class CBotVarClass;

Expand Down Expand Up @@ -149,13 +149,17 @@ class CBotVar : public CBotLinkedList<CBotVar>
* \brief Set a custom pointer associated with this variable
* \param user custom pointer to set
*/
virtual void SetUserPointer(std::unique_ptr<CBotUserPointer> user);
virtual void SetUserPointer(void* user);

/**
* \brief Returns the custom pointer associated with this variable
* \return A pointer set with SetUserPointer()
* \brief Set user pointer as dead
*/
virtual const std::unique_ptr<CBotUserPointer>& GetUserPointer();
virtual void KillUserPointer();

/**
* \brief Return the custom pointer associated with this variable
*/
virtual VarUserPointer GetUserPointer();

//@}

Expand Down
24 changes: 11 additions & 13 deletions CBot/src/CBot/CBotVar/CBotVarClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "CBot/CBotInstr/CBotInstr.h"

#include "CBot/context/cbot_context.h"
#include "CBot/context/cbot_user_pointer.h"

#include <cassert>

Expand Down Expand Up @@ -195,24 +194,23 @@ CBotClass* CBotVarClass::GetClass()

void CBotVarClass::Update()
{
if (!m_userPtr) return;

void* user = m_userPtr->GetPointerAs<void>();

if (user == nullptr) return;

if (m_userPtr.GetState() != PtrState::Alive) return;
void* user = m_userPtr.Get();
m_pClass->Update(this, user);
}

void CBotVarClass::SetUserPointer(std::unique_ptr<CBotUserPointer> user)
void CBotVarClass::SetUserPointer(void* user)
{
if (m_userPtr)
assert(false);
else
m_userPtr = std::move(user);
assert(m_userPtr.GetState() == PtrState::UnInit);
m_userPtr.Set(user);
}

void CBotVarClass::KillUserPointer()
{
m_userPtr.Kill();
}

const std::unique_ptr<CBotUserPointer>& CBotVarClass::GetUserPointer()
VarUserPointer CBotVarClass::GetUserPointer()
{
return m_userPtr;
}
Expand Down
7 changes: 4 additions & 3 deletions CBot/src/CBot/CBotVar/CBotVarClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ class CBotVarClass : public CBotVar
bool Save1State(std::ostream &ostr, CBotContext& context) override;

void Update() override;
void SetUserPointer(std::unique_ptr<CBotUserPointer> user) override;
const std::unique_ptr<CBotUserPointer>& GetUserPointer() override;
void SetUserPointer(void* user) override;
void KillUserPointer() override;
VarUserPointer GetUserPointer() override;

CBotVarSPtr GetPointer() override;

Expand All @@ -78,7 +79,7 @@ class CBotVarClass : public CBotVar
//! Set after constructor is called, allows destructor to be called
bool m_bConstructor;

std::unique_ptr<CBotUserPointer> m_userPtr;
VarUserPointer m_userPtr;

friend class CBotVar;
friend class CBotVarPointer;
Expand Down
51 changes: 12 additions & 39 deletions CBot/src/CBot/CBotVar/CBotVarPointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#include "CBot/CBotEnums.h"
#include "CBot/CBotUtils.h"

#include "CBot/context/cbot_user_pointer.h"

#include <cassert>

namespace CBot
Expand Down Expand Up @@ -63,15 +61,20 @@ void CBotVarPointer::Update()
if (m_pVarClass) m_pVarClass->Update();
}

void CBotVarPointer::SetUserPointer(std::unique_ptr<CBotUserPointer> user)
void CBotVarPointer::SetUserPointer(void* user)
{
if (m_pVarClass) m_pVarClass->SetUserPointer(user);
}

void CBotVarPointer::KillUserPointer()
{
if (m_pVarClass) m_pVarClass->SetUserPointer(std::move(user));
if (m_pVarClass) m_pVarClass->KillUserPointer();
}

const std::unique_ptr<CBotUserPointer>& CBotVarPointer::GetUserPointer()
VarUserPointer CBotVarPointer::GetUserPointer()
{
if (m_pVarClass) return m_pVarClass->GetUserPointer();
return CBotVar::GetUserPointer();
return {};
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -208,45 +211,15 @@ bool CBotVarPointer::Eq(CBotVar* left, CBotVar* right)
auto r = right->GetPointer();

if ( l == r ) return true;
if ( l == nullptr )
{
if (const auto& rUser = r->GetUserPointer())
{
if (rUser->GetPointerAs<void>() == nullptr) return true;
}
}
if ( r == nullptr )
{
if (const auto& lUser = l->GetUserPointer())
{
if (lUser->GetPointerAs<void>() == nullptr) return true;
}
}
if ( l == nullptr && r->GetUserPointer().GetState() == PtrState::Dead) return true;
if ( r == nullptr && l->GetUserPointer().GetState() == PtrState::Dead) return true;
return false;
}

////////////////////////////////////////////////////////////////////////////////
bool CBotVarPointer::Ne(CBotVar* left, CBotVar* right)
{
auto l = left->GetPointer();
auto r = right->GetPointer();

if ( l == r ) return false;
if ( l == nullptr )
{
if (const auto& rUser = r->GetUserPointer())
{
if (rUser->GetPointerAs<void>() == nullptr) return false;
}
}
if ( r == nullptr )
{
if (const auto& lUser = l->GetUserPointer())
{
if (lUser->GetPointerAs<void>() == nullptr) return false;
}
}
return true;
return !Eq(left, right);
}

} // namespace CBot
5 changes: 3 additions & 2 deletions CBot/src/CBot/CBotVar/CBotVarPointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ class CBotVarPointer : public CBotVar
bool Save1State(std::ostream &ostr, CBotContext& context) override;

void Update() override;
void SetUserPointer(std::unique_ptr<CBotUserPointer> user) override;
const std::unique_ptr<CBotUserPointer>& GetUserPointer() override;
void SetUserPointer(void* user) override;
void KillUserPointer() override;
VarUserPointer GetUserPointer() override;

bool Eq(CBotVar* left, CBotVar* right) override;
bool Ne(CBotVar* left, CBotVar* right) override;
Expand Down
2 changes: 0 additions & 2 deletions CBot/src/CBot/stdlib/stdlib_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

#include "CBot/stdlib/Compilation.h"

#include "CBot/context/cbot_user_pointer.h"

#include <filesystem>
#include <memory>
#include <unordered_map>
Expand Down
49 changes: 49 additions & 0 deletions CBot/src/CBot/user_pointer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2025, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
*
* 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://gnu.org/licenses
*/

#include <cassert>
#include "CBot/user_pointer.h"

namespace CBot
{

PtrState VarUserPointer::GetState() const
{
return m_state;
}

void* VarUserPointer::Get() const
{
return m_p;
}

void VarUserPointer::Set(void* p)
{
assert(p != nullptr);
m_p = p;
m_state = PtrState::Alive;
}

void VarUserPointer::Kill()
{
m_p = nullptr;
m_state = PtrState::Dead;
}

} // namespace CBot
Loading

0 comments on commit ba04b1f

Please sign in to comment.