You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.0 KiB

  1. #pragma once
  2. #include <cpputils/mp.h>
  3. #include "group.h"
  4. namespace cppargs
  5. {
  6. template<typename T_arg, typename T_enable = void>
  7. struct group_init;
  8. template<typename T_arg>
  9. struct group_init<
  10. std::unique_ptr<T_arg>,
  11. utl::mp::enable_if<utl::mp::is_base_of<option, T_arg>>>
  12. {
  13. using option_ptr_type = std::unique_ptr<option>;
  14. using option_vector_type = std::vector<option_ptr_type>;
  15. using group_ptr_type = std::unique_ptr<group>;
  16. using group_vector_type = std::vector<group_ptr_type>;
  17. option_vector_type& options;
  18. group_vector_type& groups;
  19. template<typename X>
  20. void operator()(X&& x)
  21. { options.emplace_back(std::forward<X>(x)); }
  22. };
  23. template<typename T_arg>
  24. struct group_init<
  25. std::unique_ptr<T_arg>,
  26. utl::mp::enable_if<utl::mp::is_base_of<group, T_arg>>>
  27. {
  28. using option_ptr_type = std::unique_ptr<option>;
  29. using option_vector_type = std::vector<option_ptr_type>;
  30. using group_ptr_type = std::unique_ptr<group>;
  31. using group_vector_type = std::vector<group_ptr_type>;
  32. option_vector_type& options;
  33. group_vector_type& groups;
  34. template<typename X>
  35. void operator()(X&& x)
  36. { groups.emplace_back(std::forward<X>(x)); }
  37. };
  38. group_meta& group_meta::set_name(const std::string& value)
  39. {
  40. name = value;
  41. return *this;
  42. }
  43. group_meta& group_meta::set_description(const std::string& value)
  44. {
  45. description = value;
  46. return *this;
  47. }
  48. template<typename... T_args>
  49. group::group(
  50. const group_meta& p_meta,
  51. T_args&&... p_args)
  52. : meta(p_meta)
  53. {
  54. (void) (int[]) { 0, (group_init<T_args> { _options, _groups } (std::forward<T_args>(p_args)), 0)... };
  55. }
  56. const group::option_vector_type& group::options() const
  57. { return _options; }
  58. const group::group_vector_type& group::groups() const
  59. { return _groups; }
  60. }