Line data Source code
1 : /* File: interface_group.h 2 : * This file is part of the program hardiocore 3 : * Program description : Abstract low level I/O removing direct hardware 4 : * implementation Copyright (C) 2018-2020 - Robin Passama (CNRS / LIRMM) 5 : * Clement Rebut (EPITA / LIRMM) Charles Villard (EPITA / LIRMM). All Right 6 : * reserved. 7 : * 8 : * This software is free software: you can redistribute it and/or modify 9 : * it under the terms of the CeCILL-C license as published by 10 : * the CEA CNRS INRIA, either version 1 11 : * of the License, or (at your option) any later version. 12 : * This software is distributed in the hope that it will be useful, 13 : * but WITHOUT ANY WARRANTY without even the implied warranty of 14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 : * CeCILL-C License for more details. 16 : * 17 : * You should have received a copy of the CeCILL-C License 18 : * along with this software. If not, it can be found on the official 19 : * website of the CeCILL licenses family (http://www.cecill.info/index.en.html). 20 : */ 21 : /** 22 : * @file common/interface_group.h 23 : * @author Robin Passama 24 : * @brief header file for InterfaceGroup class 25 : * @date May 2025 26 : * @ingroup core 27 : */ 28 : #pragma once 29 : 30 : #include <hardio/common/interface.h> 31 : 32 : namespace hardio { 33 : 34 : /** 35 : * @brief Group of interfaces used by a protocol implementation 36 : * 37 : * @tparam Interfaces the types of interfaces belonging to the group 38 : */ 39 : template <typename... Interfaces> 40 : class InterfaceGroup : public Interface { 41 : public: 42 : using type = InterfaceGroup<typename Interfaces::base_type...>; 43 : static constexpr int size = sizeof...(Interfaces); 44 : 45 : InterfaceGroup(const InterfaceGroup&) = delete; 46 : virtual ~InterfaceGroup() = default; 47 : InterfaceGroup() = delete; 48 : InterfaceGroup(typename Interfaces::base_type&... accesses) 49 : : Interface(), // type id will be the same whatever 50 : // the specialization of base type is 51 : elements_{accesses...} { 52 : } 53 : 54 : /** 55 : * @brief get elements the group is made of 56 : * @return a tuple of all elements belonging to the group 57 : */ 58 0 : const std::tuple<typename Interfaces::base_type&...>& elements() const { 59 0 : return elements_; 60 : } 61 : 62 : /** 63 : * @brief get name of the group 64 : * @details the name is a string containing the name of all interfaces it 65 : * constains 66 : * @return the name as a string 67 : */ 68 : std::string name() const final { 69 : std::string ret = ""; 70 : ret += "{"; 71 : build_name(ret); 72 : ret += "}"; 73 : ret += "(" + id() + ")"; 74 : return ret; 75 : } 76 : 77 : /** 78 : * @brief get id of the group 79 : * @details the id is a string containing the id of all interfaces it 80 : * constains 81 : * @return the id as a string 82 : */ 83 : std::string id() const final { 84 : std::string ret = ""; 85 : ret += "{"; 86 : build_id(ret); 87 : ret += "}"; 88 : return ret; 89 : } 90 : 91 : /** 92 : * @brief get type id of the group 93 : * 94 : * @return the type id as a uint64_t 95 : */ 96 : uint64_t type_id() const final { 97 : return pid::type_id<type>(); 98 : } 99 : 100 : private: 101 : std::tuple<typename Interfaces::base_type&...> elements_; 102 : 103 : template <size_t I = 0> 104 : void build_name(std::string& ret) const { 105 : if constexpr (I == sizeof...(Interfaces)) { 106 : // iteration over all elements done 107 : // Last case, if nothing is left to 108 : // iterate, then exit the function 109 : return; 110 : } else { 111 : if constexpr (I != 0) { 112 : ret += ", "; 113 : } 114 : ret += std::get<I>(elements_).name(); 115 : // Going for next element. 116 : build_name<I + 1>(ret); 117 : } 118 : } 119 : template <size_t I = 0> 120 : void build_id(std::string& ret) const { 121 : if constexpr (I == sizeof...(Interfaces)) { 122 : // iteration over all elements done 123 : // Last case, if nothing is left to 124 : // iterate, then exit the function 125 : return; 126 : } else { 127 : if constexpr (I != 0) { 128 : ret += ", "; 129 : } 130 : ret += std::get<I>(elements_).id(); 131 : // Going for next element. 132 : build_id<I + 1>(ret); 133 : } 134 : } 135 : }; 136 : 137 : template <typename... Accesses> 138 : using group = InterfaceGroup<Accesses...>; 139 : 140 : template <size_t I, typename T> 141 : struct group_n { 142 : template <typename... Args> 143 : using type = typename group_n<I - 1, T>::template type<T, Args...>; 144 : }; 145 : 146 : template <typename T> 147 : struct group_n<0, T> { 148 : template <typename... Args> 149 : using type = group<Args...>; 150 : }; 151 : template <size_t I, typename T> 152 : using group_of = typename group_n<I, T>::template type<>; 153 : } // namespace hardio