config.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * @file
  3. * @author Caleb Fangmeier <caleb@fangmeier.tech>
  4. * @version 0.1
  5. *
  6. * @section LICENSE
  7. *
  8. *
  9. * MIT License
  10. *
  11. * Copyright (c) 2017 Caleb Fangmeier
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in all
  21. * copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. *
  31. */
  32. #ifndef config_hpp
  33. #define config_hpp
  34. #include <string>
  35. #include <iostream>
  36. #include "yaml-cpp/yaml.h"
  37. namespace fv::util{
  38. class Config{
  39. private:
  40. std::string input_filename;
  41. YAML::Node root;
  42. public:
  43. Config ()
  44. :root(), input_filename("") { }
  45. Config (const Config& c)
  46. :root(c.root), input_filename(c.input_filename) { }
  47. Config (const std::string& input_filename)
  48. :root(YAML::LoadFile(input_filename)),
  49. input_filename(input_filename) { }
  50. template <typename KEY_TYPE>
  51. YAML::Node get(const KEY_TYPE& key){
  52. auto val = root[key];
  53. return val;
  54. }
  55. };
  56. Config* the_config = nullptr;
  57. void init_config(const std::string& input_filename){
  58. if(the_config) delete the_config;
  59. the_config = new Config(input_filename);
  60. }
  61. }
  62. #endif // config_hpp