world.h
Go to the documentation of this file.
1 #ifndef TTG_WORLD_H
2 #define TTG_WORLD_H
3 
4 #include "ttg/impl_selector.h"
5 
6 #include <stdexcept>
7 #include <algorithm>
8 
9 #include "ttg/base/world.h"
10 #include "ttg/base/keymap.h"
11 
12 #include "ttg/fwd.h"
13 
14 namespace ttg {
15 
16  /* Slim wrapper to allow for forward declaration */
17  class World : public ttg::base::World<TTG_IMPL_NS::WorldImpl> {
19  };
20 
21  namespace detail {
22  template<typename WorldT>
24  static WorldT world;
25  return world;
26  }
27 
28  template<typename WorldT>
29  inline void set_default_world(WorldT& world) { detail::default_world_accessor<WorldT>() = world; }
30  template<typename WorldT>
31  inline void set_default_world(WorldT&& world) { detail::default_world_accessor<WorldT>() = std::move(world); }
32 
33  template <typename keyT>
35  public:
36  default_keymap() = default;
37  default_keymap(const ttg::World& world) : ttg::detail::default_keymap_impl<keyT>(world.size()) {}
38  };
39 
40  template <typename keyT>
42  public:
43  default_priomap() = default;
44  };
45 
46  template<typename WorldImplT>
47  std::list<WorldImplT*>&
49  static std::list<WorldImplT*> world_registry;
50  return world_registry;
51  }
52 
53  /* TODO: how should the MADNESS and PaRSEC init/finalize play together? */
54  template<typename WorldImplT>
55  void register_world(WorldImplT& world)
56  {
57  world_registry_accessor<WorldImplT>().push_back(&world);
58  }
59 
60  template<typename WorldImplT>
61  void deregister_world(WorldImplT& world) {
62  auto& world_registry = world_registry_accessor<WorldImplT>();
63  auto it = std::find(world_registry.begin(), world_registry.end(), &world);
64  if (it != world_registry.end()) {
65  world_registry.remove(&world);
66  }
67  }
68 
69  template<typename WorldImplT>
70  void destroy_worlds(void) {
71  auto& world_registry = world_registry_accessor<WorldImplT>();
72  while (!world_registry.empty()) {
73  auto it = world_registry.begin();
74  (*it)->destroy();
75  }
76  }
77 
78  } // namespace detail
79 
81  if (detail::default_world_accessor<ttg::World>().is_valid()) {
82  return detail::default_world_accessor<ttg::World>();
83  } else {
84  throw std::runtime_error("ttg::set_default_world() must be called before use");
85  }
86  }
87 
88 } // namespace ttg
89 
90 #endif // TTG_WORLD_H
void set_default_world(WorldT &world)
Definition: world.h:29
void deregister_world(ttg::base::WorldImplBase &world)
WorldT & default_world_accessor()
Definition: world.h:23
std::list< WorldImplT * > & world_registry_accessor()
Definition: world.h:48
void register_world(ttg::base::WorldImplBase &world)
void destroy_worlds(void)
Definition: world.h:70
top-level TTG namespace contains runtime-neutral functionality
Definition: keymap.h:8
int size(World world=default_execution_context())
Definition: run.h:89
ttg::World & get_default_world()
Definition: world.h:80
default_keymap(const ttg::World &world)
Definition: world.h:37
the default priority map implementation
Definition: keymap.h:42