Belle II Software development
CppOrPyList Class Reference

Wrapper class for a list of strings to either be held in a std::vector or in a python list. More...

#include <Configuration.h>

Public Member Functions

boost::python::list & ensurePy ()
 Return the python list version.
 
std::vector< std::string > & ensureCpp ()
 Return the C++ vector version.
 
void append (const std::string &element)
 Append an element to whatever representation we currently have.
 
void prepend (const std::string &element)
 Prepend an element to whatever representation we currently have.
 
void shallowCopy (const boost::python::object &source)
 shallow copy all elements of the source object into the python representation.
 

Private Attributes

std::variant< std::vector< std::string >, boost::python::list > m_value
 Store either a std::vector or a python list of strings.
 

Detailed Description

Wrapper class for a list of strings to either be held in a std::vector or in a python list.

It's basically a std::variant with some convenience members to convert between the two representations.

This is necessary as python lists can only exist after Py_Initialize() and before Py_Finalize() and since we want this configuration class to work also in command line tools we need to be able to use std::vector unless python functions are called.

Definition at line 30 of file Configuration.h.

Member Function Documentation

◆ append()

void append ( const std::string & element)

Append an element to whatever representation we currently have.

Definition at line 70 of file Configuration.cc.

71 {
72 std::visit(Utils::VisitOverload{
73 [&element](std::vector<std::string>& list) {list.emplace_back(element);},
74 [&element](boost::python::list & list) {list.append(element);}
75 }, m_value);
76 }

◆ ensureCpp()

std::vector< std::string > & ensureCpp ( )

Return the C++ vector version.

Convert if necessary

Definition at line 60 of file Configuration.cc.

61 {
62 // or convert to std::vector ...
63 if (m_value.index() == 1) {
64 std::vector<std::string> tmp = extractStringList(std::get<1>(m_value));
65 m_value.emplace<std::vector<std::string>>(std::move(tmp));
66 }
67 return std::get<0>(m_value);
68 }

◆ ensurePy()

boost::python::list & ensurePy ( )

Return the python list version.

Convert if necessary

Definition at line 49 of file Configuration.cc.

50 {
51 // convert to python list ...
52 if (m_value.index() == 0) {
53 boost::python::list tmp;
54 for (const auto& e : std::get<0>(m_value)) { tmp.append(e); }
55 m_value.emplace<boost::python::list>(std::move(tmp));
56 }
57 return std::get<1>(m_value);
58 }

◆ prepend()

void prepend ( const std::string & element)

Prepend an element to whatever representation we currently have.

Definition at line 78 of file Configuration.cc.

79 {
80 std::visit(Utils::VisitOverload{
81 [&element](std::vector<std::string>& list) {list.emplace(list.begin(), element);},
82 [&element](boost::python::list & list) {list.insert(0, element);}
83 }, m_value);
84 }

◆ shallowCopy()

void shallowCopy ( const boost::python::object & source)

shallow copy all elements of the source object into the python representation.

Also converts to python representation

Definition at line 86 of file Configuration.cc.

87 {
88 ensurePy().slice(boost::python::_, boost::python::_) = source;
89 }

Member Data Documentation

◆ m_value

std::variant<std::vector<std::string>, boost::python::list> m_value
private

Store either a std::vector or a python list of strings.

Definition at line 45 of file Configuration.h.


The documentation for this class was generated from the following files: