godot_voxel/util/ref_count.h

35 lines
595 B
C
Raw Normal View History

2020-09-06 19:01:12 +01:00
#ifndef VOXEL_VIEWER_REF_COUNT_H
#define VOXEL_VIEWER_REF_COUNT_H
#include "errors.h"
2022-01-31 21:29:08 +00:00
namespace zylann {
// Simple reference counter.
// This one is not thread-safe.
class RefCount {
2020-09-06 19:01:12 +01:00
public:
RefCount() {}
RefCount(unsigned int initial_count): _count(initial_count) {}
2021-04-15 20:16:27 +01:00
inline void add() {
++_count;
2020-09-06 19:01:12 +01:00
}
2021-04-15 20:16:27 +01:00
inline void remove() {
ZN_ASSERT_RETURN_MSG(_count != 0, "Trying to decrease refcount when it's already zero");
2021-04-15 20:16:27 +01:00
--_count;
2020-09-06 19:01:12 +01:00
}
2021-04-15 20:16:27 +01:00
inline unsigned int get() const {
return _count;
2020-09-06 19:01:12 +01:00
}
private:
2021-04-15 20:16:27 +01:00
unsigned int _count = 0;
2020-09-06 19:01:12 +01:00
};
2022-01-31 21:29:08 +00:00
} // namespace zylann
2020-09-06 19:01:12 +01:00
#endif // VOXEL_VIEWER_REF_COUNT_H