OpenStructure
vector.hh
Go to the documentation of this file.
1 #ifndef OST_EXPORT_HELPER_VECTOR_HH
2 #define OST_EXPORT_HELPER_VECTOR_HH
3 
4 #include <boost/python/def_visitor.hpp>
5 #include <boost/python/suite/indexing/container_utils.hpp>
6 /*
7  Author: Marco Biasini
8  */
9 
10 namespace geom {
11 
12 namespace bp=boost::python;
13 template <typename Container>
15  public bp::def_visitor<VectorAdditions<Container> > {
16 public:
17  typedef typename Container::value_type value_type;
18  typedef Container container_type;
19  template <class Class>
20  void visit(Class& cl) const
21  {
22  cl
23  .def("__str__", &to_string)
24  .def("__init__", make_constructor(&from_iter))
25  ;
26  }
27 private:
28  static boost::shared_ptr<Container> from_iter(const bp::object& obj)
29  {
30  boost::shared_ptr<Container> c(new Container);
31  bp::container_utils::extend_container(*c.get(), obj);
32  return c;
33  }
34 
35  static std::string to_string(Container& cl)
36  {
37  std::stringstream ss;
38  ss << "[";
39  bool first=true;
40  for (typename Container::const_iterator
41  i=cl.begin(), e=cl.end(); i!=e; ++i) {
42  if (first) {
43  first=false;
44  } else {
45  ss << ", ";
46  }
47  ss << (*i);
48  }
49  ss << "]";
50  return ss.str();
51  }
52 };
53 
54 }
55 
56 #endif
Container::value_type value_type
Definition: vector.hh:17
Container container_type
Definition: vector.hh:18
void visit(Class &cl) const
Definition: vector.hh:20