Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
7dcc7b4e
"...git@developer.sourcefind.cn:renzhc/diffusers_dcu.git" did not exist on "d61889fc17c71a01af31f9f86ab91dbb86587ac3"
Commit
7dcc7b4e
authored
Sep 05, 2020
by
Davis King
Browse files
Added call_if_valid()
parent
e7ec6b77
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
0 deletions
+77
-0
dlib/metaprogramming.h
dlib/metaprogramming.h
+40
-0
dlib/test/metaprogramming.cpp
dlib/test/metaprogramming.cpp
+37
-0
No files found.
dlib/metaprogramming.h
View file @
7dcc7b4e
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
#ifndef DLIB_METApROGRAMMING_Hh_
#ifndef DLIB_METApROGRAMMING_Hh_
#define DLIB_METApROGRAMMING_Hh_
#define DLIB_METApROGRAMMING_Hh_
#include "algs.h"
namespace
dlib
namespace
dlib
{
{
...
@@ -62,6 +63,45 @@ namespace dlib
...
@@ -62,6 +63,45 @@ namespace dlib
// base case
// base case
template
<
>
struct
make_compile_time_integer_range
<
0
>
{
typedef
compile_time_integer_list
<>
type
;
};
template
<
>
struct
make_compile_time_integer_range
<
0
>
{
typedef
compile_time_integer_list
<>
type
;
};
// ----------------------------------------------------------------------------------------
namespace
impl
{
template
<
typename
Funct
,
typename
...
Args
,
typename
int_
<
decltype
(
std
::
declval
<
Funct
>()(
std
::
declval
<
Args
>
()...))
>::
type
=
0
>
bool
call_if_valid
(
special_
,
Funct
&&
f
,
Args
&&
...
args
)
{
f
(
std
::
forward
<
Args
>
(
args
)...);
return
true
;
}
template
<
typename
Funct
,
typename
...
Args
>
bool
call_if_valid
(
general_
,
Funct
&&
/*f*/
,
Args
&&
...
/*args*/
)
{
return
false
;
}
}
template
<
typename
Funct
,
typename
...
Args
>
bool
call_if_valid
(
Funct
&&
f
,
Args
&&
...
args
)
/*!
ensures
- if f(std::forward<Args>(args)...) is a valid expression then we evaluate it and return
true. Otherwise we do nothing and return false.
!*/
{
return
impl
::
call_if_valid
(
special_
(),
f
,
std
::
forward
<
Args
>
(
args
)...);
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
}
}
...
...
dlib/test/metaprogramming.cpp
View file @
7dcc7b4e
...
@@ -7,6 +7,7 @@
...
@@ -7,6 +7,7 @@
#include <cstdlib>
#include <cstdlib>
#include <ctime>
#include <ctime>
#include <dlib/algs.h>
#include <dlib/algs.h>
#include <dlib/metaprogramming.h>
#include "tester.h"
#include "tester.h"
...
@@ -70,6 +71,41 @@ namespace
...
@@ -70,6 +71,41 @@ namespace
}
}
void
test_call_if_valid
()
{
int
value
=
0
;
auto
foo
=
[
&
](
int
a
,
int
b
)
{
value
+=
a
+
b
;
};
auto
bar
=
[
&
](
std
::
string
)
{
value
++
;
};
auto
baz
=
[
&
]()
{
value
++
;
};
DLIB_TEST
(
value
==
0
);
DLIB_TEST
(
call_if_valid
(
baz
));
DLIB_TEST
(
value
==
1
);
DLIB_TEST
(
!
call_if_valid
(
foo
));
DLIB_TEST
(
value
==
1
);
DLIB_TEST
(
!
call_if_valid
(
bar
));
DLIB_TEST
(
value
==
1
);
DLIB_TEST
(
call_if_valid
(
bar
,
"stuff"
));
DLIB_TEST
(
value
==
2
);
DLIB_TEST
(
!
call_if_valid
(
baz
,
"stuff"
));
DLIB_TEST
(
value
==
2
);
DLIB_TEST
(
call_if_valid
(
foo
,
3
,
1
));
DLIB_TEST
(
value
==
6
);
DLIB_TEST
(
!
call_if_valid
(
bar
,
3
,
1
));
DLIB_TEST
(
value
==
6
);
// make sure stateful lambdas are modified when called
value
=
0
;
auto
stateful
=
[
&
value
,
i
=
value
]()
mutable
{
++
i
;
value
=
i
;
};
DLIB_TEST
(
call_if_valid
(
stateful
));
DLIB_TEST
(
value
==
1
);
DLIB_TEST
(
call_if_valid
(
stateful
));
DLIB_TEST
(
value
==
2
);
DLIB_TEST
(
call_if_valid
(
stateful
));
DLIB_TEST
(
value
==
3
);
}
class
metaprogramming_tester
:
public
tester
class
metaprogramming_tester
:
public
tester
...
@@ -85,6 +121,7 @@ namespace
...
@@ -85,6 +121,7 @@ namespace
)
)
{
{
metaprogramming_test
();
metaprogramming_test
();
test_call_if_valid
();
}
}
}
a
;
}
a
;
...
...
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