LCOV - code coverage report
Current view: top level - include/core/hardio/common - protocol.h (source / functions) Hit Total Coverage
Test: hardiocore_coverage Lines: 13 13 100.0 %
Date: 2025-05-26 19:00:29 Functions: 38 39 97.4 %

          Line data    Source code
       1             : /*      File: protocol.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/protocol.h
      23             :  * @author Robin Passama
      24             :  * @brief header file for Protocol class
      25             :  * @date May 2025
      26             :  * @ingroup  core
      27             :  */
      28             : #pragma once
      29             : 
      30             : #include <hardio/common/interface.h>
      31             : #include <cstdint>
      32             : #include <memory>
      33             : #include <vector>
      34             : 
      35             : using namespace std::literals::string_literals;
      36             : namespace hardio {
      37             : 
      38             : /**
      39             :  * @brief A protocol is a way devices can communicate through a specific
      40             :  * interface.
      41             :  * @details this class is the abstrct class for all protocols.
      42             :  *
      43             :  */
      44             : class Protocol : public std::enable_shared_from_this<Protocol> {
      45             : public:
      46             :     Protocol() = delete;
      47             :     Protocol(const Protocol&) = delete;
      48         200 :     virtual ~Protocol() = default;
      49             : 
      50             :     /**
      51             :      * @brief add an access point, called interface, that uses this protocol
      52             :      *
      53             :      * @tparam T type of Interface
      54             :      * @tparam Args types of additional arguments used when creating the
      55             :      * procotol
      56             :      * @param args additional arguments used when creating protocol
      57             :      * @return T& the Interface object newly created
      58             :      */
      59             :     template <typename T, typename... Args>
      60         341 :     T& add_interface(Args&&... args) {
      61             :         using Prot = typename T::protocol_type;
      62         682 :         auto new_interface = std::make_shared<T>(std::forward<Args>(args)...);
      63         682 :         auto specialized_prot = this->ptr<Prot>();
      64         341 :         new_interface->set_protocol(specialized_prot);
      65         371 :         std::string reason = "";
      66         341 :         if (not specialized_prot->manage_add_interface(new_interface, reason)) {
      67             :             throw std::runtime_error(
      68             :                 "Protocol::add_interface: invalid specification "
      69             :                 "for interface with protocol "s +
      70          30 :                 std::string(pid::type_name<Prot>()) + ". Reason: "s + reason);
      71             :         }
      72         311 :         auto mem = new_interface.get();
      73         311 :         provided_interfaces_.push_back(std::move(new_interface));
      74         622 :         return *mem;
      75             :     }
      76             : 
      77             :     /**
      78             :      * @brief get a shared pointer to this protocol
      79             :      *
      80             :      * @tparam T type of the protocol
      81             :      * @return the std::shared_ptr<T> of this
      82             :      * @see ref()
      83             :      */
      84             :     template <typename T>
      85         428 :     std::shared_ptr<T> ptr() {
      86         428 :         return std::dynamic_pointer_cast<T>(shared_from_this());
      87             :     }
      88             : 
      89             :     /**
      90             :      * @brief get a reference to this protocol
      91             :      *
      92             :      * @tparam T the concrete type of this protocol
      93             :      * @return the reference over this
      94             :      * @see ptr()
      95             :      */
      96             :     template <typename T>
      97             :     T& ref() {
      98             :         return *static_cast<T*>(shared_from_this().get());
      99             :     }
     100             : 
     101             :     /**
     102             :      * @brief check if the protocol can be bound to the given interface
     103             :      * @details this function must be specialized by each concrete protocol
     104             :      * class
     105             :      * @param [in] other the interface to bind
     106             :      * @param [out] reason the string explaining the problem if an error
     107             :      * occurred
     108             :      * @return true if binding allowed, false otherwise
     109             :      */
     110             :     virtual bool can_bind_to(const std::shared_ptr<Interface>& other,
     111             :                              std::string& reason) const = 0;
     112             : 
     113             :     /**
     114             :      * @brief get the name of the protocol
     115             :      *
     116             :      * @return std::string the name
     117             :      */
     118             :     virtual std::string name() const = 0;
     119             : 
     120             :     /**
     121             :      * @brief get the unique id of the protocol
     122             :      *
     123             :      * @return std::string the id
     124             :      */
     125             :     virtual std::string id() const;
     126             : 
     127             :     /**
     128             :      * @brief function called when the protocol is bound to an interface
     129             :      * @details this function is for internal use only
     130             :      * @param [in] other lower level interface this protocol uses
     131             :      */
     132             :     void uses(const std::shared_ptr<Interface>& other);
     133             : 
     134             :     /**
     135             :      * @brief User defined function called when the protocol is bound to an
     136             :      * interface
     137             :      * @details this function is used to defined specific behavior when a
     138             :      * protocol is bound ot an interface
     139             :      * @param [in] other lower level interface this protocol uses
     140             :      */
     141             :     virtual void custom_uses(const std::shared_ptr<Interface>& other);
     142             : 
     143             :     /**
     144             :      * @brief User defined function called when the protocol is initialized
     145             :      * @details this function is used to defined specific behavior when a
     146             :      * protocol is initialized
     147             :      * @param [out] reason the string explaining the problem if an error occur
     148             :      * @param [in] managed_thread true if at initialization the protocol handles
     149             :      * automatically threads fo communication, false otherwise (user managed
     150             :      * threads)
     151             :      * @return true if initialization is successful, false otherwise
     152             :      */
     153             :     virtual bool custom_initialization(std::string& reason,
     154             :                                        bool managed_thread = true);
     155             : 
     156             :     virtual void custom_finish();
     157             :     uint64_t type_id() const;
     158             :     const std::vector<std::shared_ptr<Interface>>& provided_interfaces() const;
     159             : 
     160             :     /**
     161             :      * @brief initializing the protocol
     162             :      * @details this function is for internal use only
     163             :      * @param reason  the string explaining the problem if an error occur
     164             :      * @param managed_thread true if at initialization the protocol handles
     165             :      * automatically threads fo communication, false otherwise (user managed
     166             :      * threads)
     167             :      * @return true if initialization is successful, false otherwise
     168             :      * @see finish()
     169             :      * @see custom_initialization()
     170             :      */
     171             :     bool initialize(std::string& reason, bool managed_thread = true);
     172             : 
     173             :     /**
     174             :      * @brief stop using the protocol
     175             :      * @details this function is for internal use only
     176             :      * @see initialize()
     177             :      * @see custom_finish()
     178             :      */
     179             :     void finish();
     180             : 
     181             :     /**
     182             :      * @brief get the interface used by this protocol
     183             :      *
     184             :      * @return the std::shared_ptr<Interface> of the interface used
     185             :      */
     186             :     std::shared_ptr<Interface> used_interface() const;
     187             : 
     188             : protected:
     189             :     /**
     190             :      * @brief Construct a new Protocol object
     191             :      *
     192             :      * @param type_id id of teh protocol concrete type
     193             :      */
     194             :     Protocol(uint64_t type_id);
     195             : 
     196             : private:
     197             :     std::vector<std::shared_ptr<Interface>> provided_interfaces_;
     198             :     std::weak_ptr<Interface> used_interface_;
     199             :     uint64_t type_id_;
     200             : };
     201             : 
     202             : } // namespace hardio

Generated by: LCOV version 1.16