Repository: xbmc/xbmc
File: xbmc/guilib/listproviders/DirectoryProvider.cpp (C++)

Code before:

class CPVRSubscriber : public CDirectoryProvider::CSubscriber
{
public:
  explicit CPVRSubscriber(ISubscriberCallback& invalidate)
    : CDirectoryProvider::CSubscriber(invalidate)
  {
    CServiceBroker::GetPVRManager().Events().Subscribe(this, &CPVRSubscriber::OnPVRManagerEvent);
  }
  ~CPVRSubscriber() override { CServiceBroker::GetPVRManager().Events().Unsubscribe(this); }

private:
  void OnPVRManagerEvent(const PVR::PVREvent& event)
  {
    if (event == PVR::PVREvent::ManagerStarted || event == PVR::PVREvent::ManagerStopped ||
        event == PVR::PVREvent::RecordingsInvalidated ||
        event == PVR::PVREvent::TimersInvalidated ||
        event == PVR::PVREvent::ChannelGroupsInvalidated ||
        event == PVR::PVREvent::SavedSearchesInvalidated ||
        event == PVR::PVREvent::ClientsInvalidated ||
        event == PVR::PVREvent::ClientsPrioritiesInvalidated)
    {
      OnEventPublished();
    }
  }
};

class CFavouritesSubscriber : public CDirectoryProvider::CSubscriber
{
public:
  explicit CFavouritesSubscriber(ISubscriberCallback& invalidate)
    : CDirectoryProvider::CSubscriber(invalidate)
  {
    CServiceBroker::GetFavouritesService().Events().Subscribe(
        this, &CFavouritesSubscriber::OnFavouritesEvent);
  }
  ~CFavouritesSubscriber() override
  {
    CServiceBroker::GetFavouritesService().Events().Unsubscribe(this);
  }

private:
  void OnFavouritesEvent(const CFavouritesService::FavouritesUpdated& event) { OnEventPublished(); }
};

std::unique_ptr<CDirectoryProvider::CSubscriber> GetSubscriber(const std::string& url,
                                                               ISubscriberCallback& invalidate)
{
  if (URIUtils::IsProtocol(url, "addons"))
    return std::make_unique<CAddonsSubscriber>(invalidate);
  else if (URIUtils::IsProtocol(url, "pvr"))

Review comment (suggestion):
The use of m_pvrStarted.test() may be problematic since std::atomic_flag does not define a test() function in standard C++. Consider switching to std::atomic<bool> or an alternative method to check the flag state.
```suggestion
  bool IsReadyToUse() const override { return m_pvrStarted.load(); }

private:
  void OnPVRManagerEvent(const PVR::PVREvent& event)
  {
    if (event == PVR::PVREvent::ManagerStarted)
      m_pvrStarted.store(true);
    else if (event == PVR::PVREvent::ManagerStarting || event == PVR::PVREvent::ManagerStopping ||
             event == PVR::PVREvent::ManagerStopped)
      m_pvrStarted.store(false);

    if (!m_pvrStarted.load())
```

Code after:
    OnEventPublished();
  }
};

class CPVRSubscriber : public CDirectoryProvider::CSubscriber
{
public:
  explicit CPVRSubscriber(ISubscriberCallback& invalidate)
    : CDirectoryProvider::CSubscriber(invalidate)
  {
    CServiceBroker::GetPVRManager().Events().Subscribe(this, &CPVRSubscriber::OnPVRManagerEvent);
  }
  ~CPVRSubscriber() override { CServiceBroker::GetPVRManager().Events().Unsubscribe(this); }

  bool IsReadyToUse() const override { return m_pvrStarted.test(); }

private:
  void OnPVRManagerEvent(const PVR::PVREvent& event)
  {
    if (event == PVR::PVREvent::ManagerStarted)
      m_pvrStarted.test_and_set();
    else if (event == PVR::PVREvent::ManagerStarting || event == PVR::PVREvent::ManagerStopping ||
             event == PVR::PVREvent::ManagerStopped)
      m_pvrStarted.clear();

    if (!m_pvrStarted.test())
      return;

    if (event == PVR::PVREvent::ManagerStarted || event == PVR::PVREvent::ManagerStopped ||
        event == PVR::PVREvent::RecordingsInvalidated ||
        event == PVR::PVREvent::TimersInvalidated ||
        event == PVR::PVREvent::ChannelGroupsInvalidated ||
        event == PVR::PVREvent::SavedSearchesInvalidated ||
        event == PVR::PVREvent::ClientsInvalidated ||
        event == PVR::PVREvent::ClientsPrioritiesInvalidated)
    {
      OnEventPublished();
    }
  }

  std::atomic_flag m_pvrStarted{};
};

class CFavouritesSubscriber : public CDirectoryProvider::CSubscriber
{
public:
  explicit CFavouritesSubscriber(ISubscriberCallback& invalidate)
    : CDirectoryProvider::CSubscriber(invalidate)
  {
    CServiceBroker::GetFavouritesService().Events().Subscribe(
        this, &CFavouritesSubscriber::OnFavouritesEvent);