File
Blob: src/workerd/util/own-util.h
| 1 | // Copyright (c) 2017-2022 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | #pragma once |
| 6 | |
| 7 | #include <kj/array.h> |
| 8 | #include <kj/refcount.h> |
| 9 | #include <kj/string.h> |
| 10 | |
| 11 | namespace workerd { |
| 12 | |
| 13 | template <typename T> |
| 14 | inline auto mapAddRef(kj::Maybe<kj::Own<T>>& maybe) -> kj::Maybe<kj::Own<T>> { |
| 15 | return maybe.map([](kj::Own<T>& t) { return kj::addRef(*t); }); |
| 16 | } |
| 17 | |
| 18 | template <typename T> |
| 19 | inline auto mapAddRef(kj::Maybe<kj::Rc<T>>& maybe) -> kj::Maybe<kj::Rc<T>> { |
| 20 | return maybe.map([](kj::Rc<T>& t) { return t.addRef(); }); |
| 21 | } |
| 22 | |
| 23 | template <typename T> |
| 24 | inline auto mapAddRef(const kj::Maybe<kj::Arc<T>>& maybe) -> kj::Maybe<kj::Arc<T>> { |
| 25 | return maybe.map([](const kj::Arc<T>& t) { return t.addRef(); }); |
| 26 | } |
| 27 | |
| 28 | template <typename T> |
| 29 | inline auto mapAddRef(kj::Maybe<T&> maybe) -> kj::Maybe<kj::Own<T>> { |
| 30 | return maybe.map([](T& t) { return kj::addRef(t); }); |
| 31 | } |
| 32 | |
| 33 | template <typename T> |
| 34 | inline auto mapAddRef(kj::ArrayPtr<kj::Own<T>>& array) -> kj::Array<kj::Own<T>> { |
| 35 | return KJ_MAP(t, array) { return kj::addRef(*t); }; |
| 36 | } |
| 37 | |
| 38 | template <typename T> |
| 39 | inline auto mapAddRef(kj::Array<kj::Own<T>>& array) -> kj::Array<kj::Own<T>> { |
| 40 | return KJ_MAP(t, array) { return kj::addRef(*t); }; |
| 41 | } |
| 42 | |
| 43 | inline auto mapCopyString(const kj::Maybe<kj::String>& string) -> kj::Maybe<kj::String> { |
| 44 | return string.map([](const kj::String& s) { return kj::str(s); }); |
| 45 | } |
| 46 | |
| 47 | inline auto mapCopyString(const kj::Maybe<kj::StringPtr>& string) -> kj::Maybe<kj::String> { |
| 48 | return string.map([](const kj::StringPtr& s) { return kj::str(s); }); |
| 49 | } |
| 50 | |
| 51 | } // namespace workerd |