conformance_archetype.h 43.8 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
// Copyright 2019 The Abseil 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
//
//      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.
//
// -----------------------------------------------------------------------------
// conformance_archetype.h
// -----------------------------------------------------------------------------
//
// This file contains a facility for generating "archetypes" of out of
// "Conformance Profiles" (see "conformance_profiles.h" for more information
// about Conformance Profiles). An archetype is a type that aims to support the
// bare minimum requirements of a given Conformance Profile. For instance, an
// archetype that corresponds to an ImmutableProfile has exactly a nothrow
// move-constructor, a potentially-throwing copy constructor, a nothrow
// destructor, with all other special-member-functions deleted. These archetypes
// are useful for testing to make sure that templates are able to work with the
// kinds of types that they claim to support (i.e. that they do not accidentally
// under-constrain),
//
// The main type template in this file is the Archetype template, which takes
// a Conformance Profile as a template argument and its instantiations are a
// minimum-conforming model of that profile.

#ifndef ABSL_TYPES_INTERNAL_CONFORMANCE_ARCHETYPE_H_
#define ABSL_TYPES_INTERNAL_CONFORMANCE_ARCHETYPE_H_

#include <cstddef>
#include <functional>
#include <type_traits>
#include <utility>

#include "absl/meta/type_traits.h"
#include "absl/types/internal/conformance_profile.h"

namespace absl {
ABSL_NAMESPACE_BEGIN
namespace types_internal {

// A minimum-conforming implementation of a type with properties specified in
// `Prof`, where `Prof` is a valid Conformance Profile.
template <class Prof, class /*Enabler*/ = void>
class Archetype;

// Given an Archetype, obtain the properties of the profile associated with that
// archetype.
template <class Archetype>
struct PropertiesOfArchetype;

template <class Prof>
struct PropertiesOfArchetype<Archetype<Prof>> {
  using type = PropertiesOfT<Prof>;
};

template <class Archetype>
using PropertiesOfArchetypeT = typename PropertiesOfArchetype<Archetype>::type;

// A metafunction to determine if a type is an `Archetype`.
template <class T>
struct IsArchetype : std::false_type {};

template <class Prof>
struct IsArchetype<Archetype<Prof>> : std::true_type {};

// A constructor tag type used when creating an Archetype with internal state.
struct MakeArchetypeState {};

// Data stored within an archetype that is copied/compared/hashed when the
// corresponding operations are used.
using ArchetypeState = std::size_t;

////////////////////////////////////////////////////////////////////////////////
//   This section of the file defines a chain of base classes for Archetype,  //
//   where each base defines a specific special member function with the      //
//   appropriate properties (deleted, noexcept(false), noexcept, or trivial). //
////////////////////////////////////////////////////////////////////////////////

// The bottom-most base, which contains the state and the default constructor.
template <default_constructible DefaultConstructibleValue>
struct ArchetypeStateBase {
  static_assert(DefaultConstructibleValue == default_constructible::yes ||
                    DefaultConstructibleValue == default_constructible::nothrow,
                "");

  ArchetypeStateBase() noexcept(
      DefaultConstructibleValue ==
      default_constructible::
          nothrow) /*Vacuous archetype_state initialization*/ {}
  explicit ArchetypeStateBase(MakeArchetypeState, ArchetypeState state) noexcept
      : archetype_state(state) {}

  ArchetypeState archetype_state;
};

template <>
struct ArchetypeStateBase<default_constructible::maybe> {
  explicit ArchetypeStateBase() = delete;
  explicit ArchetypeStateBase(MakeArchetypeState, ArchetypeState state) noexcept
      : archetype_state(state) {}

  ArchetypeState archetype_state;
};

template <>
struct ArchetypeStateBase<default_constructible::trivial> {
  ArchetypeStateBase() = default;
  explicit ArchetypeStateBase(MakeArchetypeState, ArchetypeState state) noexcept
      : archetype_state(state) {}

  ArchetypeState archetype_state;
};

// The move-constructor base
template <default_constructible DefaultConstructibleValue,
          move_constructible MoveConstructibleValue>
struct ArchetypeMoveConstructor
    : ArchetypeStateBase<DefaultConstructibleValue> {
  static_assert(MoveConstructibleValue == move_constructible::yes ||
                    MoveConstructibleValue == move_constructible::nothrow,
                "");

  explicit ArchetypeMoveConstructor(MakeArchetypeState,
                                    ArchetypeState state) noexcept
      : ArchetypeStateBase<DefaultConstructibleValue>(MakeArchetypeState(),
                                                      state) {}

  ArchetypeMoveConstructor() = default;
  ArchetypeMoveConstructor(ArchetypeMoveConstructor&& other) noexcept(
      MoveConstructibleValue == move_constructible::nothrow)
      : ArchetypeStateBase<DefaultConstructibleValue>(MakeArchetypeState(),
                                                      other.archetype_state) {}
  ArchetypeMoveConstructor(const ArchetypeMoveConstructor&) = default;
  ArchetypeMoveConstructor& operator=(ArchetypeMoveConstructor&&) = default;
  ArchetypeMoveConstructor& operator=(const ArchetypeMoveConstructor&) =
      default;
};

template <default_constructible DefaultConstructibleValue>
struct ArchetypeMoveConstructor<DefaultConstructibleValue,
                                move_constructible::trivial>
    : ArchetypeStateBase<DefaultConstructibleValue> {
  explicit ArchetypeMoveConstructor(MakeArchetypeState,
                                    ArchetypeState state) noexcept
      : ArchetypeStateBase<DefaultConstructibleValue>(MakeArchetypeState(),
                                                      state) {}

  ArchetypeMoveConstructor() = default;
};

// The copy-constructor base
template <default_constructible DefaultConstructibleValue,
          move_constructible MoveConstructibleValue,
          copy_constructible CopyConstructibleValue>
struct ArchetypeCopyConstructor
    : ArchetypeMoveConstructor<DefaultConstructibleValue,
                               MoveConstructibleValue> {
  static_assert(CopyConstructibleValue == copy_constructible::yes ||
                    CopyConstructibleValue == copy_constructible::nothrow,
                "");
  explicit ArchetypeCopyConstructor(MakeArchetypeState,
                                    ArchetypeState state) noexcept
      : ArchetypeMoveConstructor<DefaultConstructibleValue,
                                 MoveConstructibleValue>(MakeArchetypeState(),
                                                         state) {}

  ArchetypeCopyConstructor() = default;
  ArchetypeCopyConstructor(ArchetypeCopyConstructor&&) = default;
  ArchetypeCopyConstructor(const ArchetypeCopyConstructor& other) noexcept(
      CopyConstructibleValue == copy_constructible::nothrow)
      : ArchetypeMoveConstructor<DefaultConstructibleValue,
                                 MoveConstructibleValue>(
            MakeArchetypeState(), other.archetype_state) {}
  ArchetypeCopyConstructor& operator=(ArchetypeCopyConstructor&&) = default;
  ArchetypeCopyConstructor& operator=(const ArchetypeCopyConstructor&) =
      default;
};

template <default_constructible DefaultConstructibleValue,
          move_constructible MoveConstructibleValue>
struct ArchetypeCopyConstructor<DefaultConstructibleValue,
                                MoveConstructibleValue,
                                copy_constructible::maybe>
    : ArchetypeMoveConstructor<DefaultConstructibleValue,
                               MoveConstructibleValue> {
  explicit ArchetypeCopyConstructor(MakeArchetypeState,
                                    ArchetypeState state) noexcept
      : ArchetypeMoveConstructor<DefaultConstructibleValue,
                                 MoveConstructibleValue>(MakeArchetypeState(),
                                                         state) {}

  ArchetypeCopyConstructor() = default;
  ArchetypeCopyConstructor(ArchetypeCopyConstructor&&) = default;
  ArchetypeCopyConstructor(const ArchetypeCopyConstructor&) = delete;
  ArchetypeCopyConstructor& operator=(ArchetypeCopyConstructor&&) = default;
  ArchetypeCopyConstructor& operator=(const ArchetypeCopyConstructor&) =
      default;
};

template <default_constructible DefaultConstructibleValue,
          move_constructible MoveConstructibleValue>
struct ArchetypeCopyConstructor<DefaultConstructibleValue,
                                MoveConstructibleValue,
                                copy_constructible::trivial>
    : ArchetypeMoveConstructor<DefaultConstructibleValue,
                               MoveConstructibleValue> {
  explicit ArchetypeCopyConstructor(MakeArchetypeState,
                                    ArchetypeState state) noexcept
      : ArchetypeMoveConstructor<DefaultConstructibleValue,
                                 MoveConstructibleValue>(MakeArchetypeState(),
                                                         state) {}

  ArchetypeCopyConstructor() = default;
};

// The move-assign base
template <default_constructible DefaultConstructibleValue,
          move_constructible MoveConstructibleValue,
          copy_constructible CopyConstructibleValue,
          move_assignable MoveAssignableValue>
struct ArchetypeMoveAssign
    : ArchetypeCopyConstructor<DefaultConstructibleValue,
                               MoveConstructibleValue, CopyConstructibleValue> {
  static_assert(MoveAssignableValue == move_assignable::yes ||
                    MoveAssignableValue == move_assignable::nothrow,
                "");
  explicit ArchetypeMoveAssign(MakeArchetypeState,
                               ArchetypeState state) noexcept
      : ArchetypeCopyConstructor<DefaultConstructibleValue,
                                 MoveConstructibleValue,
                                 CopyConstructibleValue>(MakeArchetypeState(),
                                                         state) {}

  ArchetypeMoveAssign() = default;
  ArchetypeMoveAssign(ArchetypeMoveAssign&&) = default;
  ArchetypeMoveAssign(const ArchetypeMoveAssign&) = default;
  ArchetypeMoveAssign& operator=(ArchetypeMoveAssign&& other) noexcept(
      MoveAssignableValue == move_assignable::nothrow) {
    this->archetype_state = other.archetype_state;
    return *this;
  }

  ArchetypeMoveAssign& operator=(const ArchetypeMoveAssign&) = default;
};

template <default_constructible DefaultConstructibleValue,
          move_constructible MoveConstructibleValue,
          copy_constructible CopyConstructibleValue>
struct ArchetypeMoveAssign<DefaultConstructibleValue, MoveConstructibleValue,
                           CopyConstructibleValue, move_assignable::trivial>
    : ArchetypeCopyConstructor<DefaultConstructibleValue,
                               MoveConstructibleValue, CopyConstructibleValue> {
  explicit ArchetypeMoveAssign(MakeArchetypeState,
                               ArchetypeState state) noexcept
      : ArchetypeCopyConstructor<DefaultConstructibleValue,
                                 MoveConstructibleValue,
                                 CopyConstructibleValue>(MakeArchetypeState(),
                                                         state) {}

  ArchetypeMoveAssign() = default;
};

// The copy-assign base
template <default_constructible DefaultConstructibleValue,
          move_constructible MoveConstructibleValue,
          copy_constructible CopyConstructibleValue,
          move_assignable MoveAssignableValue,
          copy_assignable CopyAssignableValue>
struct ArchetypeCopyAssign
    : ArchetypeMoveAssign<DefaultConstructibleValue, MoveConstructibleValue,
                          CopyConstructibleValue, MoveAssignableValue> {
  static_assert(CopyAssignableValue == copy_assignable::yes ||
                    CopyAssignableValue == copy_assignable::nothrow,
                "");
  explicit ArchetypeCopyAssign(MakeArchetypeState,
                               ArchetypeState state) noexcept
      : ArchetypeMoveAssign<DefaultConstructibleValue, MoveConstructibleValue,
                            CopyConstructibleValue, MoveAssignableValue>(
            MakeArchetypeState(), state) {}

  ArchetypeCopyAssign() = default;
  ArchetypeCopyAssign(ArchetypeCopyAssign&&) = default;
  ArchetypeCopyAssign(const ArchetypeCopyAssign&) = default;
  ArchetypeCopyAssign& operator=(ArchetypeCopyAssign&&) = default;

  ArchetypeCopyAssign& operator=(const ArchetypeCopyAssign& other) noexcept(
      CopyAssignableValue == copy_assignable::nothrow) {
    this->archetype_state = other.archetype_state;
    return *this;
  }
};

template <default_constructible DefaultConstructibleValue,
          move_constructible MoveConstructibleValue,
          copy_constructible CopyConstructibleValue,
          move_assignable MoveAssignableValue>
struct ArchetypeCopyAssign<DefaultConstructibleValue, MoveConstructibleValue,
                           CopyConstructibleValue, MoveAssignableValue,
                           copy_assignable::maybe>
    : ArchetypeMoveAssign<DefaultConstructibleValue, MoveConstructibleValue,
                          CopyConstructibleValue, MoveAssignableValue> {
  explicit ArchetypeCopyAssign(MakeArchetypeState,
                               ArchetypeState state) noexcept
      : ArchetypeMoveAssign<DefaultConstructibleValue, MoveConstructibleValue,
                            CopyConstructibleValue, MoveAssignableValue>(
            MakeArchetypeState(), state) {}

  ArchetypeCopyAssign() = default;
  ArchetypeCopyAssign(ArchetypeCopyAssign&&) = default;
  ArchetypeCopyAssign(const ArchetypeCopyAssign&) = default;
  ArchetypeCopyAssign& operator=(ArchetypeCopyAssign&&) = default;
  ArchetypeCopyAssign& operator=(const ArchetypeCopyAssign&) = delete;
};

template <default_constructible DefaultConstructibleValue,
          move_constructible MoveConstructibleValue,
          copy_constructible CopyConstructibleValue,
          move_assignable MoveAssignableValue>
struct ArchetypeCopyAssign<DefaultConstructibleValue, MoveConstructibleValue,
                           CopyConstructibleValue, MoveAssignableValue,
                           copy_assignable::trivial>
    : ArchetypeMoveAssign<DefaultConstructibleValue, MoveConstructibleValue,
                          CopyConstructibleValue, MoveAssignableValue> {
  explicit ArchetypeCopyAssign(MakeArchetypeState,
                               ArchetypeState state) noexcept
      : ArchetypeMoveAssign<DefaultConstructibleValue, MoveConstructibleValue,
                            CopyConstructibleValue, MoveAssignableValue>(
            MakeArchetypeState(), state) {}

  ArchetypeCopyAssign() = default;
};

// The destructor base
template <default_constructible DefaultConstructibleValue,
          move_constructible MoveConstructibleValue,
          copy_constructible CopyConstructibleValue,
          move_assignable MoveAssignableValue,
          copy_assignable CopyAssignableValue, destructible DestructibleValue>
struct ArchetypeDestructor
    : ArchetypeCopyAssign<DefaultConstructibleValue, MoveConstructibleValue,
                          CopyConstructibleValue, MoveAssignableValue,
                          CopyAssignableValue> {
  static_assert(DestructibleValue == destructible::yes ||
                    DestructibleValue == destructible::nothrow,
                "");

  explicit ArchetypeDestructor(MakeArchetypeState,
                               ArchetypeState state) noexcept
      : ArchetypeCopyAssign<DefaultConstructibleValue, MoveConstructibleValue,
                            CopyConstructibleValue, MoveAssignableValue,
                            CopyAssignableValue>(MakeArchetypeState(), state) {}

  ArchetypeDestructor() = default;
  ArchetypeDestructor(ArchetypeDestructor&&) = default;
  ArchetypeDestructor(const ArchetypeDestructor&) = default;
  ArchetypeDestructor& operator=(ArchetypeDestructor&&) = default;
  ArchetypeDestructor& operator=(const ArchetypeDestructor&) = default;
  ~ArchetypeDestructor() noexcept(DestructibleValue == destructible::nothrow) {}
};

template <default_constructible DefaultConstructibleValue,
          move_constructible MoveConstructibleValue,
          copy_constructible CopyConstructibleValue,
          move_assignable MoveAssignableValue,
          copy_assignable CopyAssignableValue>
struct ArchetypeDestructor<DefaultConstructibleValue, MoveConstructibleValue,
                           CopyConstructibleValue, MoveAssignableValue,
                           CopyAssignableValue, destructible::trivial>
    : ArchetypeCopyAssign<DefaultConstructibleValue, MoveConstructibleValue,
                          CopyConstructibleValue, MoveAssignableValue,
                          CopyAssignableValue> {
  explicit ArchetypeDestructor(MakeArchetypeState,
                               ArchetypeState state) noexcept
      : ArchetypeCopyAssign<DefaultConstructibleValue, MoveConstructibleValue,
                            CopyConstructibleValue, MoveAssignableValue,
                            CopyAssignableValue>(MakeArchetypeState(), state) {}

  ArchetypeDestructor() = default;
};

// An alias to the top of the chain of bases for special-member functions.
// NOTE: move_constructible::maybe, move_assignable::maybe, and
// destructible::maybe are handled in the top-level type by way of SFINAE.
// Because of this, we never instantiate the base classes with
// move_constructible::maybe, move_assignable::maybe, or destructible::maybe so
// that we minimize the number of different possible type-template
// instantiations.
template <default_constructible DefaultConstructibleValue,
          move_constructible MoveConstructibleValue,
          copy_constructible CopyConstructibleValue,
          move_assignable MoveAssignableValue,
          copy_assignable CopyAssignableValue, destructible DestructibleValue>
using ArchetypeSpecialMembersBase = ArchetypeDestructor<
    DefaultConstructibleValue,
    MoveConstructibleValue != move_constructible::maybe
        ? MoveConstructibleValue
        : move_constructible::nothrow,
    CopyConstructibleValue,
    MoveAssignableValue != move_assignable::maybe ? MoveAssignableValue
                                                  : move_assignable::nothrow,
    CopyAssignableValue,
    DestructibleValue != destructible::maybe ? DestructibleValue
                                             : destructible::nothrow>;

// A function that is used to create an archetype with some associated state.
template <class Arch>
Arch MakeArchetype(ArchetypeState state) noexcept {
  static_assert(IsArchetype<Arch>::value,
                "The explicit template argument to MakeArchetype is required "
                "to be an Archetype.");
  return Arch(MakeArchetypeState(), state);
}

// This is used to conditionally delete "copy" and "move" constructors in a way
// that is consistent with what the ConformanceProfile requires and that also
// strictly enforces the arguments to the copy/move to not come from implicit
// conversions when dealing with the Archetype.
template <class Prof, class T>
constexpr bool ShouldDeleteConstructor() {
  return !((PropertiesOfT<Prof>::move_constructible_support !=
                move_constructible::maybe &&
            std::is_same<T, Archetype<Prof>>::value) ||
           (PropertiesOfT<Prof>::copy_constructible_support !=
                copy_constructible::maybe &&
            (std::is_same<T, const Archetype<Prof>&>::value ||
             std::is_same<T, Archetype<Prof>&>::value ||
             std::is_same<T, const Archetype<Prof>>::value)));
}

// This is used to conditionally delete "copy" and "move" assigns in a way
// that is consistent with what the ConformanceProfile requires and that also
// strictly enforces the arguments to the copy/move to not come from implicit
// conversions when dealing with the Archetype.
template <class Prof, class T>
constexpr bool ShouldDeleteAssign() {
  return !(
      (PropertiesOfT<Prof>::move_assignable_support != move_assignable::maybe &&
       std::is_same<T, Archetype<Prof>>::value) ||
      (PropertiesOfT<Prof>::copy_assignable_support != copy_assignable::maybe &&
       (std::is_same<T, const Archetype<Prof>&>::value ||
        std::is_same<T, Archetype<Prof>&>::value ||
        std::is_same<T, const Archetype<Prof>>::value)));
}

// TODO(calabrese) Inherit from a chain of secondary bases to pull in the
// associated functions of other concepts.
template <class Prof, class Enabler>
class Archetype : ArchetypeSpecialMembersBase<
                      PropertiesOfT<Prof>::default_constructible_support,
                      PropertiesOfT<Prof>::move_constructible_support,
                      PropertiesOfT<Prof>::copy_constructible_support,
                      PropertiesOfT<Prof>::move_assignable_support,
                      PropertiesOfT<Prof>::copy_assignable_support,
                      PropertiesOfT<Prof>::destructible_support> {
  static_assert(std::is_same<Enabler, void>::value,
                "An explicit type must not be passed as the second template "
                "argument to 'Archetype`.");

  // The cases mentioned in these static_asserts are expected to be handled in
  // the partial template specializations of Archetype that follow this
  // definition.
  static_assert(PropertiesOfT<Prof>::destructible_support !=
                    destructible::maybe,
                "");
  static_assert(PropertiesOfT<Prof>::move_constructible_support !=
                        move_constructible::maybe ||
                    PropertiesOfT<Prof>::copy_constructible_support ==
                        copy_constructible::maybe,
                "");
  static_assert(PropertiesOfT<Prof>::move_assignable_support !=
                        move_assignable::maybe ||
                    PropertiesOfT<Prof>::copy_assignable_support ==
                        copy_assignable::maybe,
                "");

 public:
  Archetype() = default;

  // Disallow moves when requested, and disallow implicit conversions.
  template <class T, typename std::enable_if<
                         ShouldDeleteConstructor<Prof, T>()>::type* = nullptr>
  Archetype(T&&) = delete;

  // Disallow moves when requested, and disallow implicit conversions.
  template <class T, typename std::enable_if<
                         ShouldDeleteAssign<Prof, T>()>::type* = nullptr>
  Archetype& operator=(T&&) = delete;

  using ArchetypeSpecialMembersBase<
      PropertiesOfT<Prof>::default_constructible_support,
      PropertiesOfT<Prof>::move_constructible_support,
      PropertiesOfT<Prof>::copy_constructible_support,
      PropertiesOfT<Prof>::move_assignable_support,
      PropertiesOfT<Prof>::copy_assignable_support,
      PropertiesOfT<Prof>::destructible_support>::archetype_state;

 private:
  explicit Archetype(MakeArchetypeState, ArchetypeState state) noexcept
      : ArchetypeSpecialMembersBase<
            PropertiesOfT<Prof>::default_constructible_support,
            PropertiesOfT<Prof>::move_constructible_support,
            PropertiesOfT<Prof>::copy_constructible_support,
            PropertiesOfT<Prof>::move_assignable_support,
            PropertiesOfT<Prof>::copy_assignable_support,
            PropertiesOfT<Prof>::destructible_support>(MakeArchetypeState(),
                                                       state) {}

  friend Archetype MakeArchetype<Archetype>(ArchetypeState) noexcept;
};

template <class Prof>
class Archetype<Prof, typename std::enable_if<
                          PropertiesOfT<Prof>::move_constructible_support !=
                              move_constructible::maybe &&
                          PropertiesOfT<Prof>::move_assignable_support ==
                              move_assignable::maybe &&
                          PropertiesOfT<Prof>::destructible_support !=
                              destructible::maybe>::type>
    : ArchetypeSpecialMembersBase<
          PropertiesOfT<Prof>::default_constructible_support,
          PropertiesOfT<Prof>::move_constructible_support,
          PropertiesOfT<Prof>::copy_constructible_support,
          PropertiesOfT<Prof>::move_assignable_support,
          PropertiesOfT<Prof>::copy_assignable_support,
          PropertiesOfT<Prof>::destructible_support> {
 public:
  Archetype() = default;
  Archetype(Archetype&&) = default;
  Archetype(const Archetype&) = default;
  Archetype& operator=(Archetype&&) = delete;
  Archetype& operator=(const Archetype&) = default;

  // Disallow moves when requested, and disallow implicit conversions.
  template <class T, typename std::enable_if<
                         ShouldDeleteConstructor<Prof, T>()>::type* = nullptr>
  Archetype(T&&) = delete;

  // Disallow moves when requested, and disallow implicit conversions.
  template <class T, typename std::enable_if<
                         ShouldDeleteAssign<Prof, T>()>::type* = nullptr>
  Archetype& operator=(T&&) = delete;

  using ArchetypeSpecialMembersBase<
      PropertiesOfT<Prof>::default_constructible_support,
      PropertiesOfT<Prof>::move_constructible_support,
      PropertiesOfT<Prof>::copy_constructible_support,
      PropertiesOfT<Prof>::move_assignable_support,
      PropertiesOfT<Prof>::copy_assignable_support,
      PropertiesOfT<Prof>::destructible_support>::archetype_state;

 private:
  explicit Archetype(MakeArchetypeState, ArchetypeState state) noexcept
      : ArchetypeSpecialMembersBase<
            PropertiesOfT<Prof>::default_constructible_support,
            PropertiesOfT<Prof>::move_constructible_support,
            PropertiesOfT<Prof>::copy_constructible_support,
            PropertiesOfT<Prof>::move_assignable_support,
            PropertiesOfT<Prof>::copy_assignable_support,
            PropertiesOfT<Prof>::destructible_support>(MakeArchetypeState(),
                                                       state) {}

  friend Archetype MakeArchetype<Archetype>(ArchetypeState) noexcept;
};

template <class Prof>
class Archetype<Prof, typename std::enable_if<
                          PropertiesOfT<Prof>::move_constructible_support ==
                              move_constructible::maybe &&
                          PropertiesOfT<Prof>::move_assignable_support ==
                              move_assignable::maybe &&
                          PropertiesOfT<Prof>::destructible_support !=
                              destructible::maybe>::type>
    : ArchetypeSpecialMembersBase<
          PropertiesOfT<Prof>::default_constructible_support,
          PropertiesOfT<Prof>::move_constructible_support,
          PropertiesOfT<Prof>::copy_constructible_support,
          PropertiesOfT<Prof>::move_assignable_support,
          PropertiesOfT<Prof>::copy_assignable_support,
          PropertiesOfT<Prof>::destructible_support> {
 public:
  Archetype() = default;
  Archetype(Archetype&&) = delete;
  Archetype(const Archetype&) = default;
  Archetype& operator=(Archetype&&) = delete;
  Archetype& operator=(const Archetype&) = default;

  // Disallow moves when requested, and disallow implicit conversions.
  template <class T, typename std::enable_if<
                         ShouldDeleteConstructor<Prof, T>()>::type* = nullptr>
  Archetype(T&&) = delete;

  // Disallow moves when requested, and disallow implicit conversions.
  template <class T, typename std::enable_if<
                         ShouldDeleteAssign<Prof, T>()>::type* = nullptr>
  Archetype& operator=(T&&) = delete;

  using ArchetypeSpecialMembersBase<
      PropertiesOfT<Prof>::default_constructible_support,
      PropertiesOfT<Prof>::move_constructible_support,
      PropertiesOfT<Prof>::copy_constructible_support,
      PropertiesOfT<Prof>::move_assignable_support,
      PropertiesOfT<Prof>::copy_assignable_support,
      PropertiesOfT<Prof>::destructible_support>::archetype_state;

 private:
  explicit Archetype(MakeArchetypeState, ArchetypeState state) noexcept
      : ArchetypeSpecialMembersBase<
            PropertiesOfT<Prof>::default_constructible_support,
            PropertiesOfT<Prof>::move_constructible_support,
            PropertiesOfT<Prof>::copy_constructible_support,
            PropertiesOfT<Prof>::move_assignable_support,
            PropertiesOfT<Prof>::copy_assignable_support,
            PropertiesOfT<Prof>::destructible_support>(MakeArchetypeState(),
                                                       state) {}

  friend Archetype MakeArchetype<Archetype>(ArchetypeState) noexcept;
};

template <class Prof>
class Archetype<Prof, typename std::enable_if<
                          PropertiesOfT<Prof>::move_constructible_support ==
                              move_constructible::maybe &&
                          PropertiesOfT<Prof>::move_assignable_support !=
                              move_assignable::maybe &&
                          PropertiesOfT<Prof>::destructible_support !=
                              destructible::maybe>::type>
    : ArchetypeSpecialMembersBase<
          PropertiesOfT<Prof>::default_constructible_support,
          PropertiesOfT<Prof>::move_constructible_support,
          PropertiesOfT<Prof>::copy_constructible_support,
          PropertiesOfT<Prof>::move_assignable_support,
          PropertiesOfT<Prof>::copy_assignable_support,
          PropertiesOfT<Prof>::destructible_support> {
 public:
  Archetype() = default;
  Archetype(Archetype&&) = delete;
  Archetype(const Archetype&) = default;
  Archetype& operator=(Archetype&&) = default;
  Archetype& operator=(const Archetype&) = default;

  // Disallow moves when requested, and disallow implicit conversions.
  template <class T, typename std::enable_if<
                         ShouldDeleteConstructor<Prof, T>()>::type* = nullptr>
  Archetype(T&&) = delete;

  // Disallow moves when requested, and disallow implicit conversions.
  template <class T, typename std::enable_if<
                         ShouldDeleteAssign<Prof, T>()>::type* = nullptr>
  Archetype& operator=(T&&) = delete;

  using ArchetypeSpecialMembersBase<
      PropertiesOfT<Prof>::default_constructible_support,
      PropertiesOfT<Prof>::move_constructible_support,
      PropertiesOfT<Prof>::copy_constructible_support,
      PropertiesOfT<Prof>::move_assignable_support,
      PropertiesOfT<Prof>::copy_assignable_support,
      PropertiesOfT<Prof>::destructible_support>::archetype_state;

 private:
  explicit Archetype(MakeArchetypeState, ArchetypeState state) noexcept
      : ArchetypeSpecialMembersBase<
            PropertiesOfT<Prof>::default_constructible_support,
            PropertiesOfT<Prof>::move_constructible_support,
            PropertiesOfT<Prof>::copy_constructible_support,
            PropertiesOfT<Prof>::move_assignable_support,
            PropertiesOfT<Prof>::copy_assignable_support,
            PropertiesOfT<Prof>::destructible_support>(MakeArchetypeState(),
                                                       state) {}

  friend Archetype MakeArchetype<Archetype>(ArchetypeState) noexcept;
};

template <class Prof>
class Archetype<Prof, typename std::enable_if<
                          PropertiesOfT<Prof>::move_constructible_support !=
                              move_constructible::maybe &&
                          PropertiesOfT<Prof>::move_assignable_support ==
                              move_assignable::maybe &&
                          PropertiesOfT<Prof>::destructible_support ==
                              destructible::maybe>::type>
    : ArchetypeSpecialMembersBase<
          PropertiesOfT<Prof>::default_constructible_support,
          PropertiesOfT<Prof>::move_constructible_support,
          PropertiesOfT<Prof>::copy_constructible_support,
          PropertiesOfT<Prof>::move_assignable_support,
          PropertiesOfT<Prof>::copy_assignable_support,
          PropertiesOfT<Prof>::destructible_support> {
 public:
  Archetype() = default;
  Archetype(Archetype&&) = default;
  Archetype(const Archetype&) = default;
  Archetype& operator=(Archetype&&) = delete;
  Archetype& operator=(const Archetype&) = default;
  ~Archetype() = delete;

  // Disallow moves when requested, and disallow implicit conversions.
  template <class T, typename std::enable_if<
                         ShouldDeleteConstructor<Prof, T>()>::type* = nullptr>
  Archetype(T&&) = delete;

  // Disallow moves when requested, and disallow implicit conversions.
  template <class T, typename std::enable_if<
                         ShouldDeleteAssign<Prof, T>()>::type* = nullptr>
  Archetype& operator=(T&&) = delete;

  using ArchetypeSpecialMembersBase<
      PropertiesOfT<Prof>::default_constructible_support,
      PropertiesOfT<Prof>::move_constructible_support,
      PropertiesOfT<Prof>::copy_constructible_support,
      PropertiesOfT<Prof>::move_assignable_support,
      PropertiesOfT<Prof>::copy_assignable_support,
      PropertiesOfT<Prof>::destructible_support>::archetype_state;

 private:
  explicit Archetype(MakeArchetypeState, ArchetypeState state) noexcept
      : ArchetypeSpecialMembersBase<
            PropertiesOfT<Prof>::default_constructible_support,
            PropertiesOfT<Prof>::move_constructible_support,
            PropertiesOfT<Prof>::copy_constructible_support,
            PropertiesOfT<Prof>::move_assignable_support,
            PropertiesOfT<Prof>::copy_assignable_support,
            PropertiesOfT<Prof>::destructible_support>(MakeArchetypeState(),
                                                       state) {}

  friend Archetype MakeArchetype<Archetype>(ArchetypeState) noexcept;
};

template <class Prof>
class Archetype<Prof, typename std::enable_if<
                          PropertiesOfT<Prof>::move_constructible_support ==
                              move_constructible::maybe &&
                          PropertiesOfT<Prof>::move_assignable_support ==
                              move_assignable::maybe &&
                          PropertiesOfT<Prof>::destructible_support ==
                              destructible::maybe>::type>
    : ArchetypeSpecialMembersBase<
          PropertiesOfT<Prof>::default_constructible_support,
          PropertiesOfT<Prof>::move_constructible_support,
          PropertiesOfT<Prof>::copy_constructible_support,
          PropertiesOfT<Prof>::move_assignable_support,
          PropertiesOfT<Prof>::copy_assignable_support,
          PropertiesOfT<Prof>::destructible_support> {
 public:
  Archetype() = default;
  Archetype(Archetype&&) = delete;
  Archetype(const Archetype&) = default;
  Archetype& operator=(Archetype&&) = delete;
  Archetype& operator=(const Archetype&) = default;
  ~Archetype() = delete;

  // Disallow moves when requested, and disallow implicit conversions.
  template <class T, typename std::enable_if<
                         ShouldDeleteConstructor<Prof, T>()>::type* = nullptr>
  Archetype(T&&) = delete;

  // Disallow moves when requested, and disallow implicit conversions.
  template <class T, typename std::enable_if<
                         ShouldDeleteAssign<Prof, T>()>::type* = nullptr>
  Archetype& operator=(T&&) = delete;

  using ArchetypeSpecialMembersBase<
      PropertiesOfT<Prof>::default_constructible_support,
      PropertiesOfT<Prof>::move_constructible_support,
      PropertiesOfT<Prof>::copy_constructible_support,
      PropertiesOfT<Prof>::move_assignable_support,
      PropertiesOfT<Prof>::copy_assignable_support,
      PropertiesOfT<Prof>::destructible_support>::archetype_state;

 private:
  explicit Archetype(MakeArchetypeState, ArchetypeState state) noexcept
      : ArchetypeSpecialMembersBase<
            PropertiesOfT<Prof>::default_constructible_support,
            PropertiesOfT<Prof>::move_constructible_support,
            PropertiesOfT<Prof>::copy_constructible_support,
            PropertiesOfT<Prof>::move_assignable_support,
            PropertiesOfT<Prof>::copy_assignable_support,
            PropertiesOfT<Prof>::destructible_support>(MakeArchetypeState(),
                                                       state) {}

  friend Archetype MakeArchetype<Archetype>(ArchetypeState) noexcept;
};

template <class Prof>
class Archetype<Prof, typename std::enable_if<
                          PropertiesOfT<Prof>::move_constructible_support ==
                              move_constructible::maybe &&
                          PropertiesOfT<Prof>::move_assignable_support !=
                              move_assignable::maybe &&
                          PropertiesOfT<Prof>::destructible_support ==
                              destructible::maybe>::type>
    : ArchetypeSpecialMembersBase<
          PropertiesOfT<Prof>::default_constructible_support,
          PropertiesOfT<Prof>::move_constructible_support,
          PropertiesOfT<Prof>::copy_constructible_support,
          PropertiesOfT<Prof>::move_assignable_support,
          PropertiesOfT<Prof>::copy_assignable_support,
          PropertiesOfT<Prof>::destructible_support> {
 public:
  Archetype() = default;
  Archetype(Archetype&&) = delete;
  Archetype(const Archetype&) = default;
  Archetype& operator=(Archetype&&) = default;
  Archetype& operator=(const Archetype&) = default;
  ~Archetype() = delete;

  // Disallow moves when requested, and disallow implicit conversions.
  template <class T, typename std::enable_if<
                         ShouldDeleteConstructor<Prof, T>()>::type* = nullptr>
  Archetype(T&&) = delete;

  // Disallow moves when requested, and disallow implicit conversions.
  template <class T, typename std::enable_if<
                         ShouldDeleteAssign<Prof, T>()>::type* = nullptr>
  Archetype& operator=(T&&) = delete;

  using ArchetypeSpecialMembersBase<
      PropertiesOfT<Prof>::default_constructible_support,
      PropertiesOfT<Prof>::move_constructible_support,
      PropertiesOfT<Prof>::copy_constructible_support,
      PropertiesOfT<Prof>::move_assignable_support,
      PropertiesOfT<Prof>::copy_assignable_support,
      PropertiesOfT<Prof>::destructible_support>::archetype_state;

 private:
  explicit Archetype(MakeArchetypeState, ArchetypeState state) noexcept
      : ArchetypeSpecialMembersBase<
            PropertiesOfT<Prof>::default_constructible_support,
            PropertiesOfT<Prof>::move_constructible_support,
            PropertiesOfT<Prof>::copy_constructible_support,
            PropertiesOfT<Prof>::move_assignable_support,
            PropertiesOfT<Prof>::copy_assignable_support,
            PropertiesOfT<Prof>::destructible_support>(MakeArchetypeState(),
                                                       state) {}

  friend Archetype MakeArchetype<Archetype>(ArchetypeState) noexcept;
};

// Explicitly deleted swap for Archetype if the profile does not require swap.
// It is important to delete it rather than simply leave it out so that the
// "using std::swap;" idiom will result in this deleted overload being picked.
template <class Prof,
          absl::enable_if_t<!PropertiesOfT<Prof>::is_swappable, int> = 0>
void swap(Archetype<Prof>&, Archetype<Prof>&) = delete;  // NOLINT

// A conditionally-noexcept swap implementation for Archetype when the profile
// supports swap.
template <class Prof,
          absl::enable_if_t<PropertiesOfT<Prof>::is_swappable, int> = 0>
void swap(Archetype<Prof>& lhs, Archetype<Prof>& rhs)  // NOLINT
    noexcept(PropertiesOfT<Prof>::swappable_support != swappable::yes) {
  std::swap(lhs.archetype_state, rhs.archetype_state);
}

// A convertible-to-bool type that is used as the return type of comparison
// operators since the standard doesn't always require exactly bool.
struct NothrowBool {
  explicit NothrowBool() = delete;
  ~NothrowBool() = default;

  // TODO(calabrese) Delete the copy constructor in C++17 mode since guaranteed
  // elision makes it not required when returning from a function.
  // NothrowBool(NothrowBool const&) = delete;

  NothrowBool& operator=(NothrowBool const&) = delete;

  explicit operator bool() const noexcept { return value; }

  static NothrowBool make(bool const value) noexcept {
    return NothrowBool(value);
  }

 private:
  explicit NothrowBool(bool const value) noexcept : value(value) {}

  bool value;
};

// A convertible-to-bool type that is used as the return type of comparison
// operators since the standard doesn't always require exactly bool.
// Note: ExceptionalBool has a conversion operator that is not noexcept, so
// that even when a comparison operator is noexcept, that operation may still
// potentially throw when converted to bool.
struct ExceptionalBool {
  explicit ExceptionalBool() = delete;
  ~ExceptionalBool() = default;

  // TODO(calabrese) Delete the copy constructor in C++17 mode since guaranteed
  // elision makes it not required when returning from a function.
  // ExceptionalBool(ExceptionalBool const&) = delete;

  ExceptionalBool& operator=(ExceptionalBool const&) = delete;

  explicit operator bool() const { return value; }  // NOLINT

  static ExceptionalBool make(bool const value) noexcept {
    return ExceptionalBool(value);
  }

 private:
  explicit ExceptionalBool(bool const value) noexcept : value(value) {}

  bool value;
};

// The following macro is only used as a helper in this file to stamp out
// comparison operator definitions. It is undefined after usage.
//
// NOTE: Non-nothrow operators throw via their result's conversion to bool even
// though the operation itself is noexcept.
#define ABSL_TYPES_INTERNAL_OP(enum_name, op)                                \
  template <class Prof>                                                      \
  absl::enable_if_t<!PropertiesOfT<Prof>::is_##enum_name, bool> operator op( \
      const Archetype<Prof>&, const Archetype<Prof>&) = delete;              \
                                                                             \
  template <class Prof>                                                      \
  typename absl::enable_if_t<                                                \
      PropertiesOfT<Prof>::is_##enum_name,                                   \
      std::conditional<PropertiesOfT<Prof>::enum_name##_support ==           \
                           enum_name::nothrow,                               \
                       NothrowBool, ExceptionalBool>>::type                  \
  operator op(const Archetype<Prof>& lhs,                                    \
              const Archetype<Prof>& rhs) noexcept {                         \
    return absl::conditional_t<                                              \
        PropertiesOfT<Prof>::enum_name##_support == enum_name::nothrow,      \
        NothrowBool, ExceptionalBool>::make(lhs.archetype_state op           \
                                                rhs.archetype_state);        \
  }

ABSL_TYPES_INTERNAL_OP(equality_comparable, ==);
ABSL_TYPES_INTERNAL_OP(inequality_comparable, !=);
ABSL_TYPES_INTERNAL_OP(less_than_comparable, <);
ABSL_TYPES_INTERNAL_OP(less_equal_comparable, <=);
ABSL_TYPES_INTERNAL_OP(greater_equal_comparable, >=);
ABSL_TYPES_INTERNAL_OP(greater_than_comparable, >);

#undef ABSL_TYPES_INTERNAL_OP

// Base class for std::hash specializations when an Archetype doesn't support
// hashing.
struct PoisonedHash {
  PoisonedHash() = delete;
  PoisonedHash(const PoisonedHash&) = delete;
  PoisonedHash& operator=(const PoisonedHash&) = delete;
};

// Base class for std::hash specializations when an Archetype supports hashing.
template <class Prof>
struct EnabledHash {
  using argument_type = Archetype<Prof>;
  using result_type = std::size_t;
  result_type operator()(const argument_type& arg) const {
    return std::hash<ArchetypeState>()(arg.archetype_state);
  }
};

}  // namespace types_internal
ABSL_NAMESPACE_END
}  // namespace absl

namespace std {

template <class Prof>  // NOLINT
struct hash<::absl::types_internal::Archetype<Prof>>
    : conditional<::absl::types_internal::PropertiesOfT<Prof>::is_hashable,
                  ::absl::types_internal::EnabledHash<Prof>,
                  ::absl::types_internal::PoisonedHash>::type {};

}  // namespace std

#endif  // ABSL_TYPES_INTERNAL_CONFORMANCE_ARCHETYPE_H_