summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Morrow <acm@mongodb.com>2021-11-02 17:44:08 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-11-04 13:07:12 +0000
commit20549d58943b586749d1570eee834c71bdef1b37 (patch)
tree8d522c88999853190c87d4894bb9238791384e54
parent7bff1ee135ec1520ed4ef7265e6705757e6432e7 (diff)
downloadmongo-20549d58943b586749d1570eee834c71bdef1b37.tar.gz
SERVER-60970 Repair and extend explicit instantiations for BSON builders
-rw-r--r--src/mongo/SConscript1
-rw-r--r--src/mongo/bson/bsonobjbuilder.cpp5
-rw-r--r--src/mongo/bson/bsonobjbuilder.h49
-rw-r--r--src/mongo/bson/util/builder.cpp41
-rw-r--r--src/mongo/bson/util/builder.h36
5 files changed, 128 insertions, 4 deletions
diff --git a/src/mongo/SConscript b/src/mongo/SConscript
index 8a7e3af3a01..4e4ec6aad5f 100644
--- a/src/mongo/SConscript
+++ b/src/mongo/SConscript
@@ -158,6 +158,7 @@ baseEnv.Library(
'bson/simple_bsonelement_comparator.cpp',
'bson/simple_bsonobj_comparator.cpp',
'bson/timestamp.cpp',
+ 'bson/util/builder.cpp',
'logv2/attributes.cpp',
'logv2/bson_formatter.cpp',
'logv2/console.cpp',
diff --git a/src/mongo/bson/bsonobjbuilder.cpp b/src/mongo/bson/bsonobjbuilder.cpp
index ec2cc72c6b8..e8e50fd38a7 100644
--- a/src/mongo/bson/bsonobjbuilder.cpp
+++ b/src/mongo/bson/bsonobjbuilder.cpp
@@ -235,8 +235,7 @@ bool BSONObjBuilderBase<Derived, B>::hasField(StringData name) const {
// Explicit instantiations
template class BSONObjBuilderBase<BSONObjBuilder, BufBuilder>;
template class BSONObjBuilderBase<UniqueBSONObjBuilder, UniqueBufBuilder>;
-
-template class StringBuilderImpl<BufBuilder>;
-template class StringBuilderImpl<StackBufBuilderBase<StackSizeDefault>>;
+template class BSONArrayBuilderBase<BSONArrayBuilder, BSONObjBuilder>;
+template class BSONArrayBuilderBase<UniqueBSONArrayBuilder, UniqueBSONObjBuilder>;
} // namespace mongo
diff --git a/src/mongo/bson/bsonobjbuilder.h b/src/mongo/bson/bsonobjbuilder.h
index 3a527c72850..c4b578e5f35 100644
--- a/src/mongo/bson/bsonobjbuilder.h
+++ b/src/mongo/bson/bsonobjbuilder.h
@@ -686,6 +686,21 @@ protected:
bool _doneCalled = false;
};
+// The following forward declaration exists to enable the extern
+// declaration, which must come before the use of the matching
+// instantiation of the base class of BSONObjBuilder. Do not remove or
+// re-order these lines w.r.t BSONObjBuilderBase or BSONObjBuilder
+// without being sure that you are not undoing the advantages of the
+// extern template declaration.
+class BSONObjBuilder;
+extern template class BSONObjBuilderBase<BSONObjBuilder, BufBuilder>;
+
+// BSONObjBuilder needs this forward declared in order to declare the
+// ArrayBuilder typedef. This forward declaration is also required to
+// allow one of the extern template declarations for
+// BSONArrayBuilderBase below.
+class BSONArrayBuilder;
+
/**
* "Standard" class used for constructing BSONObj on the fly. Stores the BSON in a refcounted
* buffer.
@@ -801,6 +816,21 @@ private:
BSONObjBuilderValueStream _s;
};
+// The following forward declaration exists to enable the extern
+// declaration, which must come before the use of the matching
+// instantiation of the base class of UniqueBSONObjBuilder. Do not
+// remove or re-order these lines w.r.t BSONObjBuilderBase or
+// UniqueBSONObjBuilder without being sure that you are not undoing
+// the advantages of the extern template declaration.
+class UniqueBSONObjBuilder;
+extern template class BSONObjBuilderBase<UniqueBSONObjBuilder, UniqueBufBuilder>;
+
+// UniqueBSONObjBuilder needs this forward declared in order to
+// declare the ArrayBuilder typedef. This forward declaration is also
+// required to allow one of the extern template declarations for
+// BSONArrayBuilderBase below.
+class UniqueBSONArrayBuilder;
+
/**
* Alternative to BSONObjBuilder which uses a non-refcounted buffer (UniqueBuffer) instead of a
* refcounted buffer (SharedBuffer).
@@ -808,7 +838,6 @@ private:
* This should only be used when you care about having direct ownership over the BSONObj's
* underlying memory.
*/
-class UniqueBSONArrayBuilder;
class UniqueBSONObjBuilder : public BSONObjBuilderBase<UniqueBSONObjBuilder, UniqueBufBuilder> {
private:
using Super = BSONObjBuilderBase<UniqueBSONObjBuilder, UniqueBufBuilder>;
@@ -1024,6 +1053,15 @@ protected:
BSONObjBuilderType _b;
};
+// The following extern template declaration must come after the
+// forward declaration of BSONArrayBuilder above, and before the use
+// of the matching instantiation of the base class of
+// BSONArrayBuilder. Do not remove or re-order these lines w.r.t
+// BSONArrayBuilderBase or BSONArrayBuilder without being sure that
+// you are not undoing the advantages of the extern template
+// declaration.
+extern template class BSONArrayBuilderBase<BSONArrayBuilder, BSONObjBuilder>;
+
/**
* "Standard" class used for building BSON arrays.
*/
@@ -1036,6 +1074,15 @@ public:
: BSONArrayBuilderBase<BSONArrayBuilder, BSONObjBuilder>(bufBuilder) {}
};
+// The following extern template declaration must come after the
+// forward delaration of UniqueBSONArrayBuilder above, and before the
+// use of the matching instantiation of the base class of
+// UniqueBSONArrayBuilder. Do not remove or re-order these lines w.r.t
+// BSONArrayBuilderBase or UniqueBSONArrayBuilder without being sure
+// that you are not undoing the advantages of the extern template
+// declaration.
+extern template class BSONArrayBuilderBase<UniqueBSONArrayBuilder, UniqueBSONObjBuilder>;
+
/**
* Alternative to BSONArrayBuilder. This class is analogous to UniqueBSONObjBuilder.
*/
diff --git a/src/mongo/bson/util/builder.cpp b/src/mongo/bson/util/builder.cpp
new file mode 100644
index 00000000000..8b967d498f8
--- /dev/null
+++ b/src/mongo/bson/util/builder.cpp
@@ -0,0 +1,41 @@
+/**
+ * Copyright (C) 2021-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the Server Side Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#include "mongo/bson/util/builder.h"
+
+namespace mongo {
+
+template class BasicBufBuilder<SharedBufferAllocator>;
+template class BasicBufBuilder<SharedBufferFragmentAllocator>;
+template class BasicBufBuilder<UniqueBufferAllocator>;
+template class StackBufBuilderBase<StackSizeDefault>;
+template class StringBuilderImpl<BufBuilder>;
+template class StringBuilderImpl<StackBufBuilderBase<StackSizeDefault>>;
+
+} // namespace mongo
diff --git a/src/mongo/bson/util/builder.h b/src/mongo/bson/util/builder.h
index 5201c30535e..9f3b03bce81 100644
--- a/src/mongo/bson/util/builder.h
+++ b/src/mongo/bson/util/builder.h
@@ -565,6 +565,13 @@ protected:
friend class StringBuilderImpl;
};
+// The following extern template declaration must follow
+// BasicBufBuilder and come before its instantiation as a base class
+// for BufBuilder. Do not remove or re-order these lines w.r.t those
+// types without being sure that you are not undoing the advantages of
+// the extern template declaration.
+extern template class BasicBufBuilder<SharedBufferAllocator>;
+
class BufBuilder : public BasicBufBuilder<SharedBufferAllocator> {
public:
static constexpr size_t kDefaultInitSizeBytes = 512;
@@ -578,6 +585,14 @@ public:
return _buf.release();
}
};
+
+// The following extern template declaration must follow
+// BasicBufBuilder and come before its instantiation as a base class
+// for PooledFragmentBuilder. Do not remove or re-order these lines
+// w.r.t those types without being sure that you are not undoing the
+// advantages of the extern template declaration.
+extern template class BasicBufBuilder<SharedBufferFragmentAllocator>;
+
class PooledFragmentBuilder : public BasicBufBuilder<SharedBufferFragmentAllocator> {
public:
PooledFragmentBuilder(SharedBufferFragmentBuilder& fragmentBuilder)
@@ -589,6 +604,13 @@ public:
};
MONGO_STATIC_ASSERT(std::is_move_constructible_v<BufBuilder>);
+// The following extern template declaration must follow
+// BasicBufBuilder and come before its instantiation as a base class
+// for UniqueBufBuilder. Do not remove or re-order these lines w.r.t
+// those types without being sure that you are not undoing the
+// advantages of the extern template declaration.
+extern template class BasicBufBuilder<UniqueBufferAllocator>;
+
class UniqueBufBuilder : public BasicBufBuilder<UniqueBufferAllocator> {
public:
static constexpr size_t kDefaultInitSizeBytes = 512;
@@ -619,6 +641,14 @@ public:
};
MONGO_STATIC_ASSERT(!std::is_move_constructible<StackBufBuilder>::value);
+// This extern template declaration must follow the declaration of
+// StackBufBuilderBase, and must come before the extern template
+// declarations of StringBuilder below. Do not remove or re-order
+// these lines w.r.t those StackBufBuilderBase or the other extern
+// template declarations without being sure that you are not undoing
+// the advantages of the extern template declaration.
+extern template class StackBufBuilderBase<StackSizeDefault>;
+
/** std::stringstream deals with locale so this is a lot faster than std::stringstream for UTF8 */
template <typename Builder>
class StringBuilderImpl {
@@ -784,6 +814,12 @@ private:
Builder _buf;
};
+
+// The following extern template declaration must follow the
+// declaration of StringBuilderImpl and StackBufBuilderBase along with
+// the extern template delarations for that type. Do not remove or
+// re-order these lines w.r.t those types without being sure that you
+// are not undoing the advantages of the extern template declaration.
extern template class StringBuilderImpl<BufBuilder>;
extern template class StringBuilderImpl<StackBufBuilderBase<StackSizeDefault>>;