Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
pistache
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
pistache
Commits
daecadce
Unverified
Commit
daecadce
authored
Aug 09, 2019
by
Dennis Jenkins
Committed by
GitHub
Aug 09, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #589 from muttleyxd/fix-optional
Fix optional
parents
4dc9e3ef
002ec8de
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
113 additions
and
5 deletions
+113
-5
include/pistache/optional.h
include/pistache/optional.h
+10
-5
tests/CMakeLists.txt
tests/CMakeLists.txt
+1
-0
tests/optional_test.cc
tests/optional_test.cc
+102
-0
No files found.
include/pistache/optional.h
View file @
daecadce
...
...
@@ -106,6 +106,7 @@ public:
{
if
(
!
other
.
isEmpty
())
{
::
new
(
data
())
T
(
*
other
.
data
());
none_flag
=
ValueMarker
;
}
else
{
none_flag
=
NoneMarker
;
...
...
@@ -153,6 +154,7 @@ public:
data
()
->~
T
();
}
::
new
(
data
())
T
(
*
other
.
data
());
none_flag
=
ValueMarker
;
}
else
{
if
(
none_flag
!=
NoneMarker
)
{
...
...
@@ -237,33 +239,36 @@ private:
void
move_helper
(
Optional
<
T
>
&&
other
,
std
::
true_type
)
{
::
new
(
data
())
T
(
std
::
move
(
*
other
.
data
()));
none_flag
=
ValueMarker
;
}
void
move_helper
(
Optional
<
T
>
&&
other
,
std
::
false_type
)
{
::
new
(
data
())
T
(
*
other
.
data
());
none_flag
=
ValueMarker
;
}
template
<
typename
U
>
void
from_some_helper
(
types
::
Some
<
U
>
some
,
std
::
true_type
)
{
::
new
(
data
())
T
(
std
::
move
(
some
.
val_
));
none_flag
=
ValueMarker
;
}
template
<
typename
U
>
void
from_some_helper
(
types
::
Some
<
U
>
some
,
std
::
false_type
)
{
::
new
(
data
())
T
(
some
.
val_
);
none_flag
=
ValueMarker
;
}
typedef
uint8_t
none_flag_t
;
static
constexpr
none_flag_t
NoneMarker
=
1
;
static
constexpr
none_flag_t
ValueMarker
=
0
;
union
{
uint8_t
bytes
[
sizeof
(
T
)];
none_flag_t
none_flag
;
};
uint8_t
bytes
[
sizeof
(
T
)];
none_flag_t
none_flag
;
};
#define PistacheCheckSize(Type) \
static_assert(sizeof(Optional<Type>) == sizeof(Type), "Size differs")
static_assert(sizeof(Optional<Type>) == sizeof(Type)
+ sizeof(uint8_t)
, "Size differs")
PistacheCheckSize
(
uint8_t
);
PistacheCheckSize
(
uint16_t
);
...
...
tests/CMakeLists.txt
View file @
daecadce
...
...
@@ -33,6 +33,7 @@ pistache_test(mailbox_test)
pistache_test
(
stream_test
)
pistache_test
(
reactor_test
)
pistache_test
(
threadname_test
)
pistache_test
(
optional_test
)
if
(
PISTACHE_USE_SSL
)
...
...
tests/optional_test.cc
0 → 100644
View file @
daecadce
#include "gtest/gtest.h"
#include <pistache/optional.h>
using
Pistache
::
Optional
;
TEST
(
optional
,
constructor
)
{
Optional
<
bool
>
value
(
Pistache
::
Some
(
true
));
ASSERT_FALSE
(
value
.
isEmpty
());
EXPECT_TRUE
(
value
.
get
());
}
TEST
(
optional
,
copy_constructor
)
{
Optional
<
bool
>
value
(
Pistache
::
Some
(
true
));
ASSERT_FALSE
(
value
.
isEmpty
());
EXPECT_TRUE
(
value
.
get
());
Optional
<
bool
>
copy_constructed
(
value
);
ASSERT_FALSE
(
copy_constructed
.
isEmpty
());
EXPECT_TRUE
(
copy_constructed
.
get
());
}
TEST
(
optional
,
assignment_operator
)
{
Optional
<
bool
>
value
;
EXPECT_TRUE
(
value
.
isEmpty
());
value
=
Pistache
::
Some
(
true
);
ASSERT_FALSE
(
value
.
isEmpty
());
EXPECT_TRUE
(
value
.
get
());
}
TEST
(
optional
,
copy_assignment_operator
)
{
Optional
<
bool
>
value
(
Pistache
::
Some
(
true
));
ASSERT_FALSE
(
value
.
isEmpty
());
Optional
<
bool
>
other
;
EXPECT_TRUE
(
other
.
isEmpty
());
other
=
value
;
ASSERT_FALSE
(
other
.
isEmpty
());
EXPECT_TRUE
(
other
.
get
());
}
TEST
(
optional
,
move_constructor
)
{
Optional
<
bool
>
value
(
Pistache
::
Some
(
true
));
ASSERT_FALSE
(
value
.
isEmpty
());
EXPECT_TRUE
(
value
.
get
());
Optional
<
bool
>
value_from_move
(
std
::
move
(
value
));
ASSERT_FALSE
(
value_from_move
.
isEmpty
());
EXPECT_TRUE
(
value_from_move
.
get
());
}
TEST
(
optional
,
move_assignment_operator
)
{
Optional
<
bool
>
value
(
Pistache
::
Some
(
true
));
ASSERT_FALSE
(
value
.
isEmpty
());
EXPECT_TRUE
(
value
.
get
());
Optional
<
bool
>
move_assigned
=
std
::
move
(
value
);
ASSERT_FALSE
(
move_assigned
.
isEmpty
());
EXPECT_TRUE
(
move_assigned
.
get
());
}
TEST
(
optional
,
constructor_none
)
{
Optional
<
bool
>
value
(
Pistache
::
None
());
EXPECT_TRUE
(
value
.
isEmpty
());
}
TEST
(
optional
,
copy_constructor_none
)
{
Optional
<
bool
>
value
(
Pistache
::
None
());
EXPECT_TRUE
(
value
.
isEmpty
());
Optional
<
bool
>
copy_constructed
(
value
);
EXPECT_TRUE
(
value
.
isEmpty
());
}
TEST
(
optional
,
assignment_operator_none
)
{
Optional
<
bool
>
value
(
Pistache
::
None
());
EXPECT_TRUE
(
value
.
isEmpty
());
Optional
<
bool
>
assigned
=
Pistache
::
None
();
EXPECT_TRUE
(
assigned
.
isEmpty
());
}
TEST
(
optional
,
move_constructor_none
)
{
Optional
<
bool
>
value
(
Pistache
::
None
());
EXPECT_TRUE
(
value
.
isEmpty
());
Optional
<
bool
>
move_constructed
(
std
::
move
(
value
));
EXPECT_TRUE
(
move_constructed
.
isEmpty
());
}
TEST
(
optional
,
move_assignment_operator_none
)
{
Optional
<
bool
>
value
(
Pistache
::
None
());
EXPECT_TRUE
(
value
.
isEmpty
());
Optional
<
bool
>
move_assigned
(
std
::
move
(
value
));
EXPECT_TRUE
(
move_assigned
.
isEmpty
());
}
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