Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2010, 2011, 2012 Google Inc. All rights reserved. |
| 3 | // http://code.google.com/p/ceres-solver/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright notice, |
| 9 | // this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
| 12 | // and/or other materials provided with the distribution. |
| 13 | // * Neither the name of Google Inc. nor the names of its contributors may be |
| 14 | // used to endorse or promote products derived from this software without |
| 15 | // specific prior written permission. |
| 16 | // |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 21 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | // POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // |
| 30 | // Various Google-specific macros. |
| 31 | // |
| 32 | // This code is compiled directly on many platforms, including client |
| 33 | // platforms like Windows, Mac, and embedded systems. Before making |
| 34 | // any changes here, make sure that you're not breaking any platforms. |
| 35 | |
| 36 | #ifndef CERES_PUBLIC_INTERNAL_MACROS_H_ |
| 37 | #define CERES_PUBLIC_INTERNAL_MACROS_H_ |
| 38 | |
| 39 | #include <cstddef> // For size_t. |
| 40 | |
| 41 | // A macro to disallow the copy constructor and operator= functions |
| 42 | // This should be used in the private: declarations for a class |
| 43 | // |
| 44 | // For disallowing only assign or copy, write the code directly, but declare |
| 45 | // the intend in a comment, for example: |
Sameer Agarwal | 237d659 | 2012-05-30 20:34:49 -0700 | [diff] [blame] | 46 | // |
| 47 | // void operator=(const TypeName&); // _DISALLOW_ASSIGN |
| 48 | |
| 49 | // Note, that most uses of CERES_DISALLOW_ASSIGN and CERES_DISALLOW_COPY |
| 50 | // are broken semantically, one should either use disallow both or |
| 51 | // neither. Try to avoid these in new code. |
| 52 | #define CERES_DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 53 | TypeName(const TypeName&); \ |
| 54 | void operator=(const TypeName&) |
| 55 | |
| 56 | // A macro to disallow all the implicit constructors, namely the |
| 57 | // default constructor, copy constructor and operator= functions. |
| 58 | // |
| 59 | // This should be used in the private: declarations for a class |
| 60 | // that wants to prevent anyone from instantiating it. This is |
| 61 | // especially useful for classes containing only static methods. |
Sameer Agarwal | 237d659 | 2012-05-30 20:34:49 -0700 | [diff] [blame] | 62 | #define CERES_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 63 | TypeName(); \ |
Sameer Agarwal | 237d659 | 2012-05-30 20:34:49 -0700 | [diff] [blame] | 64 | CERES_DISALLOW_COPY_AND_ASSIGN(TypeName) |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 65 | |
| 66 | // The arraysize(arr) macro returns the # of elements in an array arr. |
| 67 | // The expression is a compile-time constant, and therefore can be |
| 68 | // used in defining new arrays, for example. If you use arraysize on |
| 69 | // a pointer by mistake, you will get a compile-time error. |
| 70 | // |
| 71 | // One caveat is that arraysize() doesn't accept any array of an |
| 72 | // anonymous type or a type defined inside a function. In these rare |
| 73 | // cases, you have to use the unsafe ARRAYSIZE() macro below. This is |
| 74 | // due to a limitation in C++'s template system. The limitation might |
| 75 | // eventually be removed, but it hasn't happened yet. |
| 76 | |
| 77 | // This template function declaration is used in defining arraysize. |
| 78 | // Note that the function doesn't need an implementation, as we only |
| 79 | // use its type. |
| 80 | template <typename T, size_t N> |
| 81 | char (&ArraySizeHelper(T (&array)[N]))[N]; |
| 82 | |
| 83 | // That gcc wants both of these prototypes seems mysterious. VC, for |
| 84 | // its part, can't decide which to use (another mystery). Matching of |
| 85 | // template overloads: the final frontier. |
Keir Mierle | efe7ac6 | 2012-06-24 22:25:28 -0700 | [diff] [blame] | 86 | #ifndef _WIN32 |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 87 | template <typename T, size_t N> |
| 88 | char (&ArraySizeHelper(const T (&array)[N]))[N]; |
| 89 | #endif |
| 90 | |
| 91 | #define arraysize(array) (sizeof(ArraySizeHelper(array))) |
| 92 | |
| 93 | // ARRAYSIZE performs essentially the same calculation as arraysize, |
| 94 | // but can be used on anonymous types or types defined inside |
| 95 | // functions. It's less safe than arraysize as it accepts some |
| 96 | // (although not all) pointers. Therefore, you should use arraysize |
| 97 | // whenever possible. |
| 98 | // |
| 99 | // The expression ARRAYSIZE(a) is a compile-time constant of type |
| 100 | // size_t. |
| 101 | // |
| 102 | // ARRAYSIZE catches a few type errors. If you see a compiler error |
| 103 | // |
| 104 | // "warning: division by zero in ..." |
| 105 | // |
| 106 | // when using ARRAYSIZE, you are (wrongfully) giving it a pointer. |
| 107 | // You should only use ARRAYSIZE on statically allocated arrays. |
| 108 | // |
| 109 | // The following comments are on the implementation details, and can |
| 110 | // be ignored by the users. |
| 111 | // |
| 112 | // ARRAYSIZE(arr) works by inspecting sizeof(arr) (the # of bytes in |
| 113 | // the array) and sizeof(*(arr)) (the # of bytes in one array |
| 114 | // element). If the former is divisible by the latter, perhaps arr is |
| 115 | // indeed an array, in which case the division result is the # of |
| 116 | // elements in the array. Otherwise, arr cannot possibly be an array, |
| 117 | // and we generate a compiler error to prevent the code from |
| 118 | // compiling. |
| 119 | // |
| 120 | // Since the size of bool is implementation-defined, we need to cast |
| 121 | // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final |
| 122 | // result has type size_t. |
| 123 | // |
| 124 | // This macro is not perfect as it wrongfully accepts certain |
| 125 | // pointers, namely where the pointer size is divisible by the pointee |
| 126 | // size. Since all our code has to go through a 32-bit compiler, |
| 127 | // where a pointer is 4 bytes, this means all pointers to a type whose |
| 128 | // size is 3 or greater than 4 will be (righteously) rejected. |
| 129 | // |
| 130 | // Kudos to Jorg Brown for this simple and elegant implementation. |
| 131 | // |
| 132 | // - wan 2005-11-16 |
| 133 | // |
Keir Mierle | efe7ac6 | 2012-06-24 22:25:28 -0700 | [diff] [blame] | 134 | // Starting with Visual C++ 2005, WinNT.h includes ARRAYSIZE. However, |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 135 | // the definition comes from the over-broad windows.h header that |
Keir Mierle | efe7ac6 | 2012-06-24 22:25:28 -0700 | [diff] [blame] | 136 | // introduces a macro, ERROR, that conflicts with the logging framework |
| 137 | // that Ceres uses. Instead, rename ARRAYSIZE to CERES_ARRAYSIZE. |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 138 | #define CERES_ARRAYSIZE(a) \ |
| 139 | ((sizeof(a) / sizeof(*(a))) / \ |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 140 | static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 141 | |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 142 | // Tell the compiler to warn about unused return values for functions |
| 143 | // declared with this macro. The macro should be used on function |
| 144 | // declarations following the argument list: |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 145 | // |
| 146 | // Sprocket* AllocateSprocket() MUST_USE_RESULT; |
| 147 | // |
| 148 | #undef MUST_USE_RESULT |
| 149 | #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) \ |
| 150 | && !defined(COMPILER_ICC) |
| 151 | #define MUST_USE_RESULT __attribute__ ((warn_unused_result)) |
| 152 | #else |
| 153 | #define MUST_USE_RESULT |
| 154 | #endif |
| 155 | |
Sameer Agarwal | eb89340 | 2012-06-17 08:55:01 -0700 | [diff] [blame] | 156 | // Platform independent macros to get aligned memory allocations. |
| 157 | // For example |
| 158 | // |
| 159 | // MyFoo my_foo CERES_ALIGN_ATTRIBUTE(16); |
| 160 | // |
| 161 | // Gives us an instance of MyFoo which is aligned at a 16 byte |
| 162 | // boundary. |
| 163 | #if defined(_MSC_VER) |
| 164 | #define CERES_ALIGN_ATTRIBUTE(n) __declspec(align(n)) |
| 165 | #define CERES_ALIGN_OF(T) __alignof(T) |
| 166 | #elif defined(__GNUC__) |
| 167 | #define CERES_ALIGN_ATTRIBUTE(n) __attribute__((aligned(n))) |
| 168 | #define CERES_ALIGN_OF(T) __alignof(T) |
| 169 | #endif |
| 170 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 171 | #endif // CERES_PUBLIC_INTERNAL_MACROS_H_ |