Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
f2a20a0a
Commit
f2a20a0a
authored
Dec 15, 2013
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
util/WritableBuffer: add cast methods
parent
e5a2efaa
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
1 deletion
+30
-1
WritableBuffer.hxx
src/util/WritableBuffer.hxx
+30
-1
No files found.
src/util/WritableBuffer.hxx
View file @
f2a20a0a
...
...
@@ -32,7 +32,11 @@
#include "Compiler.h"
#include <stddef.h>
#include <cstddef>
#ifndef NDEBUG
#include <assert.h>
#endif
/**
* A reference to a memory area that is writable.
...
...
@@ -52,6 +56,8 @@ struct WritableBuffer {
WritableBuffer
()
=
default
;
constexpr
WritableBuffer
(
std
::
nullptr_t
)
:
data
(
nullptr
),
size
(
0
)
{}
constexpr
WritableBuffer
(
pointer_type
_data
,
size_type
_size
)
:
data
(
_data
),
size
(
_size
)
{}
...
...
@@ -59,6 +65,29 @@ struct WritableBuffer {
return
{
nullptr
,
0
};
}
/**
* Cast a WritableBuffer<void> to a WritableBuffer<T>. A "void"
* buffer records its size in bytes, and when casting to "T",
* the assertion below ensures that the size is a multiple of
* sizeof(T).
*/
#ifdef NDEBUG
constexpr
#endif
static
WritableBuffer
<
T
>
FromVoid
(
WritableBuffer
<
void
>
other
)
{
static_assert
(
sizeof
(
T
)
>
0
,
"Empty base type"
);
#ifndef NDEBUG
assert
(
other
.
size
%
sizeof
(
T
)
==
0
);
#endif
return
WritableBuffer
<
T
>
(
pointer_type
(
other
.
data
),
other
.
size
/
sizeof
(
T
));
}
constexpr
WritableBuffer
<
void
>
ToVoid
()
const
{
static_assert
(
sizeof
(
T
)
>
0
,
"Empty base type"
);
return
WritableBuffer
<
void
>
(
data
,
size
*
sizeof
(
T
));
}
constexpr
bool
IsNull
()
const
{
return
data
==
nullptr
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment