chromium/third_party/grpc/src/src/core/lib/slice/slice.h

// Copyright 2021 gRPC authors.
//
// 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 GRPC_SRC_CORE_LIB_SLICE_SLICE_H
#define GRPC_SRC_CORE_LIB_SLICE_SLICE_H

#include <grpc/support/port_platform.h>

#include <string.h>

#include <cstdint>
#include <string>
#include <utility>

#include "absl/strings/string_view.h"

#include <grpc/event_engine/internal/slice_cast.h>
#include <grpc/event_engine/slice.h>
#include <grpc/slice.h>
#include <grpc/support/log.h>

#include "src/core/lib/gpr/string.h"
#include "src/core/lib/gprpp/debug_location.h"
#include "src/core/lib/slice/slice_internal.h"
#include "src/core/lib/slice/slice_refcount.h"

// Herein lies grpc_core::Slice and its team of thin wrappers around grpc_slice.
// They aim to keep you safe by providing strong guarantees around lifetime and
// mutability.
//
// The team:
//   Slice        - provides a wrapper around an unknown type of slice.
//                  Immutable (since we don't know who else might be referencing
//                  it), and potentially ref counted.
//   StaticSlice  - provides a wrapper around a static slice. Not refcounted,
//                  fast to copy.
//   MutableSlice - provides a guarantee of unique ownership, meaning the
//                  underlying data can be mutated safely.

// This slice implementation is an extension of the EventEngine Slice
// implementation defined in <grpc/event_engine/slice.h>. Changes to this
// implementation might warrant changes to the public EventEngine Slice
// type as well.

namespace grpc_core {

inline const grpc_slice& CSliceRef(const grpc_slice& slice,
                                   DebugLocation loc = {}

inline void CSliceUnref(const grpc_slice& slice, DebugLocation loc = {}

namespace slice_detail {

// Returns an empty slice.
static constexpr grpc_slice EmptySlice() {}

// BaseSlice holds the grpc_slice object, but does not apply refcounting policy.
// It does export immutable access into the slice, such that this can be shared
// by all storage policies.
class BaseSlice {};

inline bool operator==(const BaseSlice& a, const BaseSlice& b) {}

inline bool operator!=(const BaseSlice& a, const BaseSlice& b) {}

inline bool operator==(const BaseSlice& a, absl::string_view b) {}

inline bool operator!=(const BaseSlice& a, absl::string_view b) {}

inline bool operator==(absl::string_view a, const BaseSlice& b) {}

inline bool operator!=(absl::string_view a, const BaseSlice& b) {}

inline bool operator==(const BaseSlice& a, const grpc_slice& b) {}

inline bool operator!=(const BaseSlice& a, const grpc_slice& b) {}

inline bool operator==(const grpc_slice& a, const BaseSlice& b) {}

inline bool operator!=(const grpc_slice& a, const BaseSlice& b) {}

template <typename Out>
struct CopyConstructors {};

template <typename Out>
struct StaticConstructors {};

}  // namespace slice_detail

class StaticSlice : public slice_detail::BaseSlice,
                    public slice_detail::StaticConstructors<StaticSlice> {};

class GPR_MSVC_EMPTY_BASE_CLASS_WORKAROUND MutableSlice
    : public slice_detail::BaseSlice,
      public slice_detail::CopyConstructors<MutableSlice> {};

class GPR_MSVC_EMPTY_BASE_CLASS_WORKAROUND Slice
    : public slice_detail::BaseSlice,
      public slice_detail::CopyConstructors<Slice>,
      public slice_detail::StaticConstructors<Slice> {};

}  // namespace grpc_core

namespace grpc_event_engine {
namespace experimental {
namespace internal {
template <>
struct SliceCastable<grpc_core::Slice, grpc_slice> {};
template <>
struct SliceCastable<grpc_slice, grpc_core::Slice> {};
template <>
struct SliceCastable<grpc_core::Slice, Slice> {};
template <>
struct SliceCastable<Slice, grpc_core::Slice> {};
}  // namespace internal
}  // namespace experimental
}  // namespace grpc_event_engine

#endif  // GRPC_SRC_CORE_LIB_SLICE_SLICE_H