ppap4lmp  0.7.2
pro_data.cpp
Go to the documentation of this file.
1 
9 #include <alias/pybind.h>
10 
11 #include "pro_data.h"
12 #include "../utils/pyargs_to_vec.h"
13 
14 namespace ut = utils;
15 
16 /* ------------------------------------------------------------------ */
17 
19  const ElPtr &elem)
20 {
21  register_generator(elem);
22 }
23 
24 /* ------------------------------------------------------------------ */
25 
27  const Vec<ElPtr> &elems)
28 {
29  register_generators(elems);
30 }
31 
32 /* ------------------------------------------------------------------ */
33 
35  const int index)
36 {
37  auto elem = generators[index]->get_element();
38  auto &data = elem->get_data();
39 
40  if (selected_keys.empty())
41  {
42  results[index] = data;
43  }
44  else
45  {
46  elem->check_required_keys(selected_keys);
47 
48  auto json = Json::array();
49 
50  if (data.is_array())
51  {
52  json.get_ref<Json::array_t&>().reserve(data.size());
53  }
54 
55  for (const auto &d : data.is_array() ? data : Json::array({data}))
56  {
57  json.push_back({});
58  auto &elem = json.back();
59 
60  for (const auto &k : selected_keys)
61  {
62  elem[k] = d[k];
63  }
64  }
65 
66  results[index].swap(data.is_array() ? json : json.front());
67  }
68 }
69 
70 /* ------------------------------------------------------------------ */
71 
73 {
74  results.clear();
75  results.resize(n_generators);
76 }
77 
78 /* ------------------------------------------------------------------ */
79 
81  const py::args &args)
82 {
84 }
85 
86 /* ------------------------------------------------------------------ */
87 
89 {
90  return results;
91 }
Vec< Str > selected_keys
Definition: pro_data.h:28
Vec< ShPtr< Generator > > generators
Definition: processor.h:37
ShPtr< Element > ElPtr
An alias for a shared pointer of Element class.
Definition: element.h:378
int n_generators
Definition: processor.h:31
virtual void prepare() override
Clear and resize results.
Definition: pro_data.cpp:72
void select(const py::args &args)
Specify string keys for properties to be copied.
Definition: pro_data.cpp:80
void pyargs_to_vec(const py::args &args, Vec< T > &vec)
Convert a py::args (a variable number arguments) object to a Vec object.
std::vector< T > Vec
Vec is an alias for vector (same as list in Python).
Definition: std.h:27
void register_generator(const ShPtr< GEN > &gen)
Definition: processor.cpp:16
This file has a definition of ProData class, which is a subclass of Processor class.
virtual void run_impl(const int index) override
Implementation of analysis using an element of generators.
Definition: pro_data.cpp:34
Vec< Json > results
Definition: pro_data.h:34
const Vec< Json > & get_results()
Get list of Json objects consisting of all or some properties of Element objects stored in this objec...
Definition: pro_data.cpp:88
ProData(const ElPtr &elem)
Constructor of ProData class for one Element object.
Definition: pro_data.cpp:18
Namespace for utility functions.
Definition: join.h:14
void register_generators(const Vec< ShPtr< GEN >> &gens)
Definition: processor.cpp:36
This file includes pybind11 and defines an alias for the namespace pybind11.