-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add DeprecatedOptionalField structure
Summary: Currently if we have "optionals" in thrift_cpp2_options, we will use folly::Optional for optional fields. The motivation of this task is that we want to get rid of "optionals" in thrift_cpp2_options and migrate existing code to use optional_field_ref instead. As Step #1, we will create new thrift::DeprecatedOptionalField that's compatible with folly::Optional, and can be implicitly casted to each other. After the first step it done, we can codemod the API of thrift::DeprecatedOptionalField to make it identical to optional_field_ref. This diff created DeprecatedOptionalField struct. I will modify thrift compiler in following diff to generate optional field with this code. Reviewed By: vitaut Differential Revision: D19785312 fbshipit-source-id: b5fcaadc9aec8d0a6a223e69bbcb7b565a27210f
- Loading branch information
1 parent
587989f
commit 717dbbe
Showing
2 changed files
with
117 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its 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 <folly/Optional.h> | ||
#include <folly/Portability.h> | ||
#include <type_traits> | ||
|
||
namespace apache { | ||
namespace thrift { | ||
|
||
/** | ||
* DeprecatedOptionalField is used for thrift optional field with "optionals" | ||
* turned on. It will be eventually replaced by thrift::optional_field_ref. | ||
*/ | ||
template <typename T> | ||
class DeprecatedOptionalField : public folly::Optional<T> { | ||
private: | ||
const folly::Optional<T>& toFolly() const { | ||
return *this; | ||
} | ||
|
||
public: | ||
using folly::Optional<T>::Optional; | ||
using folly::Optional<T>::operator=; | ||
|
||
template <typename L, typename R> | ||
friend bool operator==( | ||
const DeprecatedOptionalField<L>& lhs, | ||
const DeprecatedOptionalField<R>& rhs); | ||
template <typename L, typename R> | ||
friend bool operator!=( | ||
const DeprecatedOptionalField<L>& lhs, | ||
const DeprecatedOptionalField<R>& rhs); | ||
template <typename U> | ||
friend bool operator==( | ||
const DeprecatedOptionalField<U>& lhs, | ||
const folly::Optional<U>& rhs); | ||
template <typename U> | ||
friend bool operator==( | ||
const folly::Optional<U>& lhs, | ||
const DeprecatedOptionalField<U>& rhs); | ||
template <typename U> | ||
friend bool operator!=( | ||
const DeprecatedOptionalField<U>& lhs, | ||
const folly::Optional<U>& rhs); | ||
template <typename U> | ||
friend bool operator!=( | ||
const folly::Optional<U>& lhs, | ||
const DeprecatedOptionalField<U>& rhs); | ||
}; | ||
|
||
template <typename L, typename R> | ||
bool operator==( | ||
const DeprecatedOptionalField<L>& lhs, | ||
const DeprecatedOptionalField<R>& rhs) { | ||
return lhs.toFolly() == rhs.toFolly(); | ||
} | ||
template <typename L, typename R> | ||
bool operator!=( | ||
const DeprecatedOptionalField<L>& lhs, | ||
const DeprecatedOptionalField<R>& rhs) { | ||
return lhs.toFolly() != rhs.toFolly(); | ||
} | ||
template <typename U> | ||
bool operator==( | ||
const DeprecatedOptionalField<U>& lhs, | ||
const folly::Optional<U>& rhs) { | ||
return lhs.toFolly() == rhs; | ||
} | ||
template <typename U> | ||
bool operator==( | ||
const folly::Optional<U>& lhs, | ||
const DeprecatedOptionalField<U>& rhs) { | ||
return lhs == rhs.toFolly(); | ||
} | ||
template <typename U> | ||
bool operator!=( | ||
const DeprecatedOptionalField<U>& lhs, | ||
const folly::Optional<U>& rhs) { | ||
return lhs.toFolly() != rhs; | ||
} | ||
template <typename U> | ||
bool operator!=( | ||
const folly::Optional<U>& lhs, | ||
const DeprecatedOptionalField<U>& rhs) { | ||
return lhs != rhs.toFolly(); | ||
} | ||
|
||
template <class T> | ||
folly::Optional<T> castToFolly(const DeprecatedOptionalField<T>& t) { | ||
return t; | ||
} | ||
|
||
} // namespace thrift | ||
} // namespace apache | ||
|
||
FOLLY_NAMESPACE_STD_BEGIN | ||
template <class T> | ||
struct hash<apache::thrift::DeprecatedOptionalField<T>> | ||
: hash<folly::Optional<T>> {}; | ||
FOLLY_NAMESPACE_STD_END |
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