-
Notifications
You must be signed in to change notification settings - Fork 613
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move managed_object_ptr → managed_ptr.h
Summary: These functionality seem generally useful outside of simply interacting with `object.h`. Indeed in future diff, the (arbitrary) restriction that `T` be `whisker::object` or one of its alternatives is relaxed. Change is no-op. Reviewed By: thedavekwon Differential Revision: D68078565 fbshipit-source-id: ac1422ab63f72deb490e397376952388de2887da
- Loading branch information
1 parent
9cab624
commit 5f2a309
Showing
10 changed files
with
175 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* 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> | ||
#include <tuple> | ||
#include <utility> | ||
|
||
namespace whisker { | ||
|
||
/** | ||
* A managed_ptr<T> represents one of: | ||
* 1. A "static"-ally managed object. See manage_as_static(...). | ||
* 2. An owned instance of T. See manage_owned(...). | ||
* 3. A derived instance of T, that is a sub-object of another managed object. | ||
* See manage_derived(...) and manage_derived_ref(...). | ||
* | ||
* All three cases are represented by shared_ptr. See `folly::MaybeManagedPtr`. | ||
* (1) and (3) use the aliasing constructor of shared_ptr. | ||
*/ | ||
template <typename T> | ||
using managed_ptr = std::shared_ptr<const T>; | ||
|
||
/** | ||
* Returns a pointer which is an unmanaged reference to the provided | ||
* object. | ||
* | ||
* The caller must guarantee that the provided object is kept alive as long as | ||
* the returned pointer is used. | ||
*/ | ||
template <typename T> | ||
managed_ptr<T> manage_as_static(const T& o) { | ||
return managed_ptr<T>(managed_ptr<void>(), std::addressof(o)); | ||
} | ||
|
||
/** | ||
* Returns a pointer which directly owns an instance of T. | ||
*/ | ||
template <typename T, typename... Args> | ||
managed_ptr<T> manage_owned(Args&&... args) { | ||
return std::make_shared<const T>(std::forward<Args>(args)...); | ||
} | ||
|
||
/** | ||
* Returns a pointer where the lifetime of `value` depends on one or more other | ||
* objects. This means that `from` must outlive `value`. | ||
* | ||
* The caller must guarantee that the provided object is kept alive as long as | ||
* the returned pointer is used. | ||
*/ | ||
template <typename T, typename U> | ||
managed_ptr<T> manage_derived_ref(const managed_ptr<U>& from, const T& value) { | ||
return managed_ptr<T>(from, std::addressof(value)); | ||
} | ||
// Same as above except derived from multiple objects. | ||
template <typename T, typename... U> | ||
managed_ptr<T> manage_derived_ref( | ||
std::tuple<managed_ptr<U>...> froms, const T& value) { | ||
return managed_ptr<T>( | ||
std::make_shared<std::tuple<managed_ptr<U>...>>(std::move(froms)), | ||
std::addressof(value)); | ||
} | ||
|
||
/** | ||
* Returns a pointer where the lifetime of `value` depends on one or more other | ||
* objects. This means that `from` must outlive `value`. | ||
*/ | ||
template <typename T, typename U> | ||
managed_ptr<T> manage_derived( | ||
const managed_ptr<U>& from, managed_ptr<T> value_ptr) { | ||
const T& value = *value_ptr; | ||
return manage_derived_ref( | ||
std::tuple<managed_ptr<U>, managed_ptr<T>>(from, std::move(value_ptr)), | ||
value); | ||
} | ||
// Same as above except derived from multiple objects. | ||
template <typename T, typename... U> | ||
managed_ptr<T> manage_derived( | ||
std::tuple<managed_ptr<U>...> froms, managed_ptr<T> value_ptr) { | ||
const T& value = *value_ptr; | ||
return manage_derived_ref( | ||
std::tuple_cat(std::tuple(std::move(value_ptr)), std::move(froms)), | ||
value); | ||
} | ||
|
||
} // namespace whisker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.