test_icl_core_config.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00021 //----------------------------------------------------------------------
00022 #include <iostream>
00023 
00024 #include <icl_core_config/Config.h>
00025 #include <icl_core_config/ConfigObserver.h>
00026 
00027 class TestObserver : public icl_core::config::ConfigObserver
00028 {
00029 public:
00030   TestObserver()
00031   {
00032     icl_core::config::ConfigManager::instance().registerObserver(this, "/example/config/node");
00033     icl_core::config::ConfigManager::instance().registerObserver(this, "/observed/by/its/path");
00034     icl_core::config::ConfigManager::instance().registerObserver(this, "/foo");
00035     icl_core::config::ConfigManager::instance().registerObserver(this);
00036   }
00037 
00038   virtual void valueChanged(const icl_core::String &key)
00039   {
00040     std::cout << "The value " << key << " changed to ";
00041     std::cout << icl_core::config::getDefault<icl_core::String>(key, " error when trying to get new value!") << std::endl;
00042   }
00043 };
00044 
00045 int main(int argc, char *argv[])
00046 {
00047   icl_core::config::ConfigParameterList cmd_parameters;
00048   cmd_parameters.push_back(icl_core::config::ConfigParameter("example:", "e", "/example/config/node", "Example help text."));
00049   icl_core::config::addParameter(cmd_parameters);
00050 
00051   icl_core::config::initialize(argc, argv);
00052 
00053   icl_core::config::ConfigManager::instance().dump();
00054 
00055   std::cout << std::endl << "-- Direct access by name --" << std::endl;
00056   icl_core::String value;
00057   bool success = icl_core::config::get<icl_core::String>("/example/config/node", value);
00058   std::cout << "/example/config/node(" << success << "): " << value << std::endl;
00059 
00060   std::cout << std::endl << "-- Direct access by name with default value --" << std::endl;
00061   icl_core::String another_value = icl_core::config::getDefault<icl_core::String>("/example/config/node", icl_core::String("42"));
00062   std::cout << "/example/config/node: " << another_value << std::endl;
00063 
00064   std::cout << std::endl << "-- Regex access --" << std::endl;
00065   icl_core::config::ConfigIterator find_it = icl_core::config::find("\\/example\\/(.*)");
00066   while (find_it.next()) {
00067     std::cout << find_it.key() << "(" << find_it.matchGroup(1) << "): " << find_it.value() << std::endl;
00068   }
00069 
00070   std::cout << std::endl << "-- Observing configuration changes --" << std::endl;
00071   TestObserver observer;
00072   icl_core::config::ConfigManager::instance().setValue<double>("/observed/by/its/path", 4.2);
00073   icl_core::config::ConfigManager::instance().setValue<icl_core::String>("/observed/generally", "Using an empty key during registration");
00074 
00075   return 0;
00076 }


fzi_icl_core
Author(s):
autogenerated on Tue Aug 8 2017 02:28:04