<commit_before>// 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
//
//     https://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 "concepts/callable.h"
#include "mem/__private/relocate.h"
#include "mem/forward.h"

namespace sus::fn {

namespace __private {

/// The type-erased type (dropping the type of the internal lambda) of the
/// closure's heap-allocated storage.
struct FnStorageBase;

/// Helper type returned by sus_bind() and used to construct a closure.
template <class F>
struct SusBind;

/// Helper to determine which functions need to be instatiated for the closure,
/// to be called from FnOnce, FnMut, and/or Fn.
///
/// This type indicates the closure can only be called from FnOnce.
enum StorageConstructionFnOnceType { StorageConstructionFnOnce };
/// Helper to determine which functions need to be instatiated for the closure,
/// to be called from FnOnce, FnMut, and/or Fn.
///
/// This type indicates the closure can be called from FnMut or FnOnce.
enum StorageConstructionFnMutType { StorageConstructionFnMut };
/// Helper to determine which functions need to be instatiated for the closure,
/// to be called from FnOnce, FnMut, and/or Fn.
///
/// This type indicates the closure can be called from Fn, FnMut or FnOnce.
enum StorageConstructionFnType { StorageConstructionFn };

/// Used to indicate if the closure is holding a function pointer or
/// heap-allocated storage.
enum FnType {
  /// Holds a function pointer or captureless lambda.
  FnPointer,
  /// Holds the type-erased output of sus_bind() in a heap allocation.
  Storage,
};

}  // namespace __private

template <class R, class... Args>
class FnOnce;
template <class R, class... Args>
class FnMut;
template <class R, class... Args>
class Fn;

/// A closure that erases the type of the internal callable object (lambda). A
/// FnOnce may only be called a single time.
///
/// Fn can be used as a FnMut, which can be used as a FnOnce.
///
/// Lambdas without captures can be converted into a FnOnce, FnMut, or Fn
/// directly. If the lambda has captured, it must be given to one of:<commit_msg>Add a TODO about generic lambdas<commit_after>// 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
//
//     https://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 "concepts/callable.h"
#include "mem/__private/relocate.h"
#include "mem/forward.h"

namespace sus::fn {

namespace __private {

/// The type-erased type (dropping the type of the internal lambda) of the
/// closure's heap-allocated storage.
struct FnStorageBase;

/// Helper type returned by sus_bind() and used to construct a closure.
template <class F>
struct SusBind;

/// Helper to determine which functions need to be instatiated for the closure,
/// to be called from FnOnce, FnMut, and/or Fn.
///
/// This type indicates the closure can only be called from FnOnce.
enum StorageConstructionFnOnceType { StorageConstructionFnOnce };
/// Helper to determine which functions need to be instatiated for the closure,
/// to be called from FnOnce, FnMut, and/or Fn.
///
/// This type indicates the closure can be called from FnMut or FnOnce.
enum StorageConstructionFnMutType { StorageConstructionFnMut };
/// Helper to determine which functions need to be instatiated for the closure,
/// to be called from FnOnce, FnMut, and/or Fn.
///
/// This type indicates the closure can be called from Fn, FnMut or FnOnce.
enum StorageConstructionFnType { StorageConstructionFn };

/// Used to indicate if the closure is holding a function pointer or
/// heap-allocated storage.
enum FnType {
  /// Holds a function pointer or captureless lambda.
  FnPointer,
  /// Holds the type-erased output of sus_bind() in a heap allocation.
  Storage,
};

}  // namespace __private

template <class R, class... Args>
class FnOnce;
template <class R, class... Args>
class FnMut;
template <class R, class... Args>
class Fn;

// TODO: Consider generic lambdas, it should be possible to bind them into
// FnOnce/FnMut/Fn?

/// A closure that erases the type of the internal callable object (lambda). A
/// FnOnce may only be called a single time.
///
/// Fn can be used as a FnMut, which can be used as a FnOnce.
///
/// Lambdas without captures can be converted into a FnOnce, FnMut, or Fn
/// directly. If the lambda has captured, it must be given to one of:
{'fraction_non_alphanumeric': 0.09270142180094787, 'fraction_numerical': 0.003033175355450237, 'mean_word_length': 3.441077441077441, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6132008', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1467, 'n_words': 719}