Skip to content

Commit 11432ed

Browse files
matthiasrichterktf
authored andcommitted
Make wrapper device capable of handling boost program options
- Getter for custom options of device and component - Building component command line options from the boost variable map
1 parent 245fee1 commit 11432ed

2 files changed

Lines changed: 62 additions & 12 deletions

File tree

Utilities/aliceHLTwrapper/include/aliceHLTwrapper/WrapperDevice.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
//****************************************************************************
66
//* This file is free software: you can redistribute it and/or modify *
77
//* it under the terms of the GNU General Public License as published by *
8-
//* the Free Software Foundation, either version 3 of the License, or *
9-
//* (at your option) any later version. *
8+
//* the Free Software Foundation, either version 3 of the License, or *
9+
//* (at your option) any later version. *
1010
//* *
1111
//* Primary Authors: Matthias Richter <richterm@scieq.net> *
1212
//* *
@@ -21,6 +21,9 @@
2121

2222
#include <FairMQDevice.h>
2323
#include <vector>
24+
#include <boost/program_options.hpp>
25+
26+
namespace bpo = boost::program_options;
2427

2528
class FairMQMessage;
2629

@@ -38,10 +41,13 @@ class Component;
3841
class WrapperDevice : public FairMQDevice {
3942
public:
4043
/// default constructor
41-
WrapperDevice(int argc, char** argv, int verbosity = 0);
44+
WrapperDevice(int verbosity = 0);
4245
/// destructor
4346
~WrapperDevice() override;
4447

48+
/// get description of options
49+
static bpo::options_description GetOptionsDescription();
50+
4551
/////////////////////////////////////////////////////////////////
4652
// the FairMQDevice interface
4753

@@ -82,7 +88,6 @@ class WrapperDevice : public FairMQDevice {
8288
unsigned char* createMessageBuffer(unsigned size);
8389

8490
Component* mComponent; // component instance
85-
std::vector<char*> mArgv; // array of arguments for the component
8691
std::vector<std::unique_ptr<FairMQMessage>> mMessages; // array of output messages
8792

8893
int mPollingPeriod; // period of polling on input sockets in ms

Utilities/aliceHLTwrapper/src/WrapperDevice.cxx

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//****************************************************************************
22
//* This file is free software: you can redistribute it and/or modify *
33
//* it under the terms of the GNU General Public License as published by *
4-
//* the Free Software Foundation, either version 3 of the License, or *
5-
//* (at your option) any later version. *
4+
//* the Free Software Foundation, either version 3 of the License, or *
5+
//* (at your option) any later version. *
66
//* *
77
//* Primary Authors: Matthias Richter <richterm@scieq.net> *
88
//* *
@@ -19,6 +19,8 @@
1919
#include "aliceHLTwrapper/Component.h"
2020
#include <FairMQLogger.h>
2121
#include <FairMQPoller.h>
22+
#include <options/FairProgOptions.h>
23+
#include <options/FairMQProgOptions.h>
2224

2325
#include <boost/thread.hpp>
2426
#include <boost/bind.hpp>
@@ -36,9 +38,8 @@ using namespace ALICE::HLT;
3638
using std::chrono::system_clock;
3739
using TimeScale = std::chrono::milliseconds;
3840

39-
WrapperDevice::WrapperDevice(int argc, char** argv, int verbosity)
41+
WrapperDevice::WrapperDevice(int verbosity)
4042
: mComponent(nullptr)
41-
, mArgv()
4243
, mMessages()
4344
, mPollingPeriod(10)
4445
, mSkipProcessing(0)
@@ -51,12 +52,19 @@ WrapperDevice::WrapperDevice(int argc, char** argv, int verbosity)
5152
, mNSamples(-1)
5253
, mVerbosity(verbosity)
5354
{
54-
mArgv.insert(mArgv.end(), argv, argv+argc);
5555
}
5656

5757
WrapperDevice::~WrapperDevice()
5858
= default;
5959

60+
bpo::options_description WrapperDevice::GetOptionsDescription()
61+
{
62+
// assemble the options for the device class and component
63+
bpo::options_description od("WrapperDevice options");
64+
od.add(Component::GetOptionsDescription());
65+
return od;
66+
}
67+
6068
void WrapperDevice::Init()
6169
{
6270
}
@@ -66,18 +74,55 @@ void WrapperDevice::InitTask()
6674
/// inherited from FairMQDevice
6775

6876
int iResult=0;
77+
6978
std::unique_ptr<Component> component(new ALICE::HLT::Component);
7079
if (!component.get()) return /*-ENOMEM*/;
7180

81+
// loop over program options, check if the option was used and
82+
// add it together with the parameter to the argument vector.
83+
// would have been easier to iterate over the individual
84+
// option_description entires, but options_description does not
85+
// provide such a functionality
86+
vector<std::string> argstrings;
87+
bpo::options_description descriptions = GetOptionsDescription();
88+
const auto * config = GetConfig();
89+
if (config) {
90+
const auto varmap = config->GetVarMap();
91+
for (const auto varit : varmap) {
92+
// check if this key belongs to the options of the device
93+
const auto * description = descriptions.find_nothrow(varit.first, false);
94+
if (description && varmap.count(varit.first) && !varit.second.defaulted()) {
95+
argstrings.push_back("--");
96+
argstrings.back() += varit.first;
97+
// check the semantics of the value
98+
auto semantic = description->semantic();
99+
if (semantic) {
100+
// the value semantics allows different properties like
101+
// multitoken, zero_token and composing
102+
// currently only the simple case is supported
103+
assert(semantic->min_tokens() <= 1);
104+
assert(semantic->max_tokens() && semantic->min_tokens());
105+
if (semantic->min_tokens() > 0 ) {
106+
// add the token
107+
argstrings.push_back(varit.second.as<std::string>());
108+
}
109+
}
110+
}
111+
}
112+
}
113+
114+
// TODO: probably one can get rid of this option, the instance/device
115+
// id is now specified with the --id option of FairMQProgOptions
72116
string idkey="--instance-id";
73117
string id="";
74118
id=GetProperty(FairMQDevice::Id, id);
75119
vector<char*> argv;
76-
argv.push_back(mArgv[0]);
77120
argv.push_back(&idkey[0]);
78121
argv.push_back(&id[0]);
79-
if (mArgv.size()>1)
80-
argv.insert(argv.end(), mArgv.begin()+1, mArgv.end());
122+
for (auto& argstringiter : argstrings) {
123+
argv.push_back(&argstringiter[0]);
124+
}
125+
81126
if ((iResult=component->init(argv.size(), &argv[0]))<0) {
82127
LOG(ERROR) << "component init failed with error code " << iResult;
83128
throw std::runtime_error("component init failed");

0 commit comments

Comments
 (0)