2015-04-02 23:08:27 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2015, Mozilla Foundation and contributors
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-12-18 23:54:34 +03:00
|
|
|
|
|
|
|
#ifndef __RefCount_h__
|
|
|
|
#define __RefCount_h__
|
|
|
|
|
2015-04-02 23:08:27 +03:00
|
|
|
#include <stdint.h>
|
2015-05-06 02:40:40 +03:00
|
|
|
#include <assert.h>
|
2015-05-06 02:40:42 +03:00
|
|
|
#include "ClearKeyUtils.h"
|
|
|
|
|
|
|
|
#include <atomic>
|
2015-04-02 23:08:27 +03:00
|
|
|
|
2015-05-06 02:40:40 +03:00
|
|
|
// Note: Thread safe.
|
2014-12-18 23:54:34 +03:00
|
|
|
class RefCounted {
|
|
|
|
public:
|
|
|
|
void AddRef() {
|
|
|
|
++mRefCount;
|
|
|
|
}
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
uint32_t Release() {
|
|
|
|
uint32_t newCount = --mRefCount;
|
|
|
|
if (!newCount) {
|
2014-12-18 23:54:34 +03:00
|
|
|
delete this;
|
|
|
|
}
|
2015-01-12 10:15:55 +03:00
|
|
|
return newCount;
|
2014-12-18 23:54:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
RefCounted()
|
|
|
|
: mRefCount(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual ~RefCounted()
|
|
|
|
{
|
2015-05-06 02:40:40 +03:00
|
|
|
assert(!mRefCount);
|
2014-12-18 23:54:34 +03:00
|
|
|
}
|
2016-12-16 01:57:58 +03:00
|
|
|
std::atomic<uint32_t> mRefCount;
|
2014-12-18 23:54:34 +03:00
|
|
|
};
|
|
|
|
|
2015-04-02 23:08:27 +03:00
|
|
|
template<class T>
|
|
|
|
class RefPtr {
|
|
|
|
public:
|
2017-01-11 23:52:05 +03:00
|
|
|
RefPtr(const RefPtr& src) {
|
|
|
|
Set(src.mPtr);
|
2017-01-11 23:52:05 +03:00
|
|
|
}
|
2017-01-11 23:52:05 +03:00
|
|
|
|
|
|
|
explicit RefPtr(T* aPtr) {
|
|
|
|
Set(aPtr);
|
|
|
|
}
|
|
|
|
RefPtr() { Set(nullptr); }
|
|
|
|
|
2015-04-02 23:08:27 +03:00
|
|
|
~RefPtr() {
|
2017-01-11 23:52:05 +03:00
|
|
|
Set(nullptr);
|
2015-04-02 23:08:27 +03:00
|
|
|
}
|
|
|
|
T* operator->() const { return mPtr; }
|
2017-01-11 23:52:05 +03:00
|
|
|
T** operator&() { return &mPtr; }
|
|
|
|
T* operator->() { return mPtr; }
|
|
|
|
operator T*() { return mPtr; }
|
|
|
|
|
|
|
|
T* Get() const { return mPtr; }
|
2015-04-02 23:08:27 +03:00
|
|
|
|
|
|
|
RefPtr& operator=(T* aVal) {
|
2017-01-11 23:52:05 +03:00
|
|
|
Set(aVal);
|
2015-04-02 23:08:27 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-01-11 23:52:05 +03:00
|
|
|
T* Set(T* aPtr) {
|
|
|
|
if (mPtr == aPtr) {
|
|
|
|
return aPtr;
|
|
|
|
}
|
2015-04-02 23:08:27 +03:00
|
|
|
if (mPtr) {
|
|
|
|
mPtr->Release();
|
|
|
|
}
|
|
|
|
mPtr = aPtr;
|
|
|
|
if (mPtr) {
|
|
|
|
aPtr->AddRef();
|
|
|
|
}
|
2017-01-11 23:52:05 +03:00
|
|
|
return mPtr;
|
2015-04-02 23:08:27 +03:00
|
|
|
}
|
2017-01-11 23:52:05 +03:00
|
|
|
|
|
|
|
T* mPtr = nullptr;
|
2015-04-02 23:08:27 +03:00
|
|
|
};
|
|
|
|
|
2014-12-18 23:54:34 +03:00
|
|
|
#endif // __RefCount_h__
|