aboutsummaryrefslogtreecommitdiffstats
path: root/src/util/Cast.hxx
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/util/Cast.hxx (renamed from src/util/growing_fifo.h)55
1 files changed, 20 insertions, 35 deletions
diff --git a/src/util/growing_fifo.h b/src/util/Cast.hxx
index 723c3b3ff..69172e6de 100644
--- a/src/util/growing_fifo.h
+++ b/src/util/Cast.hxx
@@ -1,6 +1,5 @@
/*
- * Copyright (C) 2003-2011 The Music Player Daemon Project
- * http://www.musicpd.org
+ * Copyright (C) 2013 Max Kellermann <max@duempel.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -28,46 +27,32 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-/** \file
- *
- * Helper functions for our FIFO buffer library (fifo_buffer.h) that
- * allows growing the buffer on demand.
- *
- * This library is not thread safe.
- */
-
-#ifndef MPD_GROWING_FIFO_H
-#define MPD_GROWING_FIFO_H
+#ifndef CAST_HXX
+#define CAST_HXX
#include <stddef.h>
-struct fifo_buffer;
-
/**
- * Allocate a new #fifo_buffer with the default size.
- */
-struct fifo_buffer *
-growing_fifo_new(void);
-
-/**
- * Prepares writing to the buffer, see fifo_buffer_write() for
- * details. The difference is that this function will automatically
- * grow the buffer if it is too small.
- *
- * The caller is responsible for limiting the capacity of the buffer.
- *
- * @param length the number of bytes that will be written
- * @return a pointer to the end of the buffer (will not be NULL)
+ * Offset the given pointer by the specified number of bytes.
*/
-void *
-growing_fifo_write(struct fifo_buffer **buffer_p, size_t length);
+static constexpr void *
+OffsetPointer(void *p, ptrdiff_t offset)
+{
+ return (char *)p + offset;
+}
+
+template<typename T, typename U>
+static constexpr T *
+OffsetCast(U *p, ptrdiff_t offset)
+{
+ return reinterpret_cast<T *>(OffsetPointer(p, offset));
+}
/**
- * A helper function that combines growing_fifo_write(), memcpy(),
- * fifo_buffer_append().
+ * Cast the given pointer to a struct member to its parent structure.
*/
-void
-growing_fifo_append(struct fifo_buffer **buffer_p,
- const void *data, size_t length);
+#define ContainerCast(p, container, attribute) \
+ OffsetCast<container, decltype(((container*)nullptr)->attribute)>\
+ ((p), -ptrdiff_t(offsetof(container, attribute)))
#endif