File: exec_main.hpp

package info (click to toggle)
actor-framework 0.17.6-3.2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 9,008 kB
  • sloc: cpp: 77,684; sh: 674; python: 309; makefile: 13
file content (104 lines) | stat: -rw-r--r-- 4,326 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    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.                                      *
 ******************************************************************************/

#pragma once

#include "caf/actor_system.hpp"
#include "caf/actor_system_config.hpp"
#include "caf/detail/type_list.hpp"
#include "caf/detail/type_traits.hpp"

namespace caf {

template <class>
struct exec_main_helper;

template <>
struct exec_main_helper<detail::type_list<actor_system&>> {
  using config = actor_system_config;

  template <class F>
  void operator()(F& fun, actor_system& sys, config&) {
    fun(sys);
  }
};

template <class T>
struct exec_main_helper<detail::type_list<actor_system&, const T&>> {
  using config = T;

  template <class F>
  void operator()(F& fun, actor_system& sys, config& cfg) {
    fun(sys, cfg);
  }
};

template <class... Ts, class F = void (*)(actor_system&)>
int exec_main(F fun, int argc, char** argv,
              const char* config_file_name = "caf-application.ini") {
  using trait = typename detail::get_callable_trait<F>::type;
  using arg_types = typename trait::arg_types;
  static_assert(detail::tl_size<arg_types>::value == 1
                || detail::tl_size<arg_types>::value == 2,
                "main function must have one or two arguments");
  static_assert(std::is_same<
                  typename detail::tl_head<arg_types>::type,
                  actor_system&
                >::value,
                "main function must take actor_system& as first parameter");
  using arg2 = typename detail::tl_at<arg_types, 1>::type;
  using decayed_arg2 = typename std::decay<arg2>::type;
  static_assert(std::is_same<arg2, unit_t>::value
                || (std::is_base_of<actor_system_config, decayed_arg2>::value
                    && std::is_same<arg2, const decayed_arg2&>::value),
                "second parameter of main function must take a subtype of "
                "actor_system_config as const reference");
  using helper = exec_main_helper<typename trait::arg_types>;
  // Pass CLI options to config.
  typename helper::config cfg;
  if (auto err = cfg.parse(argc, argv, config_file_name)) {
    std::cerr << "error while parsing CLI and file options: "
              << actor_system_config::render(err) << std::endl;
    return EXIT_FAILURE;
  }
  // Return immediately if a help text was printed.
  if (cfg.cli_helptext_printed)
    return EXIT_SUCCESS;
  // Load modules.
  std::initializer_list<unit_t> unused{unit_t{cfg.template load<Ts>()}...};
  CAF_IGNORE_UNUSED(unused);
  // Initialize the actor system.
  actor_system system{cfg};
  if (cfg.slave_mode) {
    if (!cfg.slave_mode_fun) {
      std::cerr << "cannot run slave mode, I/O module not loaded" << std::endl;
      return EXIT_FAILURE;
    }
    return cfg.slave_mode_fun(system, cfg);
  }
  helper f;
  f(fun, system, cfg);
  return EXIT_SUCCESS;
}

} // namespace caf

#define CAF_MAIN(...)                                                          \
  int main(int argc, char** argv) {                                            \
    return ::caf::exec_main<__VA_ARGS__>(caf_main, argc, argv);                \
  }