Swarm-NG  1.1
ensemble_alloc.hpp
1 #pragma once
2 #include "types/ensemble.hpp"
3 
4 namespace swarm {
5 
11 template< int W , template<class T> class _Allocator >
12 struct EnsembleAlloc : public EnsembleBase<W> {
13  typedef EnsembleBase<W> Base;
14  typedef EnsembleAlloc Self;
15  typedef typename Base::Body Body;
16  typedef typename Base::Sys Sys;
17  typedef _Allocator<Body> BodyAllocator;
18  typedef _Allocator<Sys> SysAllocator;
19 
20  typedef boost::shared_ptr<Body> PBody;
21  typedef boost::shared_ptr<Sys> PSys;
22 
25  return cloneTo<EnsembleAlloc>();
26  }
27 
30  static EnsembleAlloc create(const int& nbod, const int& nsys) {
31  PBody b ( BodyAllocator::alloc( Base::body_element_count(nbod,nsys) ), &BodyAllocator::free );
32  PSys s ( SysAllocator::alloc( Base::sys_element_count(nsys) ), &SysAllocator::free );
33  return EnsembleAlloc(nbod,nsys,b,s);
34  }
35 
37  template< class Other >
38  Other cloneTo() {
39  Other o = Other::create(Base::nbod(),Base::nsys());
40  copyTo(o);
41  return o;
42  }
43 
45  template< class Other >
46  void copyTo(Other& o){
47  assert(o.nsys() == Base::nsys() && o.nbod() == Base::nbod());
48  alloc_copy(BodyAllocator(),typename Other::BodyAllocator(),Base::bodies().begin(),Base::bodies().end(),o.bodies().begin());
49  alloc_copy(SysAllocator(),typename Other::SysAllocator(),Base::systems().begin(),Base::systems().end(),o.systems().begin());
50  }
51 
52  EnsembleAlloc(){}
53  private:
54  EnsembleAlloc(const int& nbod,const int& nsys, PBody b, PSys s):
55  Base(nbod,nsys,b.get(),s.get())
56  ,_body(b),_sys(s){}
57 
58  PSys _sys;
59  PBody _body;
60 };
61 
62 
69 
70 
75 
76 }