# FairMQ: fairmq/ProgOptions.h Source File

ProgOptions.h
/********************************************************************************
* Copyright (C) 2014-2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
#ifndef FAIR_MQ_PROGOPTIONS_H
#define FAIR_MQ_PROGOPTIONS_H
#include "FairMQChannel.h"
#include "FairMQLogger.h"
#include <fairmq/EventManager.h>
#include <fairmq/ProgOptionsFwd.h>
#include <fairmq/Properties.h>
#include <fairmq/tools/Strings.h>
#include <boost/program_options.hpp>
#include <functional>
#include <map>
#include <mutex>
#include <string>
#include <unordered_map>
#include <vector>
#include <stdexcept>
namespace fair
{
namespace mq
{
struct PropertyNotFoundError : std::runtime_error { using std::runtime_error::runtime_error; };
class ProgOptions
{
public:
ProgOptions();
virtual ~ProgOptions() {}
void ParseAll(const std::vector<std::string>& cmdArgs, bool allowUnregistered);
void ParseAll(const int argc, char const* const* argv, bool allowUnregistered = true);
void Notify();
void AddToCmdLineOptions(const boost::program_options::options_description optDesc, bool visible = true);
boost::program_options::options_description& GetCmdLineOptions();
int Count(const std::string& key) const;
std::unordered_map<std::string, int> GetChannelInfo() const;
std::vector<std::string> GetPropertyKeys() const;
template<typename T>
T GetProperty(const std::string& key) const
{
std::lock_guard<std::mutex> lock(fMtx);
if (fVarMap.count(key)) {
return fVarMap[key].as<T>();
} else {
throw PropertyNotFoundError(fair::mq::tools::ToString("Config has no key: ", key));
}
}
template<typename T>
T GetProperty(const std::string& key, const T& ifNotFound) const
{
std::lock_guard<std::mutex> lock(fMtx);
if (fVarMap.count(key)) {
return fVarMap[key].as<T>();
}
return ifNotFound;
}
std::string GetPropertyAsString(const std::string& key) const;
std::string GetPropertyAsString(const std::string& key, const std::string& ifNotFound) const;
fair::mq::Properties GetProperties(const std::string& q) const;
fair::mq::Properties GetPropertiesStartingWith(const std::string& q) const;
std::map<std::string, std::string> GetPropertiesAsString(const std::string& q) const;
std::map<std::string, std::string> GetPropertiesAsStringStartingWith(const std::string& q) const;
template<typename T>
void SetProperty(const std::string& key, T val)
{
std::unique_lock<std::mutex> lock(fMtx);
SetVarMapValue<typename std::decay<T>::type>(key, val);
lock.unlock();
fEvents.Emit<fair::mq::PropertyChange, typename std::decay<T>::type>(key, val);
fEvents.Emit<fair::mq::PropertyChangeAsString, std::string>(key, GetPropertyAsString(key));
}
template<typename T>
bool UpdateProperty(const std::string& key, T val)
{
std::unique_lock<std::mutex> lock(fMtx);
if (fVarMap.count(key)) {
SetVarMapValue<typename std::decay<T>::type>(key, val);
lock.unlock();
fEvents.Emit<fair::mq::PropertyChange, typename std::decay<T>::type>(key, val);
fEvents.Emit<fair::mq::PropertyChangeAsString, std::string>(key, GetPropertyAsString(key));
return true;
} else {
LOG(debug) << "UpdateProperty failed, no property found with key '" << key << "'";
return false;
}
}
void SetProperties(const fair::mq::Properties& input);
bool UpdateProperties(const fair::mq::Properties& input);
void DeleteProperty(const std::string& key);
void AddChannel(const std::string& name, const FairMQChannel& channel);
template<typename T>
void Subscribe(const std::string& subscriber, std::function<void(typename fair::mq::PropertyChange::KeyType, T)> func) const
{
std::lock_guard<std::mutex> lock(fMtx);
static_assert(!std::is_same<T,const char*>::value || !std::is_same<T, char*>::value,
"In template member ProgOptions::Subscribe<T>(key,Lambda) the types const char* or char* for the calback signatures are not supported.");
fEvents.Subscribe<fair::mq::PropertyChange, T>(subscriber, func);
}
template<typename T>
void Unsubscribe(const std::string& subscriber) const
{
std::lock_guard<std::mutex> lock(fMtx);
fEvents.Unsubscribe<fair::mq::PropertyChange, T>(subscriber);
}
void SubscribeAsString(const std::string& subscriber, std::function<void(typename fair::mq::PropertyChange::KeyType, std::string)> func) const
{
std::lock_guard<std::mutex> lock(fMtx);
fEvents.Subscribe<fair::mq::PropertyChangeAsString, std::string>(subscriber, func);
}
void UnsubscribeAsString(const std::string& subscriber) const
{
std::lock_guard<std::mutex> lock(fMtx);
fEvents.Unsubscribe<fair::mq::PropertyChangeAsString, std::string>(subscriber);
}
void PrintHelp() const;
void PrintOptions() const;
void PrintOptionsRaw() const;
const boost::program_options::variables_map& GetVarMap() const { return fVarMap; }
template<typename T>
T GetValue(const std::string& key) const /* TODO: deprecate this */
{
std::lock_guard<std::mutex> lock(fMtx);
if (fVarMap.count(key)) {
return fVarMap[key].as<T>();
} else {
LOG(warn) << "Config has no key: " << key << ". Returning default constructed object.";
return T();
}
}
template<typename T>
int SetValue(const std::string& key, T val) /* TODO: deprecate this */ { SetProperty(key, val); return 0; }
std::string GetStringValue(const std::string& key) const; /* TODO: deprecate this */
private:
void ParseDefaults();
std::unordered_map<std::string, int> GetChannelInfoImpl() const;
template<typename T>
void SetVarMapValue(const std::string& key, const T& val)
{
std::map<std::string, boost::program_options::variable_value>& vm = fVarMap;
vm[key].value() = boost::any(val);
}
boost::program_options::variables_map fVarMap;
boost::program_options::options_description fAllOptions;
std::vector<std::string> fUnregisteredOptions;
mutable fair::mq::EventManager fEvents;
mutable std::mutex fMtx;
};
} // namespace mq
} // namespace fair
#endif /* FAIR_MQ_PROGOPTIONS_H */
fair::mq::ProgOptions::GetProperty
T GetProperty(const std::string &key) const
Read config property, throw if no property with this key exists.
Definition: ProgOptions.h:65
fair::mq::ProgOptions::Subscribe
void Subscribe(const std::string &subscriber, std::function< void(typename fair::mq::PropertyChange::KeyType, T)> func) const
Subscribe to property updates of type T.
Definition: ProgOptions.h:187
fair::mq::ProgOptions::GetValue
T GetValue(const std::string &key) const
Read config property, return default-constructed object if key doesn&#39;t exist.
Definition: ProgOptions.h:237
fair::mq::ProgOptions::GetProperty
T GetProperty(const std::string &key, const T &ifNotFound) const
Read config property, return provided value if no property with this key exists.
Definition: ProgOptions.h:80
fair::mq::ProgOptions::SubscribeAsString
void SubscribeAsString(const std::string &subscriber, std::function< void(typename fair::mq::PropertyChange::KeyType, std::string)> func) const
Subscribe to property updates, with values converted to string.
Definition: ProgOptions.h:209
fair::mq::ProgOptions::Unsubscribe
void Unsubscribe(const std::string &subscriber) const
Unsubscribe from property updates of type T.
Definition: ProgOptions.h:198
fair::mq::EventManager
Manages event callbacks from different subscribers.
Definition: EventManager.h:51
fair::mq::PropertyNotFoundError
Definition: ProgOptions.h:34
FairMQChannel
Definition: FairMQChannel.h:30
fair::mq::ProgOptions::UpdateProperty
bool UpdateProperty(const std::string &key, T val)
Updates an existing config property (or fails if it doesn&#39;t exist)
Definition: ProgOptions.h:148
fair::mq::ProgOptions
Definition: ProgOptions.h:36
fair::mq::ProgOptions::SetProperty
void SetProperty(const std::string &key, T val)
Set config property.
Definition: ProgOptions.h:132
fair::mq::ProgOptions::UnsubscribeAsString
void UnsubscribeAsString(const std::string &subscriber) const
Unsubscribe from property updates that convert to string.
Definition: ProgOptions.h:217
fair::mq::PropertyChangeAsString
Definition: Properties.h:33
fair
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23
fair::mq::ProgOptions::GetVarMap
const boost::program_options::variables_map & GetVarMap() const
returns the property container
Definition: ProgOptions.h:231
fair::mq::PropertyChange
Definition: Properties.h:32

privacy