Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
S
spdlog
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
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
spdlog
Commits
4b3e5b3e
Commit
4b3e5b3e
authored
Mar 20, 2014
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fast_buf
parent
ba2b6aea
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
103 additions
and
0 deletions
+103
-0
fast_buf.h
include/c11log/details/fast_buf.h
+103
-0
No files found.
include/c11log/details/fast_buf.h
0 → 100644
View file @
4b3e5b3e
#pragma once
#include <cstddef>
#include <utility>
#include <cstring>
#include <vector>
// Fast buffer
// stores its contents on the stack when possible, in vector<char> otherwise
// NOTE: User should be remember that returned buffer might be on the stack!!
namespace
c11log
{
namespace
details
{
template
<
std
::
size_t
STACK_SIZE
=
128
>
class
fast_buf
{
public
:
fast_buf
()
:
_stack_size
(
0
)
{}
~
fast_buf
()
{};
fast_buf
(
const
bufpair_t
&
buf_to_copy
)
:
fast_buf
()
{
append
(
buf_to_copy
);
}
fast_buf
(
const
fast_buf
&
other
)
{
_stack_size
=
other
.
_stack_size
;
if
(
!
other
.
_v
.
empty
())
_v
=
other
.
_v
;
else
if
(
_stack_size
)
std
::
copy
(
other
.
_stack_buf
.
begin
(),
other
.
_stack_buf
.
begin
()
+
_stack_size
,
_stack_buf
.
begin
());
}
fast_buf
(
fast_buf
&&
other
)
{
_stack_size
=
other
.
_stack_size
;
if
(
!
other
.
_v
.
empty
())
_v
=
other
.
_v
;
else
if
(
_stack_size
)
std
::
copy
(
other
.
_stack_buf
.
begin
(),
other
.
_stack_buf
.
begin
()
+
_stack_size
,
_stack_buf
.
begin
());
other
.
clear
();
}
fast_buf
&
operator
=
(
const
fast_buf
&
other
)
=
delete
;
fast_buf
&
operator
=
(
fast_buf
&&
other
)
=
delete
;
void
append
(
const
char
*
buf
,
std
::
size_t
size
)
{
//If we are aleady using _v, forget about the stack
if
(
!
_v
.
empty
())
{
_v
.
insert
(
_v
.
end
(),
buf
,
buf
+
size
);
}
//Try use the stack
else
{
if
(
_stack_size
+
size
<=
STACK_SIZE
)
{
std
::
memcpy
(
&
_stack_buf
[
_stack_size
],
buf
,
size
);
_stack_size
+=
size
;
}
//Not enough stack space. Copy all to _v
else
{
_v
.
reserve
(
_stack_size
+
size
);
if
(
_stack_size
)
_v
.
insert
(
_v
.
end
(),
_stack_buf
.
begin
(),
_stack_buf
.
begin
()
+
_stack_size
);
_v
.
insert
(
_v
.
end
(),
buf
,
buf
+
size
);
}
}
}
void
append
(
const
bufpair_t
&
buf
)
{
append
(
buf
.
first
,
buf
.
second
);
}
void
clear
()
{
_stack_size
=
0
;
_v
.
clear
();
}
bufpair_t
get
()
{
if
(
!
_v
.
empty
())
return
bufpair_t
(
_v
.
data
(),
_v
.
size
());
else
return
bufpair_t
(
_stack_buf
.
data
(),
_stack_size
);
}
private
:
std
::
vector
<
char
>
_v
;
std
::
array
<
char
,
STACK_SIZE
>
_stack_buf
;
std
::
size_t
_stack_size
;
};
}
}
//namespace c11log { namespace details {
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