Browse Source

Getter for buffer's use count, in case that std::shared_ptr is replaced in the future.

David Piuva 3 years ago
parent
commit
3d1b27e1c3
2 changed files with 22 additions and 11 deletions
  1. 13 5
      Source/DFPSR/api/bufferAPI.cpp
  2. 9 6
      Source/DFPSR/api/bufferAPI.h

+ 13 - 5
Source/DFPSR/api/bufferAPI.cpp

@@ -93,7 +93,7 @@ BufferImpl::~BufferImpl() {
 
 
 // API
 // API
 
 
-Buffer buffer_clone(Buffer buffer) {
+Buffer buffer_clone(const Buffer &buffer) {
 	if (!buffer_exists(buffer)) {
 	if (!buffer_exists(buffer)) {
 		return Buffer();
 		return Buffer();
 	} else {
 	} else {
@@ -121,7 +121,7 @@ Buffer buffer_create(int64_t newSize, uint8_t *newData) {
 	}
 	}
 }
 }
 
 
-void buffer_replaceDestructor(Buffer buffer, const std::function<void(uint8_t *)>& newDestructor) {
+void buffer_replaceDestructor(const Buffer &buffer, const std::function<void(uint8_t *)>& newDestructor) {
 	if (!buffer_exists(buffer)) {
 	if (!buffer_exists(buffer)) {
 		throwError(U"buffer_replaceDestructor: Cannot replace destructor for a buffer that don't exist.\n");
 		throwError(U"buffer_replaceDestructor: Cannot replace destructor for a buffer that don't exist.\n");
 	} else {
 	} else {
@@ -129,7 +129,7 @@ void buffer_replaceDestructor(Buffer buffer, const std::function<void(uint8_t *)
 	}
 	}
 }
 }
 
 
-int64_t buffer_getSize(Buffer buffer) {
+int64_t buffer_getSize(const Buffer &buffer) {
 	if (!buffer_exists(buffer)) {
 	if (!buffer_exists(buffer)) {
 		return 0;
 		return 0;
 	} else {
 	} else {
@@ -137,7 +137,15 @@ int64_t buffer_getSize(Buffer buffer) {
 	}
 	}
 }
 }
 
 
-uint8_t* buffer_dangerous_getUnsafeData(Buffer buffer) {
+int64_t buffer_getUseCount(const Buffer &buffer) {
+	if (!buffer_exists(buffer)) {
+		return 0;
+	} else {
+		return buffer.use_count();
+	}
+}
+
+uint8_t* buffer_dangerous_getUnsafeData(const Buffer &buffer) {
 	if (!buffer_exists(buffer)) {
 	if (!buffer_exists(buffer)) {
 		return nullptr;
 		return nullptr;
 	} else {
 	} else {
@@ -145,7 +153,7 @@ uint8_t* buffer_dangerous_getUnsafeData(Buffer buffer) {
 	}
 	}
 }
 }
 
 
-void buffer_setBytes(Buffer buffer, uint8_t value) {
+void buffer_setBytes(const Buffer &buffer, uint8_t value) {
 	if (!buffer_exists(buffer)) {
 	if (!buffer_exists(buffer)) {
 		throwError(U"buffer_setBytes: Cannot set bytes for a buffer that don't exist.\n");
 		throwError(U"buffer_setBytes: Cannot set bytes for a buffer that don't exist.\n");
 	} else {
 	} else {

+ 9 - 6
Source/DFPSR/api/bufferAPI.h

@@ -47,7 +47,7 @@ namespace dsr {
 
 
 	// Sets the allocation's destructor, to be called when there are no more reference counted pointers to the buffer.
 	// Sets the allocation's destructor, to be called when there are no more reference counted pointers to the buffer.
 	// Pre-condition: buffer exists
 	// Pre-condition: buffer exists
-	void buffer_replaceDestructor(Buffer buffer, const std::function<void(uint8_t *)>& newDestructor);
+	void buffer_replaceDestructor(const Buffer &buffer, const std::function<void(uint8_t *)>& newDestructor);
 
 
 	// Returns true iff buffer exists
 	// Returns true iff buffer exists
 	inline bool buffer_exists(Buffer buffer) {
 	inline bool buffer_exists(Buffer buffer) {
@@ -56,22 +56,25 @@ namespace dsr {
 
 
 	// Returns a clone of the buffer.
 	// Returns a clone of the buffer.
 	// Giving an empty handle returns an empty handle
 	// Giving an empty handle returns an empty handle
-	Buffer buffer_clone(Buffer buffer);
+	Buffer buffer_clone(const Buffer &buffer);
 
 
 	// Returns the buffer's size in bytes, as given when allocating it excluding allocation padding
 	// Returns the buffer's size in bytes, as given when allocating it excluding allocation padding
 	// Returns zero if buffer doesn't exist
 	// Returns zero if buffer doesn't exist
-	int64_t buffer_getSize(Buffer buffer);
+	int64_t buffer_getSize(const Buffer &buffer);
+
+	// Returns the number of reference counted handles to the buffer, or 0 if the buffer does not exist.
+	int64_t buffer_getUseCount(const Buffer &buffer);
 
 
 	// Returns a raw pointer to the data.
 	// Returns a raw pointer to the data.
 	// An empty handle will return nullptr.
 	// An empty handle will return nullptr.
-	uint8_t* buffer_dangerous_getUnsafeData(Buffer buffer);
+	uint8_t* buffer_dangerous_getUnsafeData(const Buffer &buffer);
 
 
 	// A wrapper for getting a bound-checked pointer of the correct element type.
 	// A wrapper for getting a bound-checked pointer of the correct element type.
 	//   Only cast to trivially packed types with power of two dimensions so that the compiler does not add padding.
 	//   Only cast to trivially packed types with power of two dimensions so that the compiler does not add padding.
 	// The name must be an ansi encoded constant literal, because each String contains a Buffer which would cause a cyclic dependency.
 	// The name must be an ansi encoded constant literal, because each String contains a Buffer which would cause a cyclic dependency.
 	// Returns a safe null pointer if buffer does not exist.
 	// Returns a safe null pointer if buffer does not exist.
 	template <typename T>
 	template <typename T>
-	SafePointer<T> buffer_getSafeData(Buffer buffer, const char* name) {
+	SafePointer<T> buffer_getSafeData(const Buffer &buffer, const char* name) {
 		if (!buffer_exists(buffer)) {
 		if (!buffer_exists(buffer)) {
 			return SafePointer<T>();
 			return SafePointer<T>();
 		} else {
 		} else {
@@ -82,7 +85,7 @@ namespace dsr {
 
 
 	// Set all bytes to the same value
 	// Set all bytes to the same value
 	// Pre-condition: buffer exists
 	// Pre-condition: buffer exists
-	void buffer_setBytes(Buffer buffer, uint8_t value);
+	void buffer_setBytes(const Buffer &buffer, uint8_t value);
 }
 }
 
 
 #endif
 #endif