- // Move:
- args(args&& that) : argc_(that.argc_), argv_(that.argv_)
- {
- that.argc_ = 0;
- that.argv_ = nullptr;
- }
- args& operator=(args&& that)
- {
- this->argc_ = that.argc_;
- this->argv_ = that.argv_;
- that.argc_ = 0;
- that.argv_ = nullptr;
- return *this;
- }
-
- int argc() const { return argc_; }
- char** argv() { return argv_; }
- const char*const* argv() const { return argv_; }
- char* operator[](std::size_t i) { return argv_[i]; }
-};
-
-inline std::function<void()> wrap_main(
- xbt_main_func_t code, std::shared_ptr<simgrid::simix::args> args)
-{
- if (code) {
- return [=]() {
- code(args->argc(), args->argv());
- };
- }
- else return std::function<void()>();
-}
-
-inline
-std::function<void()> wrap_main(xbt_main_func_t code, simgrid::simix::args args)
-{
- if (code)
- return wrap_main(code, std::unique_ptr<simgrid::simix::args>(
- new simgrid::simix::args(std::move(args))));
- else return std::function<void()>();
-}
-
-inline
-std::function<void()> wrap_main(xbt_main_func_t code, int argc, const char*const* argv)
-{
- return wrap_main(code, simgrid::simix::args(argc, argv));
-}
-
-class Context;
-class ContextFactory;
-
-XBT_PUBLIC_CLASS ContextFactory {
-private:
- std::string name_;
-public:
-
- ContextFactory(std::string name) : name_(std::move(name)) {}
- virtual ~ContextFactory();
- virtual Context* create_context(std::function<void()> code,
- void_pfn_smxprocess_t cleanup, smx_process_t process) = 0;
-
- // Optional methods for attaching main() as a context:
-
- /** Creates a context from the current context of execution
- *
- * This will not work on all implementation of `ContextFactory`.
- */
- virtual Context* attach(void_pfn_smxprocess_t cleanup_func, smx_process_t process);
- virtual Context* create_maestro(std::function<void()> code, smx_process_t process);
-
- virtual void run_all() = 0;
- virtual Context* self();
- std::string const& name() const
- {
- return name_;
- }
-private:
- void declare_context(void* T, std::size_t size);
-protected:
- template<class T, class... Args>
- T* new_context(Args&&... args)
- {
- T* context = new T(std::forward<Args>(args)...);
- this->declare_context(context, sizeof(T));
- return context;
- }
-};
-
-XBT_PUBLIC_CLASS Context {
-private:
- std::function<void()> code_;
- void_pfn_smxprocess_t cleanup_func_ = nullptr;
- smx_process_t process_ = nullptr;
-public:
- bool iwannadie;
-public:
- Context(std::function<void()> code,
- void_pfn_smxprocess_t cleanup_func,
- smx_process_t process);
- void operator()()
- {
- code_();
- }
- bool has_code() const
- {
- return (bool) code_;
- }
- smx_process_t process()
- {
- return this->process_;
- }
- void set_cleanup(void_pfn_smxprocess_t cleanup)
- {
- cleanup_func_ = cleanup;
- }
-
- // Virtual methods
- virtual ~Context();
- virtual void stop();
- virtual void suspend() = 0;
-};
-
-XBT_PUBLIC_CLASS AttachContext : public Context {
-public:
-
- AttachContext(std::function<void()> code,
- void_pfn_smxprocess_t cleanup_func,
- smx_process_t process)
- : Context(std::move(code), cleanup_func, process)
- {}
-
- ~AttachContext();