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
cc35b618
Commit
cc35b618
authored
8 years ago
by
Sam Yates
Browse files
Options
Downloads
Patches
Plain Diff
Add 'nop_function' helper for trivial functions.
parent
3b37947a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/util/nop.hpp
+29
-0
29 additions, 0 deletions
src/util/nop.hpp
tests/unit/CMakeLists.txt
+1
-0
1 addition, 0 deletions
tests/unit/CMakeLists.txt
tests/unit/test_nop.cpp
+75
-0
75 additions, 0 deletions
tests/unit/test_nop.cpp
with
105 additions
and
0 deletions
src/util/nop.hpp
0 → 100644
+
29
−
0
View file @
cc35b618
#pragma once
/*
* Provide object that implicitly converts to
* a std::function object that does nothing but return a
* default-constructed type or void.
*/
#include
<functional>
namespace
nest
{
namespace
mc
{
namespace
util
{
static
struct
nop_function_t
{
template
<
typename
R
,
typename
...
Args
>
operator
std
::
function
<
R
(
Args
...)
>
()
const
{
return
[](
Args
...)
{
return
R
{};
};
}
template
<
typename
...
Args
>
operator
std
::
function
<
void
(
Args
...)
>
()
const
{
return
[](
Args
...)
{
};
}
}
nop_function
;
}
// namespace util
}
// namespace mc
}
// namespace nest
This diff is collapsed.
Click to expand it.
tests/unit/CMakeLists.txt
+
1
−
0
View file @
cc35b618
...
...
@@ -21,6 +21,7 @@ set(TEST_SOURCES
test_mask_stream.cpp
test_matrix.cpp
test_mechanisms.cpp
test_nop.cpp
test_optional.cpp
test_parameters.cpp
test_point.cpp
...
...
This diff is collapsed.
Click to expand it.
tests/unit/test_nop.cpp
0 → 100644
+
75
−
0
View file @
cc35b618
#include
"gtest.h"
#include
"util/nop.hpp"
using
namespace
nest
::
mc
::
util
;
TEST
(
nop
,
void_fn
)
{
std
::
function
<
void
()
>
f
{
nop_function
};
EXPECT_TRUE
(
f
);
f
();
// should do nothing
bool
flag
=
false
;
f
=
[
&
]()
{
flag
=
true
;
};
f
();
EXPECT_TRUE
(
flag
);
flag
=
false
;
f
=
nop_function
;
f
();
EXPECT_FALSE
(
flag
);
// with some arguments
std
::
function
<
void
(
int
,
int
)
>
g
{
nop_function
};
EXPECT_TRUE
(
g
);
g
(
2
,
3
);
// should do nothing
int
sum
=
0
;
g
=
[
&
](
int
a
,
int
b
)
{
sum
=
a
+
b
;
};
g
(
2
,
3
);
EXPECT_EQ
(
5
,
sum
);
sum
=
0
;
g
=
nop_function
;
g
(
2
,
3
);
EXPECT_EQ
(
0
,
sum
);
}
struct
check_default
{
int
value
=
100
;
check_default
()
=
default
;
explicit
check_default
(
int
n
)
:
value
(
n
)
{}
};
TEST
(
nop
,
default_return_fn
)
{
std
::
function
<
check_default
()
>
f
{
nop_function
};
EXPECT_TRUE
(
f
);
auto
result
=
f
();
EXPECT_EQ
(
result
.
value
,
100
);
f
=
[]()
{
return
check_default
(
17
);
};
result
=
f
();
EXPECT_EQ
(
result
.
value
,
17
);
f
=
nop_function
;
result
=
f
();
EXPECT_EQ
(
result
.
value
,
100
);
std
::
function
<
check_default
(
double
,
double
)
>
g
{
nop_function
};
EXPECT_TRUE
(
g
);
result
=
g
(
1.4
,
1.5
);
EXPECT_EQ
(
result
.
value
,
100
);
g
=
[](
double
x
,
double
y
)
{
return
check_default
{(
int
)(
x
*
y
)};
};
result
=
g
(
1.4
,
1.5
);
EXPECT_EQ
(
result
.
value
,
2
);
g
=
nop_function
;
result
=
g
(
1.4
,
1.5
);
EXPECT_EQ
(
result
.
value
,
100
);
}
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