Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sequence() initializer to pass a runtime container of initializers. #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions boost/process/posix/initializers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <boost/process/posix/initializers/on_fork_setup.hpp>
#include <boost/process/posix/initializers/on_fork_success.hpp>
#include <boost/process/posix/initializers/run_exe.hpp>
#include <boost/process/posix/initializers/sequence.hpp>
#include <boost/process/posix/initializers/set_args.hpp>
#include <boost/process/posix/initializers/set_cmd_line.hpp>
#include <boost/process/posix/initializers/set_env.hpp>
Expand Down
106 changes: 106 additions & 0 deletions boost/process/posix/initializers/sequence.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
// Copyright (c) 2009 Boris Schaeling
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
// Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef BOOST_PROCESS_POSIX_INITIALIZERS_SEQUENCE_HPP
#define BOOST_PROCESS_POSIX_INITIALIZERS_SEQUENCE_HPP

#include <boost/process/posix/initializers/initializer_base.hpp>
#include <boost/type_erasure/any.hpp>
#include <boost/type_erasure/member.hpp>
#include <boost/type_erasure/relaxed.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/range/algorithm/for_each.hpp>

// Define initializer_base methods to Boost.TypeErasure. The convenience macro
// must be used in the global namespace.
BOOST_TYPE_ERASURE_MEMBER((boost)(process)(posix)(initializers)(has_on_fork_setup),
on_fork_setup, 1)
BOOST_TYPE_ERASURE_MEMBER((boost)(process)(posix)(initializers)(has_on_fork_error),
on_fork_error, 1)
BOOST_TYPE_ERASURE_MEMBER((boost)(process)(posix)(initializers)(has_on_fork_success),
on_fork_success, 1)
BOOST_TYPE_ERASURE_MEMBER((boost)(process)(posix)(initializers)(has_on_exec_setup),
on_exec_setup, 1)
BOOST_TYPE_ERASURE_MEMBER((boost)(process)(posix)(initializers)(has_on_exec_error),
on_exec_error, 1)

namespace boost { namespace process { namespace posix { namespace initializers {

// any_initializer should be able to store any subclass of initializer_base.
// We use Boost.TypeErasure because initializer_base is not itself
// polymorphic. This constrains the methods to accept specifically executor&
// rather than a template argument, but that should only be a problem for a
// user-coded initializer that expects an executor subclass. If such an
// initializer is stored in any_initializer, you might have to static_cast the
// argument to your executor type.
typedef
boost::type_erasure::any<
boost::mpl::vector<
boost::type_erasure::copy_constructible<>,
has_on_fork_setup <void(executor&), const boost::type_erasure::_self>,
has_on_fork_error <void(executor&), const boost::type_erasure::_self>,
has_on_fork_success<void(executor&), const boost::type_erasure::_self>,
has_on_exec_setup <void(executor&), const boost::type_erasure::_self>,
has_on_exec_error <void(executor&), const boost::type_erasure::_self>,
boost::type_erasure::relaxed> >
any_initializer;

// sequence_wrapper_ adapts any copyable runtime container acceptable to
// boost::range::for_each() -- e.g. std::vector<any_initializer> -- for use as
// an execute() initializer.
template <typename Sequence>
struct sequence_wrapper_: public initializer_base
{
sequence_wrapper_(const Sequence& c): sequence_(c) {}

Sequence sequence_;

template <class PosixExecutor>
void on_fork_setup(PosixExecutor& e) const
{
boost::range::for_each(sequence_, typename PosixExecutor::call_on_fork_setup(e));
}

template <class PosixExecutor>
void on_fork_error(PosixExecutor& e) const
{
boost::range::for_each(sequence_, typename PosixExecutor::call_on_fork_error(e));
}

template <class PosixExecutor>
void on_fork_success(PosixExecutor& e) const
{
boost::range::for_each(sequence_, typename PosixExecutor::call_on_fork_success(e));
}

template <class PosixExecutor>
void on_exec_setup(PosixExecutor& e) const
{
boost::range::for_each(sequence_, typename PosixExecutor::call_on_exec_setup(e));
}

template <class PosixExecutor>
void on_exec_error(PosixExecutor& e) const
{
boost::range::for_each(sequence_, typename PosixExecutor::call_on_exec_error(e));
}
};

// sequence() is a convenience function to construct a sequence_wrapper_<T>
// without having to state T.
template <typename Sequence>
sequence_wrapper_<Sequence>
sequence(const Sequence& c)
{
return sequence_wrapper_<Sequence>(c);
}

}}}}

#endif
1 change: 1 addition & 0 deletions boost/process/windows/initializers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <boost/process/windows/initializers/on_CreateProcess_setup.hpp>
#include <boost/process/windows/initializers/on_CreateProcess_success.hpp>
#include <boost/process/windows/initializers/run_exe.hpp>
#include <boost/process/windows/initializers/sequence.hpp>
#include <boost/process/windows/initializers/set_args.hpp>
#include <boost/process/windows/initializers/set_cmd_line.hpp>
#include <boost/process/windows/initializers/set_env.hpp>
Expand Down
88 changes: 88 additions & 0 deletions boost/process/windows/initializers/sequence.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) 2006, 2007 Julio M. Merino Vidal
// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
// Copyright (c) 2009 Boris Schaeling
// Copyright (c) 2010 Felipe Tanus, Boris Schaeling
// Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef BOOST_PROCESS_WINDOWS_INITIALIZERS_SEQUENCE_HPP
#define BOOST_PROCESS_WINDOWS_INITIALIZERS_SEQUENCE_HPP

#include <boost/process/windows/initializers/initializer_base.hpp>
#include <boost/type_erasure/any.hpp>
#include <boost/type_erasure/member.hpp>
#include <boost/type_erasure/relaxed.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/range/algorithm/for_each.hpp>

// Define initializer_base methods to Boost.TypeErasure. The convenience macro
// must be used in the global namespace.
BOOST_TYPE_ERASURE_MEMBER((boost)(process)(windows)(initializers)(has_on_CreateProcess_setup),
on_CreateProcess_setup, 1)
BOOST_TYPE_ERASURE_MEMBER((boost)(process)(windows)(initializers)(has_on_CreateProcess_error),
on_CreateProcess_error, 1)
BOOST_TYPE_ERASURE_MEMBER((boost)(process)(windows)(initializers)(has_on_CreateProcess_success),
on_CreateProcess_success, 1)

namespace boost { namespace process { namespace windows { namespace initializers {

// any_initializer should be able to store any subclass of initializer_base.
// We use Boost.TypeErasure because initializer_base is not itself
// polymorphic. This constrains the methods to accept specifically executor&
// rather than a template argument, but that should only be a problem for a
// user-coded initializer that expects an executor subclass. If such an
// initializer is stored in any_initializer, you might have to static_cast the
// argument to your executor type.
typedef
boost::type_erasure::any<
boost::mpl::vector<
boost::type_erasure::copy_constructible<>,
has_on_CreateProcess_setup <void(executor&), const boost::type_erasure::_self>,
has_on_CreateProcess_error <void(executor&), const boost::type_erasure::_self>,
has_on_CreateProcess_success<void(executor&), const boost::type_erasure::_self>,
boost::type_erasure::relaxed> >
any_initializer;

// sequence_wrapper_ adapts any copyable runtime container acceptable to
// boost::range::for_each() -- e.g. std::vector<any_initializer> -- for use as
// an execute() initializer.
template <typename Sequence>
struct sequence_wrapper_: public initializer_base
{
sequence_wrapper_(const Sequence& c): sequence_(c) {}

Sequence sequence_;

template <class WindowsExecutor>
void on_CreateProcess_setup(WindowsExecutor& e) const
{
boost::range::for_each(sequence_, typename WindowsExecutor::call_on_CreateProcess_setup(e));
}

template <class WindowsExecutor>
void on_CreateProcess_error(WindowsExecutor& e) const
{
boost::range::for_each(sequence_, typename WindowsExecutor::call_on_CreateProcess_error(e));
}

template <class WindowsExecutor>
void on_CreateProcess_success(WindowsExecutor& e) const
{
boost::range::for_each(sequence_, typename WindowsExecutor::call_on_CreateProcess_success(e));
}
};

// sequence() is a convenience function to construct a sequence_wrapper_<T>
// without having to state T.
template <typename Sequence>
sequence_wrapper_<Sequence>
sequence(const Sequence& c)
{
return sequence_wrapper_<Sequence>(c);
}

}}}}

#endif