summaryrefslogtreecommitdiffstats
path: root/xbmc/utils/ComponentContainer.h
blob: 3aa826a82d9a498862564c954d13d72d677b11e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 *  Copyright (C) 2022 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#pragma once

#include "threads/CriticalSection.h"

#include <cstddef>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <typeindex>
#include <unordered_map>
#include <utility>

//! \brief A generic container for components.
//! \details A component has to be derived from the BaseType.
//!          Only a single instance of each derived type can be registered.
//!          Intended use is through inheritance.
template<class BaseType>
class CComponentContainer
{
public:
  //! \brief Obtain a component.
  template<class T>
  std::shared_ptr<T> GetComponent()
  {
    return std::const_pointer_cast<T>(std::as_const(*this).template GetComponent<T>());
  }

  //! \brief Obtain a component.
  template<class T>
  std::shared_ptr<const T> GetComponent() const
  {
    std::unique_lock<CCriticalSection> lock(m_critSection);
    const auto it = m_components.find(std::type_index(typeid(T)));
    if (it != m_components.end())
      return std::static_pointer_cast<const T>((*it).second);

    throw std::logic_error("ComponentContainer: Attempt to obtain non-existent component");
  }

  //! \brief Returns number of registered components.
  std::size_t size() const { return m_components.size(); }

protected:
  //! \brief Register a new component instance.
  void RegisterComponent(const std::shared_ptr<BaseType>& component)
  {
    if (!component)
      return;

    // Note: Extra var needed to avoid clang warning
    // "Expression with side effects will be evaluated despite being used as an operand to 'typeid'"
    // https://stackoverflow.com/questions/46494928/clang-warning-on-expression-side-effects
    const auto& componentRef = *component;

    std::unique_lock<CCriticalSection> lock(m_critSection);
    m_components.insert({std::type_index(typeid(componentRef)), component});
  }

  //! \brief Deregister a component.
  void DeregisterComponent(const std::type_info& typeInfo)
  {
    std::unique_lock<CCriticalSection> lock(m_critSection);
    m_components.erase(typeInfo);
  }

private:
  mutable CCriticalSection m_critSection; //!< Critical section for map updates
  std::unordered_map<std::type_index, std::shared_ptr<BaseType>>
      m_components; //!< Map of components
};