chromium/third_party/fuzztest/src/fuzztest/internal/any.h

// Copyright 2022 Google LLC
//
// 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.

#ifndef FUZZTEST_FUZZTEST_INTERNAL_ANY_H_
#define FUZZTEST_FUZZTEST_INTERNAL_ANY_H_

#include <cstddef>
#include <tuple>
#include <type_traits>
#include <utility>

#include "./fuzztest/internal/logging.h"
#include "./fuzztest/internal/meta.h"

namespace fuzztest::internal {

// Base class for both implementations of Any below, and should not be used
// directly. The caller to the constructor decides if they want a copy operation
// or not.
class AnyBase {};

// These classes are similar to `std::any` but we implement our own because:
//  - We need a move only implementation of it for certain cases (MoveOnlyAny),
//    and `std::any` requires copyability.
//  - The implicit conversions of `std::any` are dangerous and easy to use
//    wrong. The conversions here are explicit and require an
//    `std::in_place_type<T>` tag.
//  - `std::any` causes compile time issues when mixed with other generic
//    types like std::pair/std::tuple/etc, because of their aggressive SFINAE
//    checks.
//
//  To access the object you use Has<T>()/GetAs<T>() instead of any_cast.
class MoveOnlyAny : private AnyBase {};

class CopyableAny : private AnyBase {};

}  // namespace fuzztest::internal

#endif  // FUZZTEST_FUZZTEST_INTERNAL_ANY_H_