Commit bea4b168 authored by Dominik Charousset's avatar Dominik Charousset

Make `ref_counted` compatible to boost-style ptrs

parent d71bfc8c
/******************************************************************************\
* ___ __ *
* /\_ \ __/\ \ *
* \//\ \ /\_\ \ \____ ___ _____ _____ __ *
* \ \ \ \/\ \ \ '__`\ /'___\/\ '__`\/\ '__`\ /'__`\ *
* \_\ \_\ \ \ \ \L\ \/\ \__/\ \ \L\ \ \ \L\ \/\ \L\.\_ *
* /\____\\ \_\ \_,__/\ \____\\ \ ,__/\ \ ,__/\ \__/.\_\ *
* \/____/ \/_/\/___/ \/____/ \ \ \/ \ \ \/ \/__/\/_/ *
* \ \_\ \ \_\ *
* \/_/ \/_/ *
* *
* Copyright (C) 2011 - 2014 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* *
* Distributed under the Boost Software License, Version 1.0. See *
* accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt *
\******************************************************************************/
#ifndef CPPA_DETAIL_MAKE_COUNTED_HPP
#define CPPA_DETAIL_MAKE_COUNTED_HPP
#include <type_traits>
#include "cppa/intrusive_ptr.hpp"
#include "cppa/ref_counted.hpp"
#include "cppa/detail/memory.hpp"
namespace cppa {
namespace detail {
template<typename T, typename... Ts>
typename std::enable_if<is_memory_cached<T>::value, intrusive_ptr<T>>::type
make_counted(Ts&&... args) {
return {memory::create<T>(std::forward<Ts>(args)...)};
}
template<typename T, typename... Ts>
typename std::enable_if<!is_memory_cached<T>::value, intrusive_ptr<T>>::type
make_counted(Ts&&... args) {
return {new T(std::forward<Ts>(args)...)};
}
} // namespace detail
} // namespace cppa
#endif // CPPA_DETAIL_MAKE_COUNTED_HPP
......@@ -25,18 +25,10 @@
#include <stdexcept>
#include <type_traits>
#include "cppa/memory_cached.hpp"
#include "cppa/util/comparable.hpp"
namespace cppa {
template<class From, class To>
struct convertible {
inline To convert() const {
return static_cast<const From*>(this)->do_convert();
}
};
/**
* @brief An intrusive, reference counting smart pointer impelementation.
* @relates ref_counted
......@@ -68,12 +60,6 @@ class intrusive_ptr : util::comparable<intrusive_ptr<T> >,
"Y* is not assignable to T*");
}
// enables "actor_ptr s = self"
template<typename From>
intrusive_ptr(const convertible<From, pointer>& from) {
set_ptr(from.convert());
}
~intrusive_ptr() { if (m_ptr) m_ptr->deref(); }
inline void swap(intrusive_ptr& other) {
......@@ -131,12 +117,6 @@ class intrusive_ptr : util::comparable<intrusive_ptr<T> >,
return *this;
}
template<typename From>
intrusive_ptr& operator=(const convertible<From, T*>& from) {
reset(from.convert());
return *this;
}
inline pointer get() const { return m_ptr; }
inline pointer operator->() const { return m_ptr; }
inline reference operator*() const { return *m_ptr; }
......@@ -193,22 +173,6 @@ inline bool operator!=(const intrusive_ptr<X>& lhs, const intrusive_ptr<Y>& rhs)
return !(lhs == rhs);
}
/**
* @brief Constructs an object of type T which must be a derived type
* of {@link ref_counted} and wraps it in an {@link intrusive_ptr}.
*/
template<typename T, typename... Ts>
typename std::enable_if<is_memory_cached<T>::value, intrusive_ptr<T>>::type
make_counted(Ts&&... args) {
return {detail::memory::create<T>(std::forward<Ts>(args)...)};
}
template<typename T, typename... Ts>
typename std::enable_if<not is_memory_cached<T>::value, intrusive_ptr<T>>::type
make_counted(Ts&&... args) {
return {new T(std::forward<Ts>(args)...)};
}
} // namespace cppa
#endif // CPPA_INTRUSIVE_PTR_HPP
......@@ -47,6 +47,8 @@
#include "cppa/util/type_list.hpp"
#include "cppa/util/limited_vector.hpp"
#include "cppa/detail/make_counted.hpp"
#include "cppa/opencl/global.hpp"
#include "cppa/opencl/command.hpp"
#include "cppa/opencl/program.hpp"
......@@ -157,7 +159,7 @@ class actor_facade<Ret(Args...)> : public abstract_actor {
args_vec arguments;
add_arguments_to_kernel<Ret>(events, arguments, m_result_size,
get_ref<Is>(*opt)...);
auto cmd = make_counted<command<actor_facade, Ret>>(
auto cmd = detail::make_counted<command<actor_facade, Ret>>(
handle, this,
std::move(events), std::move(arguments),
m_result_size, *opt
......
......@@ -66,6 +66,16 @@ class ref_counted : public memory_managed {
};
// compatibility with the intrusive_ptr implementation of boost
inline void intrusive_ptr_add_ref(ref_counted* p) {
p->ref();
}
// compatibility with the intrusive_ptr implementation of boost
inline void intrusive_ptr_release(ref_counted* p) {
p->deref();
}
} // namespace cppa
#endif // CPPA_REF_COUNTED_HPP
......@@ -32,6 +32,7 @@
#include "cppa/detail/cs_thread.hpp"
#include "cppa/util/type_traits.hpp"
#include "cppa/detail/make_counted.hpp"
#include "cppa/detail/proper_actor.hpp"
#include "cppa/detail/typed_actor_util.hpp"
......
......@@ -31,6 +31,7 @@
#include "cppa/io/middleman.hpp"
#include "cppa/io/buffered_writing.hpp"
#include "cppa/detail/make_counted.hpp"
#include "cppa/detail/actor_registry.hpp"
#include "cppa/detail/sync_request_bouncer.hpp"
......@@ -446,7 +447,7 @@ broker_ptr init_and_launch(broker_ptr ptr) {
}
broker_ptr broker::from_impl(std::function<behavior (broker*)> fun) {
return make_counted<default_broker>(fun);
return detail::make_counted<default_broker>(fun);
}
broker_ptr broker::from_impl(std::function<void (broker*)> fun) {
......@@ -504,7 +505,7 @@ actor broker::fork_impl(std::function<behavior (broker*)> fun,
throw std::invalid_argument("invalid handle");
}
scribe* sptr = i->second.get(); // keep non-owning pointer
auto result = make_counted<default_broker>(fun);
auto result = detail::make_counted<default_broker>(fun);
result->m_io.insert(make_pair(sptr->id(), move(i->second)));
init_and_launch(result);
sptr->set_broker(result); // set new broker
......
......@@ -37,6 +37,7 @@
#include "cppa/detail/raw_access.hpp"
#include "cppa/detail/types_array.hpp"
#include "cppa/detail/make_counted.hpp"
#include "cppa/detail/group_manager.hpp"
#include "cppa/util/shared_spinlock.hpp"
......@@ -290,7 +291,7 @@ class local_group_module : public abstract_group::module {
return {i->second};
}
else {
auto tmp = make_counted<local_group>(true, this, identifier);
auto tmp = detail::make_counted<local_group>(true, this, identifier);
{ // lifetime scope of uguard
upgrade_guard uguard(guard);
auto p = m_instances.insert(make_pair(identifier, tmp));
......@@ -450,7 +451,7 @@ class remote_group_module : public abstract_group::module {
public:
remote_group_module() : super("remote") {
auto sm = make_counted<shared_map>();
auto sm = detail::make_counted<shared_map>();
abstract_group::module_ptr this_group{this};
m_map = sm;
typedef map<string, pair<actor, vector<pair<string, remote_group_ptr>>>>
......@@ -494,7 +495,7 @@ class remote_group_module : public abstract_group::module {
on(atom("GROUP"), arg_match) >> [=](const group& g) {
auto gg = dynamic_cast<local_group*>(detail::raw_access::get(g));
if (gg) {
auto rg = make_counted<remote_group>(this_group, key, gg);
auto rg = detail::make_counted<remote_group>(this_group, key, gg);
sm->put(key, rg);
(*peers)[authority].second.push_back(make_pair(key, rg));
}
......
......@@ -54,6 +54,7 @@
#include "cppa/io/middleman_event_handler.hpp"
#include "cppa/detail/fd_util.hpp"
#include "cppa/detail/make_counted.hpp"
#include "cppa/detail/actor_registry.hpp"
#include "cppa/intrusive/single_reader_queue.hpp"
......@@ -288,7 +289,7 @@ class middleman_impl : public middleman {
m_node = compute_node_id();
m_handler = middleman_event_handler::create();
m_namespace.set_proxy_factory([=](actor_id aid, node_id_ptr ptr) {
return make_counted<remote_actor_proxy>(aid, std::move(ptr), this);
return detail::make_counted<remote_actor_proxy>(aid, std::move(ptr), this);
});
m_namespace.set_new_element_callback([=](actor_id aid,
const node_id& node) {
......
......@@ -40,6 +40,7 @@
#include "cppa/binary_deserializer.hpp"
#include "cppa/detail/raw_access.hpp"
#include "cppa/detail/make_counted.hpp"
#include "cppa/intrusive/single_reader_queue.hpp"
......@@ -178,7 +179,7 @@ abstract_actor_ptr remote_actor_impl(stream_ptr_pair io, string_set expected) {
" but found a strongly typed actor of type "
+ iface_str);
}
auto pinfptr = make_counted<node_id>(peer_pid, peer_node_id);
auto pinfptr = detail::make_counted<node_id>(peer_pid, peer_node_id);
if (*pinf == *pinfptr) {
// this is a local actor, not a remote actor
CPPA_LOGF_INFO("remote_actor() called to access a local actor");
......
......@@ -4,9 +4,12 @@
#include "test.hpp"
#include "cppa/ref_counted.hpp"
#include "cppa/intrusive_ptr.hpp"
#include "cppa/detail/make_counted.hpp"
using namespace cppa;
using detail::make_counted;
namespace {
int class0_instances = 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment