LCOV - code coverage report
Current view: top level - src - ring_types.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 12 14 85.7 %
Date: 2024-04-19 08:05:40 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2024 Savoir-faire Linux Inc.
       3             :  *
       4             :  *  Author: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
       5             :  *  Author: Adrien BĂ©raud <adrien.beraud@savoirfairelinux.com>
       6             :  *
       7             :  *  This program is free software; you can redistribute it and/or modify
       8             :  *  it under the terms of the GNU General Public License as published by
       9             :  *  the Free Software Foundation; either version 3 of the License, or
      10             :  *  (at your option) any later version.
      11             :  *
      12             :  *  This program 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             :  *  GNU General Public License for more details.
      16             :  *
      17             :  *  You should have received a copy of the GNU General Public License
      18             :  *  along with this program; if not, write to the Free Software
      19             :  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
      20             :  */
      21             : 
      22             : #ifndef RING_TYPES_H_
      23             : #define RING_TYPES_H_
      24             : 
      25             : #include <type_traits>
      26             : #include <memory>
      27             : #include <mutex>
      28             : #include <cstddef> // for size_t
      29             : 
      30             : #include <ciso646> // fix windows compiler bug
      31             : 
      32             : namespace jami {
      33             : 
      34             : 
      35             : static constexpr size_t SIZEBUF = 16000; /** About 62.5ms of buffering at 48kHz */
      36             : 
      37             : /**
      38             :  * This meta-function is used to enable a template overload
      39             :  * only if given class T is a base of class U
      40             :  */
      41             : template<class T, class U>
      42             : using enable_if_base_of = typename std::enable_if<std::is_base_of<T, U>::value, T>::type;
      43             : 
      44             : /**
      45             :  * Return a shared pointer on an auto-generated global instance of class T.
      46             :  * This instance is created only at usage and destroyed when not,
      47             :  * as we keep only a weak reference on it.
      48             :  * But when created it's always the same object until all holders release
      49             :  * their sharing.
      50             :  * An optional MaxRespawn positive integer can be given to limit the number
      51             :  * of time the object can be created (i.e. different instance).
      52             :  * Any negative values (default) block this effect (unlimited respawn).
      53             :  * This function is thread-safe.
      54             :  */
      55             : template<class T, signed MaxRespawn = -1>
      56             : std::shared_ptr<T>
      57         964 : getGlobalInstance()
      58             : {
      59             :     static std::recursive_mutex mutex; // recursive as instance calls recursively
      60         964 :     static std::weak_ptr<T> wlink;
      61             : 
      62         964 :     std::unique_lock<std::recursive_mutex> lock(mutex);
      63             : 
      64         964 :     if (wlink.expired()) {
      65             :         static signed counter {MaxRespawn};
      66         184 :         if (not counter)
      67           0 :             return nullptr;
      68         184 :         auto link = std::make_shared<T>();
      69         184 :         wlink = link;
      70         184 :         if (counter > 0)
      71           0 :             --counter;
      72         184 :         return link;
      73         184 :     }
      74             : 
      75         780 :     return wlink.lock();
      76         964 : }
      77             : 
      78             : } // namespace jami
      79             : 
      80             : #endif // RING_TYPES_H_

Generated by: LCOV version 1.14