1/**
2 * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved.
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 * \author Hechong.xyf
17 * \date Aug 2019
18 * \brief Interface of AiLego Utility Singleton
19 */
20
21#ifndef __AILEGO_PATTERN_SINGLETON_H__
22#define __AILEGO_PATTERN_SINGLETON_H__
23
24#include <type_traits>
25
26namespace ailego {
27
28/*! Singleton (C++11)
29 */
30template <typename T>
31class Singleton {
32 public:
33 using ObjectType = typename std::remove_reference<T>::type;
34
35 //! Retrieve instance of object
36 static ObjectType &Instance(void) noexcept(
37 std::is_nothrow_constructible<ObjectType>::value) {
38 // Since it's a static variable, if the class has already been created,
39 // it won't be created again. And it is thread-safe in C++11.
40 static ObjectType obj;
41 return obj;
42 }
43
44 protected:
45 //! Constructor (Allow inheritance)
46 Singleton(void) {}
47
48 private:
49 //! Disable them
50 Singleton(Singleton const &) = delete;
51 Singleton(Singleton &&) = delete;
52 Singleton &operator=(Singleton const &) = delete;
53 Singleton &operator=(Singleton &&) = delete;
54};
55
56} // namespace ailego
57
58#endif // __AILEGO_PATTERN_SINGLETON_H__
59