singleton.h revision c7c70aa903fdef3a6074cdb06d266b6ad3821d27
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef CHRE_UTIL_SINGLETON_H_
18#define CHRE_UTIL_SINGLETON_H_
19
20#include <type_traits>
21
22#include "chre/util/non_copyable.h"
23
24namespace chre {
25
26/**
27 * The Singleton template provides static storage for one instance of the
28 * provided type. Initialization does not happen automatically which allows
29 * users of this API to control the order of initialization.
30 *
31 * Caution is recommended when using this class to avoid Singleton hell. In
32 * many cases there is a better solution than using a Singleton in your design.
33 * One good use of this class is for a class that owns the state of your
34 * application (the "root" of a tree of object ownership).
35 */
36template<typename ObjectType>
37class Singleton : public NonCopyable {
38 public:
39  /**
40   * Constructs the object in the space provided by this container. If the
41   * object is already constructed, no operation is performed. Use the
42   * isInitialized method to determine if construction is required.
43   *
44   * @param args The constructor arguments to pass to the singleton instance.
45   */
46  template<typename... Args>
47  static void init(Args&&... args);
48
49  /**
50   * Invokes the destructor on the underlying object if it has been constructed
51   * already.
52   */
53  static void deinit();
54
55  /**
56   * Returns whether or not this singleton instance has been constructed.
57   *
58   * @return Returns true if the object has already been constructed.
59   */
60  static bool isInitialized();
61
62  /**
63   * Returns a pointer to the underlying object. The singleton object must be
64   * constructed prior to using get to interact with it. If the object is not
65   * initialized, nullptr is returned.
66   *
67   * @return A pointer to the singleton instance.
68   */
69  static ObjectType *get();
70
71 private:
72  //! Static storage for the type of this singleton.
73  static typename std::aligned_storage<
74      sizeof(ObjectType), alignof(ObjectType)>::type sObject;
75
76  //! Static storage for the initialized state of this singleton.
77  static bool sIsInitialized;
78};
79
80}  // namespace chre
81
82#include "chre/util/singleton_impl.h"
83
84#endif  // CHRE_UTIL_SINGLETON_H_
85