test_icl_core_config.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
21 //----------------------------------------------------------------------
22 #include <iostream>
23 
24 #include <icl_core_config/Config.h>
26 
28 {
29 public:
31  {
32  icl_core::config::ConfigManager::instance().registerObserver(this, "/example/config/node");
33  icl_core::config::ConfigManager::instance().registerObserver(this, "/observed/by/its/path");
36  }
37 
38  virtual void valueChanged(const icl_core::String &key)
39  {
40  std::cout << "The value " << key << " changed to ";
41  std::cout << icl_core::config::getDefault<icl_core::String>(key, " error when trying to get new value!") << std::endl;
42  }
43 };
44 
45 int main(int argc, char *argv[])
46 {
48  cmd_parameters.push_back(icl_core::config::ConfigParameter("example:", "e", "/example/config/node", "Example help text."));
49  icl_core::config::addParameter(cmd_parameters);
50 
51  icl_core::config::initialize(argc, argv);
52 
54 
55  std::cout << std::endl << "-- Direct access by name --" << std::endl;
56  icl_core::String value;
57  bool success = icl_core::config::get<icl_core::String>("/example/config/node", value);
58  std::cout << "/example/config/node(" << success << "): " << value << std::endl;
59 
60  std::cout << std::endl << "-- Direct access by name with default value --" << std::endl;
61  icl_core::String another_value = icl_core::config::getDefault<icl_core::String>("/example/config/node", icl_core::String("42"));
62  std::cout << "/example/config/node: " << another_value << std::endl;
63 
64  std::cout << std::endl << "-- Regex access --" << std::endl;
65  icl_core::config::ConfigIterator find_it = icl_core::config::find("\\/example\\/(.*)");
66  while (find_it.next()) {
67  std::cout << find_it.key() << "(" << find_it.matchGroup(1) << "): " << find_it.value() << std::endl;
68  }
69 
70  std::cout << std::endl << "-- Observing configuration changes --" << std::endl;
71  TestObserver observer;
72  icl_core::config::ConfigManager::instance().setValue<double>("/observed/by/its/path", 4.2);
73  icl_core::config::ConfigManager::instance().setValue<icl_core::String>("/observed/generally", "Using an empty key during registration");
74 
75  return 0;
76 }
static ConfigManager & instance()
bool initialize(int &argc, char *argv[], bool remove_read_arguments)
Definition: Config.cpp:50
Contains ConfigObserver.
Interface for observing configuration changes.
void registerObserver(ConfigObserver *observer, const String &key="")
Base header file for the configuration framework.
int main(int argc, char *argv[])
ThreadStream & endl(ThreadStream &stream)
Definition: ThreadStream.h:249
ConfigIterator find(const ::icl_core::String &query)
Definition: Config.cpp:45
void addParameter(const ConfigParameter &parameter)
Definition: Config.h:541
virtual void valueChanged(const icl_core::String &key)
String matchGroup(size_t index) const
std::string String
Definition: BaseTypes.h:43
ConvertToRef< T >::ToConstRef value() const
bool setValue(const icl_core::String &key, typename icl_core::ConvertToRef< T >::ToConstRef value)
Add a key/value pair or change a value. In contrast to Insert, this method notifies observers...


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58