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
12224897
Commit
12224897
authored
Nov 04, 2011
by
neverlord
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed_vector for pattern matching
parent
52331a6c
Changes
10
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
213 additions
and
94 deletions
+213
-94
Makefile.am
Makefile.am
+1
-1
cppa.files
cppa.files
+1
-1
cppa/detail/decorated_tuple.hpp
cppa/detail/decorated_tuple.hpp
+5
-3
cppa/detail/invokable.hpp
cppa/detail/invokable.hpp
+8
-4
cppa/detail/pattern_details.hpp
cppa/detail/pattern_details.hpp
+61
-4
cppa/get_view.hpp
cppa/get_view.hpp
+3
-3
cppa/pattern.hpp
cppa/pattern.hpp
+8
-2
cppa/tuple_view.hpp
cppa/tuple_view.hpp
+11
-16
cppa/util/fixed_vector.hpp
cppa/util/fixed_vector.hpp
+115
-0
src/pattern.cpp
src/pattern.cpp
+0
-60
No files found.
Makefile.am
View file @
12224897
...
...
@@ -42,7 +42,6 @@ libcppa_la_SOURCES = \
src/native_socket.cpp
\
src/network_manager.cpp
\
src/object_array.cpp
\
src/pattern.cpp
\
src/object.cpp
\
src/post_office.cpp
\
src/post_office_msg.cpp
\
...
...
@@ -186,6 +185,7 @@ nobase_library_include_HEADERS = \
cppa/util/fiber.hpp
\
cppa/util/filter_type_list.hpp
\
cppa/util/first_n.hpp
\
cppa/util/fixed_vector.hpp
\
cppa/util/has_copy_member_fun.hpp
\
cppa/util.hpp
\
cppa/util/if_else.hpp
\
...
...
cppa.files
View file @
12224897
...
...
@@ -229,6 +229,6 @@ src/addressed_message.cpp
unit_testing/test__yield_interface.cpp
cppa/anything.hpp
cppa/pattern.hpp
src/pattern.cpp
cppa/detail/pattern_details.hpp
unit_testing/test__pattern.cpp
cppa/util/fixed_vector.hpp
cppa/detail/decorated_tuple.hpp
View file @
12224897
...
...
@@ -8,6 +8,7 @@
#include "cppa/uniform_type_info.hpp"
#include "cppa/util/type_list.hpp"
#include "cppa/util/fixed_vector.hpp"
#include "cppa/detail/abstract_tuple.hpp"
#include "cppa/detail/serialize_tuple.hpp"
...
...
@@ -20,12 +21,13 @@ class decorated_tuple : public abstract_tuple
public:
typedef
util
::
fixed_vector
<
size_t
,
sizeof
...(
ElementTypes
)
>
vector_type
;
typedef
util
::
type_list
<
ElementTypes
...
>
element_types
;
typedef
cow_ptr
<
abstract_tuple
>
ptr_type
;
decorated_tuple
(
const
ptr_type
&
d
,
const
std
::
vector
<
size_t
>&
v
)
decorated_tuple
(
const
ptr_type
&
d
,
const
vector_type
&
v
)
:
m_decorated
(
d
),
m_mappings
(
v
)
{
}
...
...
@@ -68,7 +70,7 @@ class decorated_tuple : public abstract_tuple
private:
ptr_type
m_decorated
;
std
::
vector
<
size_t
>
m_mappings
;
vector_type
m_mappings
;
element_types
m_types
;
decorated_tuple
(
const
decorated_tuple
&
other
)
...
...
cppa/detail/invokable.hpp
View file @
12224897
...
...
@@ -8,6 +8,7 @@
#include "cppa/invoke.hpp"
#include "cppa/any_tuple.hpp"
#include "cppa/util/duration.hpp"
#include "cppa/util/fixed_vector.hpp"
#include "cppa/detail/intermediate.hpp"
// forward declaration
...
...
@@ -100,6 +101,8 @@ class invokable_impl : public invokable
};
typedef
util
::
fixed_vector
<
size_t
,
TupleView
::
num_elements
>
vector_type
;
std
::
unique_ptr
<
Pattern
>
m_pattern
;
TargetFun
m_target
;
iimpl
m_iimpl
;
...
...
@@ -113,7 +116,8 @@ class invokable_impl : public invokable
bool
invoke
(
const
any_tuple
&
data
)
const
{
std
::
vector
<
size_t
>
mv
;
vector_type
mv
;
if
((
*
m_pattern
)(
data
,
&
mv
))
{
if
(
mv
.
size
()
==
data
.
size
())
...
...
@@ -125,7 +129,7 @@ class invokable_impl : public invokable
else
{
// mapping needed
TupleView
tv
(
data
.
vals
(),
std
::
move
(
mv
)
);
TupleView
tv
(
data
.
vals
(),
mv
);
cppa
::
invoke
(
m_target
,
tv
);
}
return
true
;
...
...
@@ -135,7 +139,7 @@ class invokable_impl : public invokable
intermediate
*
get_intermediate
(
const
any_tuple
&
data
)
{
std
::
vector
<
size_t
>
mv
;
vector_type
mv
;
if
((
*
m_pattern
)(
data
,
&
mv
))
{
if
(
mv
.
size
()
==
data
.
size
())
...
...
@@ -145,7 +149,7 @@ class invokable_impl : public invokable
}
else
{
m_iimpl
.
m_args
=
TupleView
(
data
.
vals
(),
std
::
move
(
mv
)
);
m_iimpl
.
m_args
=
TupleView
(
data
.
vals
(),
mv
);
}
return
&
m_iimpl
;
}
...
...
cppa/detail/pattern_details.hpp
View file @
12224897
...
...
@@ -137,20 +137,23 @@ struct pattern_arg
};
template
<
typename
VectorType
>
struct
tuple_iterator_arg
{
typedef
VectorType
vector_type
;
util
::
any_tuple_iterator
iter
;
std
::
vector
<
size_t
>
*
mapping
;
vector_type
*
mapping
;
inline
tuple_iterator_arg
(
const
any_tuple
&
tup
,
std
::
vector
<
size_t
>
*
mv
=
nullptr
)
vector_type
*
mv
=
nullptr
)
:
iter
(
tup
),
mapping
(
mv
)
{
}
inline
tuple_iterator_arg
(
const
util
::
any_tuple_iterator
&
from_iter
,
std
::
vector
<
size_t
>
*
mv
=
nullptr
)
vector_type
*
mv
=
nullptr
)
:
iter
(
from_iter
),
mapping
(
mv
)
{
}
...
...
@@ -181,7 +184,61 @@ struct tuple_iterator_arg
};
bool
do_match
(
pattern_arg
&
ty_args
,
tuple_iterator_arg
&
tu_args
);
template
<
typename
VectorType
>
bool
do_match
(
pattern_arg
&
pargs
,
tuple_iterator_arg
<
VectorType
>&
targs
)
{
if
(
pargs
.
at_end
()
&&
targs
.
at_end
())
{
return
true
;
}
if
(
pargs
.
type
()
==
nullptr
)
// nullptr == wildcard
{
// perform submatching
pargs
.
next
();
if
(
pargs
.
at_end
())
{
// always true at the end of the pattern
return
true
;
}
auto
pargs_copy
=
pargs
;
VectorType
mv
;
auto
mv_ptr
=
(
targs
.
mapping
)
?
&
mv
:
nullptr
;
// iterate over tu_args until we found a match
while
(
targs
.
at_end
()
==
false
)
{
tuple_iterator_arg
<
VectorType
>
targs_copy
(
targs
.
iter
,
mv_ptr
);
if
(
do_match
(
pargs_copy
,
targs_copy
))
{
if
(
mv_ptr
)
{
targs
.
mapping
->
insert
(
targs
.
mapping
->
end
(),
mv
.
begin
(),
mv
.
end
());
}
return
true
;
}
// next iteration
mv
.
clear
();
targs
.
next
();
}
}
else
{
// compare types
if
(
targs
.
at_end
()
==
false
&&
pargs
.
type
()
==
targs
.
type
())
{
// compare values if needed
if
(
pargs
.
has_value
()
==
false
||
pargs
.
type
()
->
equal
(
pargs
.
value
(),
targs
.
value
()))
{
targs
.
push_mapping
();
// submatch
return
do_match
(
pargs
.
next
(),
targs
.
next
());
}
}
}
return
false
;
}
}
}
// namespace cppa::detail
...
...
cppa/get_view.hpp
View file @
12224897
...
...
@@ -17,11 +17,11 @@ template<typename... MatchRules>
typename
tuple_view_type_from_type_list
<
typename
util
::
filter_type_list
<
anything
,
util
::
type_list
<
MatchRules
...
>>::
type
>::
type
get_view
(
const
any_tuple
&
ut
)
{
std
::
vector
<
size_t
>
mappings
;
pattern
<
MatchRules
...
>
p
;
if
(
p
(
ut
,
&
mappings
))
typename
pattern
<
MatchRules
...
>::
mapping_vector
mapping
;
if
(
p
(
ut
,
&
mapping
))
{
return
{
ut
.
vals
(),
std
::
move
(
mappings
)
};
return
{
ut
.
vals
(),
mapping
};
}
// todo: throw nicer exception
throw
std
::
runtime_error
(
"doesn't match"
);
...
...
cppa/pattern.hpp
View file @
12224897
...
...
@@ -33,6 +33,11 @@ class pattern<T0, Tn...>
typedef
typename
util
::
filter_type_list
<
anything
,
tpl_args
>::
type
filtered_tpl_args
;
typedef
typename
tuple_view_type_from_type_list
<
filtered_tpl_args
>::
type
tuple_view_type
;
typedef
typename
tuple_view_type
::
mapping_vector
mapping_vector
;
//typedef typename detail::tdata_from_type_list<filtered_tpl_args>::type
// data_type;
...
...
@@ -54,11 +59,12 @@ class pattern<T0, Tn...>
m_utis
,
m_data_ptr
);
}
// todo: calculate expected vector type
bool
operator
()(
const
cppa
::
any_tuple
&
msg
,
std
::
vector
<
size_t
>
*
mapping
=
nullptr
)
const
mapping_vector
*
mapping
=
nullptr
)
const
{
detail
::
pattern_arg
arg0
(
size
,
m_data_ptr
,
m_utis
);
detail
::
tuple_iterator_arg
arg1
(
msg
,
mapping
);
detail
::
tuple_iterator_arg
<
mapping_vector
>
arg1
(
msg
,
mapping
);
return
detail
::
do_match
(
arg0
,
arg1
);
}
...
...
cppa/tuple_view.hpp
View file @
12224897
...
...
@@ -9,6 +9,7 @@
#include "cppa/util/at.hpp"
#include "cppa/util/type_list.hpp"
#include "cppa/util/a_matches_b.hpp"
#include "cppa/util/fixed_vector.hpp"
#include "cppa/util/compare_tuples.hpp"
#include "cppa/detail/decorated_tuple.hpp"
...
...
@@ -25,21 +26,19 @@ template<typename... ElementTypes>
class
tuple_view
{
//static_assert(sizeof...(ElementTypes) > 0,
// "could not declare an empty tuple_view");
template
<
size_t
N
,
typename
...
Types
>
friend
typename
util
::
at
<
N
,
Types
...
>::
type
&
get_ref
(
tuple_view
<
Types
...
>&
);
public:
typedef
util
::
type_list
<
ElementTypes
...
>
element_types
;
// enable use of tuple_view as type_list
typedef
typename
element_types
::
head_type
head_type
;
typedef
typename
element_types
::
tail_type
tail_type
;
typedef
cow_ptr
<
detail
::
abstract_tuple
>
vals_t
;
static_assert
(
sizeof
...(
ElementTypes
)
>
0
,
"could not declare an empty tuple_view"
);
static
constexpr
size_t
num_elements
=
sizeof
...(
ElementTypes
);
typedef
cow_ptr
<
detail
::
abstract_tuple
>
vals_t
;
typedef
util
::
fixed_vector
<
size_t
,
num_elements
>
mapping_vector
;
tuple_view
()
:
m_vals
(
tuple
<
ElementTypes
...
>
().
vals
())
{
}
...
...
@@ -53,8 +52,8 @@ class tuple_view
return
tuple_view
(
std
::
move
(
vals
));
}
tuple_view
(
const
vals_t
&
vals
,
std
::
vector
<
size_t
>&&
mappings
)
:
m_vals
(
new
detail
::
decorated_tuple
<
ElementTypes
...
>
(
vals
,
mapping
s
))
tuple_view
(
const
vals_t
&
vals
,
mapping_vector
&
mapping
)
:
m_vals
(
new
detail
::
decorated_tuple
<
ElementTypes
...
>
(
vals
,
mapping
))
{
}
...
...
@@ -81,14 +80,10 @@ class tuple_view
return
m_vals
;
}
/*inline const element_types& types() const
{
//return m_types;
}*/
inline
size_t
size
()
const
{
return
m_vals
->
size
();
return
sizeof
...(
ElementTypes
);
//return m_vals->size();
}
private:
...
...
cppa/util/fixed_vector.hpp
0 → 100644
View file @
12224897
#ifndef FIXED_VECTOR_HPP
#define FIXED_VECTOR_HPP
#include <algorithm>
#include <stdexcept>
namespace
cppa
{
namespace
util
{
// @warning does not perform any bound checks
template
<
typename
T
,
size_t
MaxSize
>
class
fixed_vector
{
size_t
m_size
;
// support for MaxSize == 0
T
m_data
[(
MaxSize
>
0
)
?
MaxSize
:
1
];
public:
typedef
size_t
size_type
;
typedef
T
&
reference
;
typedef
const
T
&
const_reference
;
typedef
T
*
iterator
;
typedef
const
T
*
const_iterator
;
constexpr
fixed_vector
()
:
m_size
(
0
)
{
}
fixed_vector
(
const
fixed_vector
&
other
)
:
m_size
(
other
.
m_size
)
{
std
::
copy
(
other
.
m_data
,
other
.
m_data
+
other
.
m_size
,
m_data
);
}
inline
size_type
size
()
const
{
return
m_size
;
}
inline
void
clear
()
{
m_size
=
0
;
}
inline
bool
full
()
const
{
return
m_size
==
MaxSize
;
}
inline
void
push_back
(
const
T
&
what
)
{
m_data
[
m_size
++
]
=
what
;
}
inline
void
push_back
(
T
&&
what
)
{
m_data
[
m_size
++
]
=
std
::
move
(
what
);
}
inline
reference
operator
[](
size_t
pos
)
{
return
m_data
[
pos
];
}
inline
const_reference
operator
[](
size_t
pos
)
const
{
return
m_data
[
pos
];
}
inline
iterator
begin
()
{
return
m_data
;
}
inline
const_iterator
begin
()
const
{
return
m_data
;
}
inline
iterator
end
()
{
return
(
static_cast
<
T
*>
(
m_data
)
+
m_size
);
}
inline
const_iterator
end
()
const
{
return
(
static_cast
<
T
*>
(
m_data
)
+
m_size
);
}
template
<
class
InputIterator
>
inline
void
insert
(
iterator
pos
,
InputIterator
first
,
InputIterator
last
)
{
if
(
pos
==
end
())
{
for
(
InputIterator
i
=
first
;
i
!=
last
;
++
i
)
{
push_back
(
*
i
);
}
}
else
{
throw
std
::
runtime_error
(
"not implemented fixed_vector::insert"
);
}
}
};
}
}
// namespace cppa::util
#endif // FIXED_VECTOR_HPP
src/pattern.cpp
deleted
100644 → 0
View file @
52331a6c
#include "cppa/pattern.hpp"
namespace
cppa
{
namespace
detail
{
bool
do_match
(
pattern_arg
&
pargs
,
tuple_iterator_arg
&
targs
)
{
if
(
pargs
.
at_end
()
&&
targs
.
at_end
())
{
return
true
;
}
if
(
pargs
.
type
()
==
nullptr
)
// nullptr == wildcard
{
// perform submatching
pargs
.
next
();
if
(
pargs
.
at_end
())
{
// always true at the end of the pattern
return
true
;
}
auto
pargs_copy
=
pargs
;
std
::
vector
<
size_t
>
mv
;
auto
mv_ptr
=
(
targs
.
mapping
)
?
&
mv
:
nullptr
;
// iterate over tu_args until we found a match
while
(
targs
.
at_end
()
==
false
)
{
tuple_iterator_arg
targs_copy
(
targs
.
iter
,
mv_ptr
);
if
(
do_match
(
pargs_copy
,
targs_copy
))
{
if
(
mv_ptr
)
{
targs
.
mapping
->
insert
(
targs
.
mapping
->
end
(),
mv
.
begin
(),
mv
.
end
());
}
return
true
;
}
// next iteration
mv
.
clear
();
targs
.
next
();
}
}
else
{
// compare types
if
(
targs
.
at_end
()
==
false
&&
pargs
.
type
()
==
targs
.
type
())
{
// compare values if needed
if
(
pargs
.
has_value
()
==
false
||
pargs
.
type
()
->
equal
(
pargs
.
value
(),
targs
.
value
()))
{
targs
.
push_mapping
();
// submatch
return
do_match
(
pargs
.
next
(),
targs
.
next
());
}
}
}
return
false
;
}
}
}
// namespace cppa::detail
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