Apollo  6.0
Open source self driving car software
macros.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. 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 
17 #ifndef CYBER_COMMON_MACROS_H_
18 #define CYBER_COMMON_MACROS_H_
19 
20 #include <iostream>
21 #include <memory>
22 #include <mutex>
23 #include <type_traits>
24 #include <utility>
25 
26 #include "cyber/base/macros.h"
27 
28 DEFINE_TYPE_TRAIT(HasShutdown, Shutdown)
29 
30 template <typename T>
31 typename std::enable_if<HasShutdown<T>::value>::type CallShutdown(T *instance) {
32  instance->Shutdown();
33 }
34 
35 template <typename T>
37  T *instance) {
38  (void)instance;
39 }
40 
41 // There must be many copy-paste versions of these macros which are same
42 // things, undefine them to avoid conflict.
43 #undef UNUSED
44 #undef DISALLOW_COPY_AND_ASSIGN
45 
46 #define UNUSED(param) (void)param
47 
48 #define DISALLOW_COPY_AND_ASSIGN(classname) \
49  classname(const classname &) = delete; \
50  classname &operator=(const classname &) = delete;
51 
52 #define DECLARE_SINGLETON(classname) \
53  public: \
54  static classname *Instance(bool create_if_needed = true) { \
55  static classname *instance = nullptr; \
56  if (!instance && create_if_needed) { \
57  static std::once_flag flag; \
58  std::call_once(flag, \
59  [&] { instance = new (std::nothrow) classname(); }); \
60  } \
61  return instance; \
62  } \
63  \
64  static void CleanUp() { \
65  auto instance = Instance(false); \
66  if (instance != nullptr) { \
67  CallShutdown(instance); \
68  } \
69  } \
70  \
71  private: \
72  classname(); \
73  DISALLOW_COPY_AND_ASSIGN(classname)
74 
75 #endif // CYBER_COMMON_MACROS_H_
#define DEFINE_TYPE_TRAIT(name, func)
Definition: macros.h:33
std::enable_if< HasShutdown< T >::value >::type CallShutdown(T *instance)
Definition: macros.h:31
apollo::cyber::base::std value