~ubuntu-branches/ubuntu/raring/ceres-solver/raring

« back to all changes in this revision

Viewing changes to include/ceres/internal/manual_constructor.h

  • Committer: Package Import Robot
  • Author(s): Koichi Akabe
  • Date: 2012-06-04 07:15:43 UTC
  • Revision ID: package-import@ubuntu.com-20120604071543-zx6uthupvmtqn3k2
Tags: upstream-1.1.1
ImportĀ upstreamĀ versionĀ 1.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
// Author: kenton@google.com (Kenton Varda)
 
30
//
 
31
// ManualConstructor statically-allocates space in which to store some
 
32
// object, but does not initialize it.  You can then call the constructor
 
33
// and destructor for the object yourself as you see fit.  This is useful
 
34
// for memory management optimizations, where you want to initialize and
 
35
// destroy an object multiple times but only allocate it once.
 
36
//
 
37
// (When I say ManualConstructor statically allocates space, I mean that
 
38
// the ManualConstructor object itself is forced to be the right size.)
 
39
 
 
40
#ifndef CERES_PUBLIC_INTERNAL_MANUAL_CONSTRUCTOR_H_
 
41
#define CERES_PUBLIC_INTERNAL_MANUAL_CONSTRUCTOR_H_
 
42
 
 
43
#include <new>
 
44
 
 
45
namespace ceres {
 
46
namespace internal {
 
47
 
 
48
// ------- Define ALIGNED_CHAR_ARRAY --------------------------------
 
49
 
 
50
#ifndef ALIGNED_CHAR_ARRAY
 
51
 
 
52
// Because MSVC and older GCCs require that the argument to their alignment
 
53
// construct to be a literal constant integer, we use a template instantiated
 
54
// at all the possible powers of two.
 
55
template<int alignment, int size> struct AlignType { };
 
56
template<int size> struct AlignType<0, size> { typedef char result[size]; };
 
57
#if defined(_MSC_VER)
 
58
#define BASE_PORT_H_ALIGN_ATTRIBUTE(X) __declspec(align(X))
 
59
#define BASE_PORT_H_ALIGN_OF(T) __alignof(T)
 
60
#elif defined(__GNUC__)
 
61
#define BASE_PORT_H_ALIGN_ATTRIBUTE(X) __attribute__((aligned(X)))
 
62
#define BASE_PORT_H_ALIGN_OF(T) __alignof__(T)
 
63
#endif
 
64
 
 
65
#if defined(BASE_PORT_H_ALIGN_ATTRIBUTE)
 
66
 
 
67
#define BASE_PORT_H_ALIGNTYPE_TEMPLATE(X) \
 
68
  template<int size> struct AlignType<X, size> { \
 
69
    typedef BASE_PORT_H_ALIGN_ATTRIBUTE(X) char result[size]; \
 
70
  }
 
71
 
 
72
BASE_PORT_H_ALIGNTYPE_TEMPLATE(1);
 
73
BASE_PORT_H_ALIGNTYPE_TEMPLATE(2);
 
74
BASE_PORT_H_ALIGNTYPE_TEMPLATE(4);
 
75
BASE_PORT_H_ALIGNTYPE_TEMPLATE(8);
 
76
BASE_PORT_H_ALIGNTYPE_TEMPLATE(16);
 
77
BASE_PORT_H_ALIGNTYPE_TEMPLATE(32);
 
78
BASE_PORT_H_ALIGNTYPE_TEMPLATE(64);
 
79
BASE_PORT_H_ALIGNTYPE_TEMPLATE(128);
 
80
BASE_PORT_H_ALIGNTYPE_TEMPLATE(256);
 
81
BASE_PORT_H_ALIGNTYPE_TEMPLATE(512);
 
82
BASE_PORT_H_ALIGNTYPE_TEMPLATE(1024);
 
83
BASE_PORT_H_ALIGNTYPE_TEMPLATE(2048);
 
84
BASE_PORT_H_ALIGNTYPE_TEMPLATE(4096);
 
85
BASE_PORT_H_ALIGNTYPE_TEMPLATE(8192);
 
86
// Any larger and MSVC++ will complain.
 
87
 
 
88
#define ALIGNED_CHAR_ARRAY(T, Size) \
 
89
  typename AlignType<BASE_PORT_H_ALIGN_OF(T), sizeof(T) * Size>::result
 
90
 
 
91
#undef BASE_PORT_H_ALIGNTYPE_TEMPLATE
 
92
#undef BASE_PORT_H_ALIGN_ATTRIBUTE
 
93
 
 
94
#else  // defined(BASE_PORT_H_ALIGN_ATTRIBUTE)
 
95
#define ALIGNED_CHAR_ARRAY you_must_define_ALIGNED_CHAR_ARRAY_for_your_compiler
 
96
#endif // defined(BASE_PORT_H_ALIGN_ATTRIBUTE)
 
97
 
 
98
#undef BASE_PORT_H_ALIGNTYPE_TEMPLATE
 
99
#undef BASE_PORT_H_ALIGN_ATTRIBUTE
 
100
 
 
101
#endif  // ALIGNED_CHAR_ARRAY
 
102
 
 
103
template <typename Type>
 
104
class ManualConstructor {
 
105
 public:
 
106
  // No constructor or destructor because one of the most useful uses of
 
107
  // this class is as part of a union, and members of a union cannot have
 
108
  // constructors or destructors.  And, anyway, the whole point of this
 
109
  // class is to bypass these.
 
110
 
 
111
  inline Type* get() {
 
112
    return reinterpret_cast<Type*>(space_);
 
113
  }
 
114
  inline const Type* get() const  {
 
115
    return reinterpret_cast<const Type*>(space_);
 
116
  }
 
117
 
 
118
  inline Type* operator->() { return get(); }
 
119
  inline const Type* operator->() const { return get(); }
 
120
 
 
121
  inline Type& operator*() { return *get(); }
 
122
  inline const Type& operator*() const { return *get(); }
 
123
 
 
124
  // You can pass up to four constructor arguments as arguments of Init().
 
125
  inline void Init() {
 
126
    new(space_) Type;
 
127
  }
 
128
 
 
129
  template <typename T1>
 
130
  inline void Init(const T1& p1) {
 
131
    new(space_) Type(p1);
 
132
  }
 
133
 
 
134
  template <typename T1, typename T2>
 
135
  inline void Init(const T1& p1, const T2& p2) {
 
136
    new(space_) Type(p1, p2);
 
137
  }
 
138
 
 
139
  template <typename T1, typename T2, typename T3>
 
140
  inline void Init(const T1& p1, const T2& p2, const T3& p3) {
 
141
    new(space_) Type(p1, p2, p3);
 
142
  }
 
143
 
 
144
  template <typename T1, typename T2, typename T3, typename T4>
 
145
  inline void Init(const T1& p1, const T2& p2, const T3& p3, const T4& p4) {
 
146
    new(space_) Type(p1, p2, p3, p4);
 
147
  }
 
148
 
 
149
  template <typename T1, typename T2, typename T3, typename T4, typename T5>
 
150
  inline void Init(const T1& p1, const T2& p2, const T3& p3, const T4& p4,
 
151
                   const T5& p5) {
 
152
    new(space_) Type(p1, p2, p3, p4, p5);
 
153
  }
 
154
 
 
155
  template <typename T1, typename T2, typename T3, typename T4, typename T5,
 
156
            typename T6>
 
157
  inline void Init(const T1& p1, const T2& p2, const T3& p3, const T4& p4,
 
158
                   const T5& p5, const T6& p6) {
 
159
    new(space_) Type(p1, p2, p3, p4, p5, p6);
 
160
  }
 
161
 
 
162
  template <typename T1, typename T2, typename T3, typename T4, typename T5,
 
163
            typename T6, typename T7>
 
164
  inline void Init(const T1& p1, const T2& p2, const T3& p3, const T4& p4,
 
165
                   const T5& p5, const T6& p6, const T7& p7) {
 
166
    new(space_) Type(p1, p2, p3, p4, p5, p6, p7);
 
167
  }
 
168
 
 
169
  template <typename T1, typename T2, typename T3, typename T4, typename T5,
 
170
            typename T6, typename T7, typename T8>
 
171
  inline void Init(const T1& p1, const T2& p2, const T3& p3, const T4& p4,
 
172
                   const T5& p5, const T6& p6, const T7& p7, const T8& p8) {
 
173
    new(space_) Type(p1, p2, p3, p4, p5, p6, p7, p8);
 
174
  }
 
175
 
 
176
  template <typename T1, typename T2, typename T3, typename T4, typename T5,
 
177
            typename T6, typename T7, typename T8, typename T9>
 
178
  inline void Init(const T1& p1, const T2& p2, const T3& p3, const T4& p4,
 
179
                   const T5& p5, const T6& p6, const T7& p7, const T8& p8,
 
180
                   const T9& p9) {
 
181
    new(space_) Type(p1, p2, p3, p4, p5, p6, p7, p8, p9);
 
182
  }
 
183
 
 
184
  template <typename T1, typename T2, typename T3, typename T4, typename T5,
 
185
            typename T6, typename T7, typename T8, typename T9, typename T10>
 
186
  inline void Init(const T1& p1, const T2& p2, const T3& p3, const T4& p4,
 
187
                   const T5& p5, const T6& p6, const T7& p7, const T8& p8,
 
188
                   const T9& p9, const T10& p10) {
 
189
    new(space_) Type(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10);
 
190
  }
 
191
 
 
192
  template <typename T1, typename T2, typename T3, typename T4, typename T5,
 
193
            typename T6, typename T7, typename T8, typename T9, typename T10,
 
194
            typename T11>
 
195
  inline void Init(const T1& p1, const T2& p2, const T3& p3, const T4& p4,
 
196
                   const T5& p5, const T6& p6, const T7& p7, const T8& p8,
 
197
                   const T9& p9, const T10& p10, const T11& p11) {
 
198
    new(space_) Type(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11);
 
199
  }
 
200
 
 
201
  inline void Destroy() {
 
202
    get()->~Type();
 
203
  }
 
204
 
 
205
 private:
 
206
  ALIGNED_CHAR_ARRAY(Type, 1) space_;
 
207
};
 
208
 
 
209
#undef ALIGNED_CHAR_ARRAY
 
210
 
 
211
}  // namespace internal
 
212
}  // namespace ceres
 
213
 
 
214
#endif  // CERES_PUBLIC_INTERNAL_MANUAL_CONSTRUCTOR_H_