Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
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
Operations
Operations
Metrics
Environments
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
cpp-libs
Actor Framework
Commits
3d4450f7
Commit
3d4450f7
authored
Apr 08, 2020
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Re-implement the message builder
parent
be0e4f12
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
96 additions
and
5 deletions
+96
-5
libcaf_core/CMakeLists.txt
libcaf_core/CMakeLists.txt
+1
-0
libcaf_core/caf/message_builder.hpp
libcaf_core/caf/message_builder.hpp
+4
-1
libcaf_core/src/message_builder.cpp
libcaf_core/src/message_builder.cpp
+25
-4
libcaf_core/test/message_builder.cpp
libcaf_core/test/message_builder.cpp
+66
-0
No files found.
libcaf_core/CMakeLists.txt
View file @
3d4450f7
...
...
@@ -234,6 +234,7 @@ set(CAF_CORE_TEST_SOURCES
test/mailbox_element.cpp
test/make_config_value_field.cpp
test/message.cpp
test/message_builder.cpp
test/message_id.cpp
test/message_lifetime.cpp
test/metaprogramming.cpp
...
...
libcaf_core/caf/message_builder.hpp
View file @
3d4450f7
...
...
@@ -23,6 +23,7 @@
#include "caf/detail/core_export.hpp"
#include "caf/detail/implicit_conversions.hpp"
#include "caf/detail/message_builder_element.hpp"
#include "caf/detail/padded_size.hpp"
#include "caf/detail/type_id_list_builder.hpp"
#include "caf/fwd.hpp"
#include "caf/message.hpp"
...
...
@@ -61,6 +62,7 @@ public:
using
namespace
detail
;
using
value_type
=
strip_and_convert_t
<
T
>
;
static_assert
(
sendable
<
value_type
>
);
storage_size_
+=
padded_size_v
<
strip_and_convert_t
<
value_type
>>
;
types_
.
push_back
(
type_id_v
<
value_type
>
);
elements_
.
emplace_back
(
make_message_builder_element
(
std
::
forward
<
T
>
(
x
)));
return
*
this
;
...
...
@@ -91,7 +93,7 @@ public:
/// Converts the buffer to an actual message object and transfers
/// ownership of the data to it, leaving this object in an invalid state.
/// @warning Calling *any* member function on this object afterwards
/// is undefined behavior
(dereferencing a `nullptr`)
/// is undefined behavior
.
message
move_to_message
();
/// Removes all elements from the buffer.
...
...
@@ -108,6 +110,7 @@ public:
}
private:
size_t
storage_size_
=
0
;
detail
::
type_id_list_builder
types_
;
std
::
vector
<
detail
::
message_builder_element_ptr
>
elements_
;
};
...
...
libcaf_core/src/message_builder.cpp
View file @
3d4450f7
...
...
@@ -21,18 +21,39 @@
namespace
caf
{
void
message_builder
::
clear
()
noexcept
{
storage_size_
=
0
;
types_
.
clear
();
elements_
.
clear
();
}
message
message_builder
::
to_message
()
const
{
// TODO: implement me
return
{};
if
(
empty
())
return
message
{};
using
namespace
detail
;
auto
vptr
=
malloc
(
sizeof
(
message_data
)
+
storage_size_
);
if
(
vptr
==
nullptr
)
throw
std
::
bad_alloc
();
auto
raw_ptr
=
new
(
vptr
)
message_data
(
types_
.
copy_to_list
());
intrusive_cow_ptr
<
message_data
>
ptr
{
raw_ptr
,
false
};
auto
storage
=
raw_ptr
->
storage
();
for
(
auto
&
element
:
elements_
)
storage
=
element
->
copy_init
(
storage
);
return
message
{
std
::
move
(
ptr
)};
}
message
message_builder
::
move_to_message
()
{
// TODO: implement me
return
{};
if
(
empty
())
return
message
{};
using
namespace
detail
;
auto
vptr
=
malloc
(
sizeof
(
message_data
)
+
storage_size_
);
if
(
vptr
==
nullptr
)
throw
std
::
bad_alloc
();
auto
raw_ptr
=
new
(
vptr
)
message_data
(
types_
.
move_to_list
());
intrusive_cow_ptr
<
message_data
>
ptr
{
raw_ptr
,
false
};
auto
storage
=
raw_ptr
->
storage
();
for
(
auto
&
element
:
elements_
)
storage
=
element
->
move_init
(
storage
);
return
message
{
std
::
move
(
ptr
)};
}
}
// namespace caf
libcaf_core/test/message_builder.cpp
0 → 100644
View file @
3d4450f7
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2018 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#define CAF_SUITE message_builder
#include "caf/message_builder.hpp"
#include "core-test.hpp"
#include <map>
#include <numeric>
#include <string>
#include <vector>
#include "caf/message.hpp"
#include "caf/type_id_list.hpp"
using
namespace
caf
;
#define STEP(message) \
CAF_MESSAGE(message); \
if (true)
CAF_TEST
(
message
builder
can
build
messages
incrermenetally
)
{
message_builder
builder
;
CAF_CHECK
(
builder
.
empty
());
CAF_CHECK
(
builder
.
to_message
().
empty
());
CAF_CHECK_EQUAL
(
builder
.
size
(),
0u
);
STEP
(
"after adding 1, the message is (1)"
)
{
builder
.
append
(
int32_t
{
1
});
auto
msg
=
builder
.
to_message
();
CAF_CHECK_EQUAL
(
builder
.
size
(),
1u
);
CAF_CHECK_EQUAL
(
msg
.
types
(),
make_type_id_list
<
int32_t
>
());
CAF_CHECK_EQUAL
(
to_string
(
msg
),
"(1)"
);
}
STEP
(
"after adding [2, 3], the message is (1, 2, 3)"
)
{
std
::
vector
<
int32_t
>
xs
{
2
,
3
};
builder
.
append
(
xs
.
begin
(),
xs
.
end
());
CAF_CHECK_EQUAL
(
builder
.
size
(),
3u
);
auto
msg
=
builder
.
to_message
();
CAF_CHECK_EQUAL
(
msg
.
types
(),
(
make_type_id_list
<
int32_t
,
int32_t
,
int32_t
>
()));
CAF_CHECK_EQUAL
(
to_string
(
msg
),
"(1, 2, 3)"
);
}
STEP
(
"moving the content to a message produces the same message again"
)
{
auto
msg
=
builder
.
move_to_message
();
CAF_CHECK_EQUAL
(
msg
.
types
(),
(
make_type_id_list
<
int32_t
,
int32_t
,
int32_t
>
()));
CAF_CHECK_EQUAL
(
to_string
(
msg
),
"(1, 2, 3)"
);
}
}
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