Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
arbor
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Analyze
Contributor analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
arbor-sim
arbor
Commits
5ed4eb72
Commit
5ed4eb72
authored
8 years ago
by
Benjamin Cumming
Browse files
Options
Downloads
Patches
Plain Diff
add double_buffer type
parent
2af3e65b
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/util/double_buffer.hpp
+57
-0
57 additions, 0 deletions
src/util/double_buffer.hpp
tests/unit/CMakeLists.txt
+1
-0
1 addition, 0 deletions
tests/unit/CMakeLists.txt
tests/unit/test_double_buffer.cpp
+58
-0
58 additions, 0 deletions
tests/unit/test_double_buffer.cpp
with
116 additions
and
0 deletions
src/util/double_buffer.hpp
0 → 100644
+
57
−
0
View file @
5ed4eb72
#pragma once
#include
<array>
#include
<atomic>
#include
<util/debug.hpp>
namespace
nest
{
namespace
mc
{
namespace
util
{
template
<
typename
T
>
class
double_buffer
{
private:
std
::
atomic
<
int
>
index_
;
std
::
array
<
T
,
2
>
buffers_
;
int
other_index
()
{
return
index_
?
0
:
1
;
}
public
:
using
value_type
=
T
;
double_buffer
()
:
index_
(
0
)
{}
double_buffer
(
double_buffer
&&
)
=
delete
;
double_buffer
(
const
double_buffer
&
)
=
delete
;
double_buffer
&
operator
=
(
const
double_buffer
&
)
=
delete
;
double_buffer
&
operator
=
(
double_buffer
&&
)
=
delete
;
void
exchange
()
{
index_
^=
1
;
}
value_type
&
get
()
{
return
buffers_
[
index_
];
}
const
value_type
&
get
()
const
{
return
buffers_
[
index_
];
}
value_type
&
other
()
{
return
buffers_
[
other_index
()];
}
const
value_type
&
other
()
const
{
return
buffers_
[
other_index
()];
}
};
}
// namespace util
}
// namespace mc
}
// namespace nest
This diff is collapsed.
Click to expand it.
tests/unit/CMakeLists.txt
+
1
−
0
View file @
5ed4eb72
...
...
@@ -11,6 +11,7 @@ set(HEADERS
set
(
TEST_SOURCES
# unit tests
test_algorithms.cpp
test_double_buffer.cpp
test_cell.cpp
test_compartments.cpp
test_event_queue.cpp
...
...
This diff is collapsed.
Click to expand it.
tests/unit/test_double_buffer.cpp
0 → 100644
+
58
−
0
View file @
5ed4eb72
#include
"gtest.h"
#include
<util/double_buffer.hpp>
// not much to test here: just test that values passed into the constructor
// are correctly stored in members
TEST
(
double_buffer
,
exchange_and_get
)
{
using
namespace
nest
::
mc
::
util
;
double_buffer
<
int
>
buf
;
buf
.
get
()
=
2134
;
buf
.
exchange
();
buf
.
get
()
=
8990
;
buf
.
exchange
();
EXPECT_EQ
(
buf
.
get
(),
2134
);
EXPECT_EQ
(
buf
.
other
(),
8990
);
buf
.
exchange
();
EXPECT_EQ
(
buf
.
get
(),
8990
);
EXPECT_EQ
(
buf
.
other
(),
2134
);
buf
.
exchange
();
EXPECT_EQ
(
buf
.
get
(),
2134
);
EXPECT_EQ
(
buf
.
other
(),
8990
);
}
TEST
(
double_buffer
,
assign_get_other
)
{
using
namespace
nest
::
mc
::
util
;
double_buffer
<
std
::
string
>
buf
;
buf
.
get
()
=
"1"
;
buf
.
other
()
=
"2"
;
EXPECT_EQ
(
buf
.
get
(),
"1"
);
EXPECT_EQ
(
buf
.
other
(),
"2"
);
}
TEST
(
double_buffer
,
non_pod
)
{
using
namespace
nest
::
mc
::
util
;
double_buffer
<
std
::
string
>
buf
;
buf
.
get
()
=
"1"
;
buf
.
other
()
=
"2"
;
EXPECT_EQ
(
buf
.
get
(),
"1"
);
EXPECT_EQ
(
buf
.
other
(),
"2"
);
buf
.
exchange
();
EXPECT_EQ
(
buf
.
get
(),
"2"
);
EXPECT_EQ
(
buf
.
other
(),
"1"
);
buf
.
exchange
();
EXPECT_EQ
(
buf
.
get
(),
"1"
);
EXPECT_EQ
(
buf
.
other
(),
"2"
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment