1 #ifndef CAFFE2_CORE_COMMON_H_ 2 #define CAFFE2_CORE_COMMON_H_ 11 #include <type_traits> 15 #include <TargetConditionals.h> 26 #include "caffe2/core/macros.h" 32 typedef int64_t TIndex;
39 template <
typename Key,
typename Value>
40 using CaffeMap = std::map<Key, Value>;
49 using std::unique_ptr;
53 #define CAFFE_NOT_IMPLEMENTED CAFFE_THROW("Not Implemented.") 60 #define CAFFE2_UNUSED __attribute__((__unused__)) 61 #define CAFFE2_USED __attribute__((__used__)) 66 #ifndef DISABLE_COPY_AND_ASSIGN 67 #define DISABLE_COPY_AND_ASSIGN(classname) \ 69 classname(const classname&) = delete; \ 70 classname& operator=(const classname&) = delete 74 #if !defined(CAFFE2_MOBILE) 75 #if defined(__ANDROID__) 76 #define CAFFE2_ANDROID 1 77 #define CAFFE2_MOBILE 1 78 #elif (defined(__APPLE__) && \ 79 (TARGET_IPHONE_SIMULATOR || TARGET_OS_SIMULATOR || TARGET_OS_IPHONE)) 81 #define CAFFE2_MOBILE 1 82 #elif (defined(__APPLE__) && TARGET_OS_MAC) 84 #define CAFFE2_MOBILE 0 86 #define CAFFE2_MOBILE 0 87 #endif // ANDROID / IOS / MACOS 88 #endif // CAFFE2_MOBILE 92 #define CAFFE2_ALIGNED(x) __declspec(align(x)) 94 #define CAFFE2_ALIGNED(x) __attribute__((aligned(x))) 101 #ifndef __GNUC_PREREQ 102 #if defined __GNUC__ && defined __GNUC_MINOR__ 103 #define __GNUC_PREREQ(maj, min) \ 104 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) 106 #define __GNUC_PREREQ(maj, min) 0 113 #if defined(_MSC_VER) 114 #if defined(CAFFE2_BUILD_SHARED_LIBS) 115 #define CAFFE2_EXPORT __declspec(dllexport) 116 #define CAFFE2_IMPORT __declspec(dllimport) 118 #define CAFFE2_EXPORT 119 #define CAFFE2_IMPORT 122 #if defined(__GNUC__) 123 #if __GNUC_PREREQ(4, 9) 124 #define CAFFE2_EXPORT [[gnu::visibility("default")]] 126 #define CAFFE2_EXPORT __attribute__((__visibility__("default"))) 129 #define CAFFE2_EXPORT 131 #define CAFFE2_IMPORT CAFFE2_EXPORT 143 #ifdef CAFFE2_BUILD_MAIN_LIB 144 #define CAFFE2_API CAFFE2_EXPORT 146 #define CAFFE2_API CAFFE2_IMPORT 151 #if __cplusplus >= 201402L || \ 152 (defined __cpp_lib_make_unique && __cpp_lib_make_unique >= 201304L) || \ 153 (defined(_MSC_VER) && _MSC_VER >= 1900) 155 using std::make_unique;
158 template<
typename T,
typename... Args>
159 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
160 make_unique(Args&&... args) {
161 return std::unique_ptr<T>(
new T(std::forward<Args>(args)...));
166 typename std::enable_if<std::is_array<T>::value, std::unique_ptr<T>>::type
167 make_unique(
const size_t n) {
168 return std::unique_ptr<T>(
new typename std::remove_extent<T>::type[n]());
172 template<
typename T,
typename... Args>
173 typename std::enable_if<
174 std::extent<T>::value != 0, std::unique_ptr<T>>::type
175 make_unique(Args&&...) =
delete;
182 #if defined(__ANDROID__) || defined(CAFFE2_TESTONLY_FORCE_STD_STRING_TEST) 183 #define CAFFE2_TESTONLY_WE_ARE_USING_CUSTOM_STRING_FUNCTIONS 1 184 template <
typename T>
185 std::string to_string(T value)
187 std::ostringstream os;
192 inline int stoi(
const string& str) {
193 std::stringstream ss;
200 inline double stod(
const string& str, std::size_t* pos = 0) {
201 std::stringstream ss;
206 if (ss.tellg() == std::streampos(-1)) {
215 #define CAFFE2_TESTONLY_WE_ARE_USING_CUSTOM_STRING_FUNCTIONS 0 216 using std::to_string;
219 #endif // defined(__ANDROID__) || defined(CAFFE2_FORCE_STD_STRING_FALLBACK_TEST) 222 template <
typename Dst,
typename Src>
223 inline Dst dynamic_cast_if_rtti(Src ptr) {
225 return dynamic_cast<Dst
>(ptr);
227 return reinterpret_cast<Dst
>(ptr);
234 template <
int... values>
238 static inline bool ContainsInternal(
const int i) {
241 template <
int First,
int Second,
int... Rest>
242 static inline bool ContainsInternal(
const int i) {
243 return (i == First) && ContainsInternal<Second, Rest...>(i);
247 static inline bool Contains(
const int i) {
248 return ContainsInternal<values...>(i);
255 static inline bool Contains(
const int ) {
264 bool HasCudaRuntime();
269 void SetCudaRuntimeFlag();
273 const std::map<string, string>& GetBuildOptions();
277 #endif // CAFFE2_CORE_COMMON_H_
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...