fbshipit-source-id: d0f4b1833968aae403b114119f4e8cda184361c9
This commit is contained in:
45
libs/fbjni/src/main/cpp/include/fb/Countable.h
Normal file
45
libs/fbjni/src/main/cpp/include/fb/Countable.h
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
#include <atomic>
|
||||
#include <fb/assert.h>
|
||||
#include <fb/noncopyable.h>
|
||||
#include <fb/nonmovable.h>
|
||||
#include <fb/RefPtr.h>
|
||||
|
||||
namespace facebook {
|
||||
|
||||
class Countable : public noncopyable, public nonmovable {
|
||||
public:
|
||||
// RefPtr expects refcount to start at 0
|
||||
Countable() : m_refcount(0) {}
|
||||
virtual ~Countable()
|
||||
{
|
||||
FBASSERT(m_refcount == 0);
|
||||
}
|
||||
|
||||
private:
|
||||
void ref() {
|
||||
++m_refcount;
|
||||
}
|
||||
|
||||
void unref() {
|
||||
if (0 == --m_refcount) {
|
||||
delete this;
|
||||
}
|
||||
}
|
||||
|
||||
bool hasOnlyOneRef() const {
|
||||
return m_refcount == 1;
|
||||
}
|
||||
|
||||
template <typename T> friend class RefPtr;
|
||||
std::atomic<int> m_refcount;
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user