Skip to content

Commit

Permalink
Merge pull request #388 from parisiale/maint
Browse files Browse the repository at this point in the history
(maint) Refactor Configuration
  • Loading branch information
mruzicka committed Apr 12, 2016
2 parents 8237385 + 9512695 commit 732f821
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 141 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Dependencies
- Boost
- OpenSSL
- ruby (2.0 and newer)
- [leatherman][leatherman] (0.3.5 or newer)
- [leatherman][leatherman] (0.4.2 or newer)
- [cpp-pcp-client][cpp-pcp-client] (master)

Initial Setup
Expand Down Expand Up @@ -47,7 +47,9 @@ The following will install most required tools and libraries:

#### Setup on Windows

[MinGW-w64][MinGW-w64] is used for full C++11 support, and [Chocolatey][Chocolatey] can be used to install. You should have at least 2GB of memory for compilation.
[MinGW-w64][MinGW-w64] is used for full C++11 support, and
[Chocolatey][Chocolatey] can be used to install. You should have at least 2GB of
memory for compilation.

* install [CMake][CMake-choco] & [7zip][7zip-choco]

Expand All @@ -57,7 +59,9 @@ The following will install most required tools and libraries:

choco install mingw --params "/threads:win32"

For the remaining tasks, build commands can be executed in the shell from Start > MinGW-w64 project > Run Terminal
For the remaining tasks, build commands can be executed in the shell from:

Start > MinGW-w64 project > Run Terminal

* select an install location for dependencies, such as C:\\tools or cmake\\release\\ext; we'll refer to it as $install

Expand Down
38 changes: 22 additions & 16 deletions lib/inc/pxp-agent/configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

namespace PXPAgent {

namespace HW = HorseWhisperer;

//
// Tokens
//
Expand All @@ -29,7 +27,8 @@ extern const std::string DEFAULT_SPOOL_DIR; // used by unit tests

enum Types { Integer, String, Bool, Double };

struct EntryBase {
struct EntryBase
{
// Config option name
// HERE: must match one of the flag names and config file option
std::string name;
Expand All @@ -51,7 +50,8 @@ struct EntryBase {
};

template <typename T>
struct Entry : EntryBase {
struct Entry : EntryBase
{
// Default value
T value;

Expand Down Expand Up @@ -107,18 +107,22 @@ void configure_platform_file_logging();
// Configuration (singleton)
//

class Configuration {
class Configuration
{
public:
struct Error : public std::runtime_error {
struct Error : public std::runtime_error
{
explicit Error(std::string const& msg) : std::runtime_error(msg) {}
};

static Configuration& Instance() {
static Configuration& Instance()
{
static Configuration instance {};
return instance;
}

struct Agent {
struct Agent
{
std::string modules_dir;
std::string broker_ws_uri;
std::string ca;
Expand Down Expand Up @@ -147,7 +151,7 @@ class Configuration {
/// Throw a Configuration::Error: if it fails to parse the CLI
/// arguments; if the specified config file cannot be parsed or
/// has any invalid JSON entry.
HW::ParseResult parseOptions(int argc, char *argv[]);
HorseWhisperer::ParseResult parseOptions(int argc, char *argv[]);

/// Validate logging configuration options and enable logging.
/// Throw a Configuration::Error: in case of invalid the specified
Expand All @@ -167,11 +171,12 @@ class Configuration {
/// value in case nd the requested flag is known or throw a
/// Configuration::Error otherwise.
template <typename T>
T get(std::string flagname) {
T get(std::string flagname)
{
if (valid_) {
try {
return HW::GetFlag<T>(flagname);
} catch (HW::undefined_flag_error& e) {
return HorseWhisperer::GetFlag<T>(flagname);
} catch (HorseWhisperer::undefined_flag_error& e) {
throw Configuration::Error { std::string { e.what() } };
}
}
Expand All @@ -193,17 +198,18 @@ class Configuration {
/// Throw a Configuration::Error in case the specified flag is
/// unknown or the value is invalid.
template<typename T>
void set(std::string flagname, T value) {
void set(std::string flagname, T value)
{
if (!valid_) {
throw Configuration::Error { "cannot set configuration value until "
"configuration is validated" };
}

try {
HW::SetFlag<T>(flagname, value);
} catch (HW::flag_validation_error) {
HorseWhisperer::SetFlag<T>(flagname, value);
} catch (HorseWhisperer::flag_validation_error) {
throw Configuration::Error { "invalid value for " + flagname };
} catch (HW::undefined_flag_error) {
} catch (HorseWhisperer::undefined_flag_error) {
throw Configuration::Error { "unknown flag name: " + flagname };
}
}
Expand Down
Loading

0 comments on commit 732f821

Please sign in to comment.