Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
9.2.2023: Due to updates GitLab will be unavailable for some minutes between 9:00 and 11:00.
Open sidebar
CAMP
campvis-public
Commits
12c47d2e
Commit
12c47d2e
authored
Jul 14, 2014
by
Christian Schulte zu Berge
Browse files
Replaced custom reference counting implementation with std::shared_ptr.
parent
504af5c9
Changes
5
Hide whitespace changes
Inline
Side-by-side
core/datastructures/abstractdata.cpp
View file @
12c47d2e
...
...
@@ -26,12 +26,12 @@
namespace
campvis
{
AbstractData
::
AbstractData
()
:
ReferenceCounted
()
{
AbstractData
::
AbstractData
()
{
}
AbstractData
::~
AbstractData
()
{
}
}
\ No newline at end of file
core/datastructures/datahandle.cpp
View file @
12c47d2e
...
...
@@ -29,52 +29,34 @@
namespace
campvis
{
DataHandle
::
DataHandle
(
AbstractData
*
data
)
:
_
data
(
data
)
:
_
ptr
(
data
)
,
_timestamp
(
clock
())
{
init
();
}
DataHandle
::
DataHandle
(
const
DataHandle
&
rhs
)
:
_
data
(
rhs
.
_
data
)
:
_
ptr
(
rhs
.
_
ptr
)
,
_timestamp
(
rhs
.
_timestamp
)
{
init
();
}
DataHandle
&
DataHandle
::
operator
=
(
const
DataHandle
&
rhs
)
{
if
(
_data
!=
rhs
.
_data
)
{
AbstractData
*
oldData
=
_data
;
_data
=
rhs
.
_data
;
if
(
_ptr
!=
rhs
.
_ptr
)
{
_ptr
=
rhs
.
_ptr
;
_timestamp
=
rhs
.
_timestamp
;
init
();
if
(
oldData
)
{
oldData
->
removeReference
();
}
}
return
*
this
;
}
DataHandle
::~
DataHandle
()
{
if
(
_data
)
_data
->
removeReference
();
}
const
AbstractData
*
DataHandle
::
getData
()
const
{
return
_data
;
}
void
DataHandle
::
init
()
{
if
(
_data
==
0
)
return
;
if
(
!
_data
->
isShareable
())
{
_data
=
_data
->
clone
();
_timestamp
=
clock
();
}
_data
->
addReference
();
return
_ptr
.
get
();
}
clock_t
DataHandle
::
getTimestamp
()
const
{
...
...
core/datastructures/datahandle.h
View file @
12c47d2e
...
...
@@ -26,6 +26,7 @@
#define datahandle_h__
#include
<ctime>
#include
<memory>
#include
"core/coreapi.h"
...
...
@@ -91,12 +92,7 @@ namespace campvis {
private:
/**
* Initializes the reference counting for the data.
*/
void
init
();
AbstractData
*
_data
;
///< managed data
std
::
shared_ptr
<
AbstractData
>
_ptr
;
///< managed data
clock_t
_timestamp
;
///< Timestamp when this data has been created
};
...
...
core/tools/referencecounted.cpp
deleted
100644 → 0
View file @
504af5c9
// ================================================================================================
//
// This file is part of the CAMPVis Software Framework.
//
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
// Christian Schulte zu Berge <christian.szb@in.tum.de>
// Chair for Computer Aided Medical Procedures
// Technische Universitaet Muenchen
// Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
//
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
//
// ================================================================================================
#include
"referencecounted.h"
#include
"core/tools/job.h"
#include
"core/tools/simplejobprocessor.h"
namespace
campvis
{
ReferenceCounted
::
ReferenceCounted
()
:
_shareable
(
true
)
{
_refCount
=
0
;
}
ReferenceCounted
::
ReferenceCounted
(
const
ReferenceCounted
&
rhs
)
:
_shareable
(
true
)
{
_refCount
=
0
;
}
ReferenceCounted
&
ReferenceCounted
::
operator
=
(
const
ReferenceCounted
&
rhs
)
{
return
*
this
;
}
ReferenceCounted
::~
ReferenceCounted
()
{
}
void
ReferenceCounted
::
addReference
()
{
++
_refCount
;
}
void
ReferenceCounted
::
removeReference
()
{
if
(
--
_refCount
==
0
)
SimpleJobProc
.
enqueueJob
(
makeJob
(
&
ReferenceCounted
::
deleteInstance
,
this
));
}
void
ReferenceCounted
::
markUnsharable
()
{
_shareable
=
false
;
}
bool
ReferenceCounted
::
isShareable
()
const
{
return
_shareable
;
}
bool
ReferenceCounted
::
isShared
()
const
{
return
_refCount
>
1
;
}
void
ReferenceCounted
::
deleteInstance
(
ReferenceCounted
*
instance
)
{
delete
instance
;
}
}
core/tools/referencecounted.h
deleted
100644 → 0
View file @
504af5c9
// ================================================================================================
//
// This file is part of the CAMPVis Software Framework.
//
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
// Christian Schulte zu Berge <christian.szb@in.tum.de>
// Chair for Computer Aided Medical Procedures
// Technische Universitaet Muenchen
// Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
//
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
//
// ================================================================================================
#ifndef REFERENCECOUNTED_H__
#define REFERENCECOUNTED_H__
#include
<tbb/atomic.h>
#include
"core/coreapi.h"
namespace
campvis
{
/**
* Base class for reference counted objects.
*
* \note Implementation inspired from Scott Meyers: More Effective C++, Item 29
* \todo Check thread-safety
*/
class
CAMPVIS_CORE_API
ReferenceCounted
{
public:
/**
* Create a new reference counted object.
*/
ReferenceCounted
();
/**
* Copy-constructor for reference counted objects.
* \param rhs source object
*/
ReferenceCounted
(
const
ReferenceCounted
&
rhs
);
/**
* Assignment operator for reference counted objects
* \param rhs source object
* \return *this
*/
ReferenceCounted
&
operator
=
(
const
ReferenceCounted
&
rhs
);
/**
* Pure virtual destructor.
*/
virtual
~
ReferenceCounted
()
=
0
;
/**
* Increment the reference count.
*/
void
addReference
();
/**
* Decrement the reference count and delete the object if necessary.
*/
void
removeReference
();
/**
* Mark this object as not shareable, i.e. there exist non-const pointers to this object.
*/
void
markUnsharable
();
/**
* Returns, whether this object is shareable, i.e. no non-const pointers to this object exist.
* \return _shareable
*/
bool
isShareable
()
const
;
/**
* Returns, whether this object has more than one references.
* \return _refCount > 1
*/
bool
isShared
()
const
;
static
void
deleteInstance
(
ReferenceCounted
*
instance
);
private:
tbb
::
atomic
<
size_t
>
_refCount
;
///< number of references to this object
bool
_shareable
;
///< flag whether this object is shareable, i.e. no non-const pointers to this object exist
};
}
#endif // REFERENCECOUNTED_H__
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment