Commit 722ef0f3 authored by Dave Watson's avatar Dave Watson Committed by Sara Golemon

move MoveWrapper to folly

Summary: Add MoveWrapper to folly.

@override-unit-failures

Test Plan: arc unit

Reviewed By: hans@fb.com

FB internal diff: D1030005
parent 8fcf9c21
/*
* Copyright 2013 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <memory>
namespace folly {
/** C++11 closures don't support move-in capture. Nor does std::bind.
facepalm.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3610.html
"[...] a work-around that should make people's stomach crawl:
write a wrapper that performs move-on-copy, much like the deprecated
auto_ptr"
Unlike auto_ptr, this doesn't require a heap allocation.
*/
template <class T>
class MoveWrapper {
public:
/** If value can be default-constructed, why not?
Then we don't have to move it in */
MoveWrapper() = default;
/// Move a value in.
explicit
MoveWrapper(T&& t) : value(std::move(t)) {}
/// copy is move
MoveWrapper(MoveWrapper& other) : value(std::move(other.value)) {}
/// move is also move
MoveWrapper(MoveWrapper&& other) : value(std::move(other.value)) {}
const T& operator*() const { return value; }
T& operator*() { return value; }
const T* operator->() const { return &value; }
T* operator->() { return &value; }
// If you want these you're probably doing it wrong, though they'd be
// easy enough to implement
MoveWrapper& operator=(MoveWrapper const&) = delete;
MoveWrapper& operator=(MoveWrapper&&) = delete;
private:
T value;
};
template <class T>
MoveWrapper<T> makeMoveWrapper(T&& t) {
return MoveWrapper<T>(std::forward<T>(t));
}
} // namespace
/*
* Copyright 2013 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include "folly/MoveWrapper.h"
#include <memory>
namespace folly {
TEST(makeMoveWrapper, Empty) {
// checks for crashes
auto p = makeMoveWrapper(std::unique_ptr<int>());
}
TEST(makeMoveWrapper, NonEmpty) {
auto u = std::unique_ptr<int>(new int(5));
EXPECT_EQ(*u, 5);
auto p = makeMoveWrapper(std::move(u));
EXPECT_TRUE(!u);
EXPECT_EQ(**p, 5);
}
} // namespace
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment