Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Core/Libraries/Source/WWVegas/WWLib/WWCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#pragma once

#include "ref_ptr.h"
#include "refcount.h"
#include "STLUtils.h"
#include "stringex.h"
Expand Down
92 changes: 67 additions & 25 deletions Core/Libraries/Source/WWVegas/WWLib/ref_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@
copying.

To create a RefCountPtr<T> from a raw pointer, use the global template functions
Create_NoAddRef should be used when wrapping a pointer that has just been created with NEW
Create_NoAddRef should be used when wrapping a pointer that has been returned from a "Get" function
Create_No_Add_Ref should be used when wrapping a pointer that has just been created with NEW
Create_No_Add_Ref should be used when wrapping a pointer that has been returned from a "Get" function
(the function added a reference prior to returning the pointer)
Create_AddRef should be used when wrapping a pointer that has been returned from a "Peek" function
Create_Add_Ref should be used when wrapping a pointer that has been returned from a "Peek" function
(the function did not add a reference prior to returning the pointer).

Create_NoAddRef and Create_AddRef are provided to allow old code to migrate from manual reference count
Create_No_Add_Ref and Create_Add_Ref are provided to allow old code to migrate from manual reference count
management to RefCountPtr. New code written with RefCountPtr should rarely if ever use
Create_NoAddRef and Create_AddRef.
Create_No_Add_Ref and Create_Add_Ref.

If it is absolutely necessary to extract the raw pointer, use Peek. Peek does not add a new
reference to the object. Using a Peek'd object after its RefCountPtr has gone out of scope requires
Expand Down Expand Up @@ -217,21 +217,23 @@ class RefCountPtr

// Creates a RefCountPtr<T> and does not increment the reference counter of the passed object.
// Is generally used for objects returned by operator new and "Get" functions.
static RefCountPtr<T> Create_NoAddRef(T *t)
// Prefer using Assign_No_Add_Ref.
static RefCountPtr<T> Create_No_Add_Ref(T *t)
{
WWASSERT(t == nullptr || t->Num_Refs() >= 1);
return RefCountPtr<T>(t, RefCountPtr<T>::GET);
}

// Creates a RefCountPtr<T> and increments the reference counter of the passed object.
// Is generally used for objects returned by "Peek" functions.
static RefCountPtr<T> Create_AddRef(T *t)
// Prefer using Assign_Add_Ref.
static RefCountPtr<T> Create_Add_Ref(T *t)
{
return RefCountPtr<T>(t, RefCountPtr<T>::PEEK);
}

RefCountPtr()
: Referent(0)
: Referent(nullptr)
{
}

Expand All @@ -247,9 +249,9 @@ class RefCountPtr
// This allows construction of the smart pointer from 0 (null)
// Without allows unwanted conversions from T * (and related types, including void *)
RefCountPtr(DummyPtrType * dummy)
: Referent(0)
: Referent(nullptr)
{
WWASSERT(dummy == 0);
WWASSERT(dummy == nullptr);
}
#endif

Expand Down Expand Up @@ -292,12 +294,41 @@ class RefCountPtr
Referent->Release_Ref();
}

Referent = 0;
Referent = nullptr;

return *this;
}
#endif


// Assigns a pointer T and does not increment the reference counter of the passed object.
// Is generally used for objects returned by operator new and "Get" functions.
void Assign_No_Add_Ref(T *t)
{
WWASSERT(t == nullptr || t->Num_Refs() >= 1);

if (Referent) {
Referent->Release_Ref();
}

Referent = t;
}

// Assigns a pointer T and increments the reference counter of the passed object.
// Is generally used for objects returned by "Peek" functions.
void Assign_Add_Ref(T *t)
{
if (t != nullptr) {
t->Add_Ref();
}

if (Referent) {
Referent->Release_Ref();
}

Referent = t;
}

template <class RHS>
const RefCountPtr<T> & operator =(const RefCountPtr<RHS> & rhs)
{
Expand All @@ -320,7 +351,6 @@ class RefCountPtr
rhs.Referent->Add_Ref();
}


if (Referent) {
Referent->Release_Ref();
}
Expand All @@ -333,7 +363,7 @@ class RefCountPtr
{
if (Referent) {
Referent->Release_Ref();
Referent = 0;
Referent = nullptr;
}
}

Expand All @@ -353,7 +383,7 @@ class RefCountPtr
{
if (Referent) {
Referent->Release_Ref();
Referent = 0;
Referent = nullptr;
}
}

Expand All @@ -380,7 +410,7 @@ class RefCountPtr
T * Release()
{
T * p = Referent;
Referent = 0;
Referent = nullptr;
return p;
}

Expand All @@ -390,7 +420,7 @@ class RefCountPtr
RefCountPtr(T * referent, ReferenceHandling reference_handling)
: Referent(referent)
{
if (reference_handling == PEEK && 0 != referent) {
if (reference_handling == PEEK && nullptr != referent) {
referent->Add_Ref();
}
}
Expand All @@ -412,34 +442,46 @@ bool operator <(const RefCountPtr<LHS> & lhs, const RefCountPtr<RHS> & rhs)
return lhs.Peek() < rhs.Peek();
}

// This comparison allows us to test our smart pointer against 0 using
// 0 == my_ptr
// This comparison allows us to test our smart pointer against null using
// nullptr == my_ptr
template <class RHS>
bool operator ==(DummyPtrType * dummy, const RefCountPtr<RHS> & rhs)
{
if (0 != dummy) {
if (nullptr != dummy) {
WWASSERT(0);
return false;
}

return 0 == rhs.Peek();
return nullptr == rhs.Peek();
}

// This comparison allows us to test our smart pointer against 0 using
// 0 != my_ptr
// This comparison allows us to test our smart pointer against null using
// nullptr != my_ptr
template <class RHS>
bool operator !=(DummyPtrType * dummy, const RefCountPtr<RHS> & rhs)
{
if (0 != dummy) {
if (nullptr != dummy) {
WWASSERT(0);
return true;
}

return 0 != rhs.Peek();
return nullptr != rhs.Peek();
}

template <class Derived, class Base>
RefCountPtr<Derived> Static_Cast(const RefCountPtr<Base> & base)
{
return RefCountPtr<Derived>::Create_AddRef((Derived *)base.Peek());
return RefCountPtr<Derived>::Create_Add_Ref(static_cast<Derived *>(base.Peek()));
}

template <class T>
RefCountPtr<T> Create_Add_Ref(T *ptr)
{
return RefCountPtr<T>::Create_Add_Ref(ptr);
}

template <class T>
RefCountPtr<T> Create_No_Add_Ref(T *ptr)
{
return RefCountPtr<T>::Create_No_Add_Ref(ptr);
}
15 changes: 5 additions & 10 deletions Core/Tools/W3DView/AssetInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,23 @@ AssetInfoClass::Initialize ()
if (m_AssetType != TypeMaterial) {

// Assume we are wrapping an instance as apposed to an asset 'name'.
RenderObjClass *prender_obj = m_pRenderObj;
if (prender_obj)
prender_obj->Add_Ref();
RefCountPtr<RenderObjClass> render_obj = m_pRenderObj;

// If we are wrapping an asset name, then create an instance of it.
if (prender_obj == nullptr) {
prender_obj = WW3DAssetManager::Get_Instance()->Create_Render_Obj (m_Name);
if (render_obj == nullptr) {
render_obj.Assign_No_Add_Ref (WW3DAssetManager::Get_Instance()->Create_Render_Obj (m_Name));
}

if (prender_obj != nullptr) {
if (render_obj != nullptr) {

// Get the hierarchy tree for this object (if one exists)
const HTreeClass *phtree = prender_obj->Get_HTree ();
const HTreeClass *phtree = render_obj->Get_HTree ();
if (phtree) {

// Get the name of the hierarchy tree
m_HierarchyName = phtree->Get_Name ();
}
}

// Release our hold on the temporary object
REF_PTR_RELEASE (prender_obj);
}
}

Expand Down
16 changes: 7 additions & 9 deletions Core/Tools/W3DView/AssetInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#pragma once

#include "WW3D2/rendobj.h"
#include "Utils.h"
#include "AssetTypes.h"


Expand All @@ -58,16 +57,15 @@ class AssetInfoClass
//
AssetInfoClass ()
: m_AssetType (TypeUnknown),
m_dwUserData (0L),
m_pRenderObj (nullptr) { Initialize (); }
m_dwUserData (0L) { Initialize (); }

AssetInfoClass (LPCTSTR passet_name, ASSET_TYPE type, RenderObjClass *prender_obj = nullptr, DWORD user_data = 0L)
: m_Name (passet_name),
m_AssetType (type),
m_dwUserData (user_data),
m_pRenderObj (nullptr) { REF_PTR_SET (m_pRenderObj, prender_obj); Initialize (); }
m_pRenderObj (Create_Add_Ref (prender_obj)) { Initialize (); }

virtual ~AssetInfoClass () { REF_PTR_RELEASE (m_pRenderObj); }
virtual ~AssetInfoClass () {}

//////////////////////////////////////////////////////////////
//
Expand All @@ -83,14 +81,14 @@ class AssetInfoClass
ASSET_TYPE Get_Type () const { return m_AssetType; }
DWORD Get_User_Number () const { return m_dwUserData; }
const CString & Get_User_String () const { return m_UserString; }
RenderObjClass * Get_Render_Obj () const { if (m_pRenderObj) m_pRenderObj->Add_Ref(); return m_pRenderObj; }
RenderObjClass * Peek_Render_Obj () const { return m_pRenderObj; }
RenderObjClass * Get_Render_Obj () const { RenderObjClass *ptr = m_pRenderObj.Peek(); if (ptr) ptr->Add_Ref(); return ptr; }
RenderObjClass * Peek_Render_Obj () const { return m_pRenderObj.Peek(); }
void Set_Name (LPCTSTR pname) { m_Name = pname; }
void Set_Hierarchy_Name (LPCTSTR pname) { m_HierarchyName = pname; }
void Set_Type (ASSET_TYPE type) { m_AssetType = type; }
void Set_User_Number (DWORD user_data) { m_dwUserData = user_data; }
void Set_User_String (LPCTSTR string) { m_UserString = string; }
void Set_Render_Obj (RenderObjClass *pobj) { REF_PTR_SET (m_pRenderObj, pobj); }
void Set_Render_Obj (RenderObjClass *pobj) { m_pRenderObj.Assign_Add_Ref (pobj); }

//
// Information methods
Expand Down Expand Up @@ -118,5 +116,5 @@ class AssetInfoClass
CString m_OriginalName;
ASSET_TYPE m_AssetType;
DWORD m_dwUserData;
RenderObjClass * m_pRenderObj;
RefCountPtr<RenderObjClass> m_pRenderObj;
};
18 changes: 8 additions & 10 deletions Core/Tools/W3DView/GraphicView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ IMPLEMENT_DYNCREATE(CGraphicView, CView)
////////////////////////////////////////////////////////////////////////////
CGraphicView::CGraphicView ()
: m_bInitialized (FALSE),
m_pCamera (nullptr),
m_TimerID (0),
m_bMouseDown (FALSE),
m_bRMouseDown (FALSE),
Expand All @@ -83,7 +82,6 @@ CGraphicView::CGraphicView ()
m_objectRotation (NoRotation),
m_LightRotation (NoRotation),
m_bLightMeshInScene (false),
m_pLightMesh (nullptr),
m_ParticleCountUpdate (0),
m_CameraBonePosX (false),
m_UpdateCounter (0),
Expand Down Expand Up @@ -212,7 +210,7 @@ CGraphicView::InitializeGraphicView ()
if (bReturn && (m_pCamera == nullptr))
{
// Instantiate a new camera class
m_pCamera = new CameraClass ();
m_pCamera.Assign_No_Add_Ref (new CameraClass ());
bReturn = (m_pCamera != nullptr);

// Were we successful in creating a camera?
Expand All @@ -230,7 +228,7 @@ CGraphicView::InitializeGraphicView ()
//
// Attach the 'listener' to the camera
//
WWAudioClass::Get_Instance ()->Get_Sound_Scene ()->Attach_Listener_To_Obj (m_pCamera);
WWAudioClass::Get_Instance ()->Get_Sound_Scene ()->Attach_Listener_To_Obj (m_pCamera.Peek());
}

Reset_FOV ();
Expand All @@ -240,7 +238,7 @@ CGraphicView::InitializeGraphicView ()
ResourceFileClass light_mesh_file (nullptr, "Light.w3d");
WW3DAssetManager::Get_Instance()->Load_3D_Assets (light_mesh_file);

m_pLightMesh = WW3DAssetManager::Get_Instance()->Create_Render_Obj ("LIGHT");
m_pLightMesh.Assign_No_Add_Ref (WW3DAssetManager::Get_Instance()->Create_Render_Obj ("LIGHT"));
ASSERT (m_pLightMesh != nullptr);
m_bLightMeshInScene = false;
}
Expand Down Expand Up @@ -323,8 +321,8 @@ CGraphicView::OnDestroy ()
//
// Free the camera object
//
REF_PTR_RELEASE (m_pCamera);
REF_PTR_RELEASE (m_pLightMesh);
m_pCamera.Clear();
m_pLightMesh.Clear();

// Is there an update thread running?
if (m_TimerID == 0) {
Expand Down Expand Up @@ -520,7 +518,7 @@ CGraphicView::RepaintView
// Wait for all previous rendering to complete before starting benchmark.
DWORD profile_time = ::Get_CPU_Clock (pt_high);

WW3D::Render (doc->GetScene (), m_pCamera, FALSE, FALSE);
WW3D::Render (doc->GetScene (), m_pCamera.Peek(), FALSE, FALSE);

// Wait for all rendering to complete before stopping benchmark.
DWORD milliseconds = (::Get_CPU_Clock (pt_high) - profile_time) / 1000;
Expand All @@ -531,7 +529,7 @@ CGraphicView::RepaintView
WW3D::Render (doc->GetCursorScene (), doc->Get2DCamera (), FALSE, FALSE);

// Render the dazzles
doc->Render_Dazzles(m_pCamera);
doc->Render_Dazzles(m_pCamera.Peek());

// Finish out the rendering process
WW3D::End_Render ();
Expand Down Expand Up @@ -589,7 +587,7 @@ CGraphicView::UpdateDisplay ()

// Render the current view inside the frame
WW3D::Begin_Render (TRUE, TRUE, Vector3 (0.2,0.4,0.6));
WW3D::Render (doc->GetScene (), m_pCamera, FALSE, FALSE);
WW3D::Render (doc->GetScene (), m_pCamera.Peek(), FALSE, FALSE);
WW3D::End_Render ();
} */
}
Expand Down
Loading
Loading