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

ash / public / cpp / scoped_singleton_resetter_for_test.h [blame]

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_PUBLIC_CPP_SCOPED_SINGLETON_RESETTER_FOR_TEST_H_
#define ASH_PUBLIC_CPP_SCOPED_SINGLETON_RESETTER_FOR_TEST_H_

#include "base/memory/raw_ptr.h"

namespace ash {

// Helper class to reset singleton instance in constructor and restore it in
// destructor so that tests could create its own instance.
template <typename T>
class ASH_PUBLIC_EXPORT ScopedSingletonResetterForTest {
 public:
  ScopedSingletonResetterForTest() : instance_(GetGlobalInstanceHolder()) {
    GetGlobalInstanceHolder() = nullptr;
  }

  ~ScopedSingletonResetterForTest() { GetGlobalInstanceHolder() = instance_; }

 private:
  // Override this method to provide pointer holding singleton instance.
  T*& GetGlobalInstanceHolder();
  const raw_ptr<T> instance_;
};

}  // namespace ash

#endif  // ASH_PUBLIC_CPP_SCOPED_SINGLETON_RESETTER_FOR_TEST_H_