OR-Tools  9.6
key_types.h
Go to the documentation of this file.
1 // Copyright 2010-2022 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 // IWYU pragma: private, include "ortools/math_opt/cpp/math_opt.h"
15 // IWYU pragma: friend "ortools/math_opt/cpp/.*"
16 
17 // This header defines the common properties of "key types" and some related
18 // constants.
19 //
20 // MathOpt provides optimized custom collections for variables and
21 // constraints. This file contains implementation details for these custom
22 // collections and should not be needed by users.
23 //
24 // Key types are types that are used as identifiers in the C++ interface where
25 // the ModelStorage is using typed integers. They are pairs of (storage,
26 // typed_index) where `storage` is a pointer on an ModelStorage and
27 // `typed_index` is the typed integer type used in ModelStorage.
28 //
29 // A key type K must match the following requirements:
30 // - K::IdType is a value type used for indices.
31 // - K has a constructor K(const ModelStorage*, K::IdType).
32 // - K is a value-semantic type.
33 // - K has a function with signature `K::IdType K::typed_id() const`.
34 // - K has a function with signature `const ModelStorage* K::storage() const`.
35 // It must return a non-null pointer.
36 // - K::IdType is a valid key for absl::flat_hash_map or absl::flat_hash_set
37 // (supports hash and ==).
38 //
39 // These requirements are met by Variable and LinearConstraint.
40 #ifndef OR_TOOLS_MATH_OPT_CPP_KEY_TYPES_H_
41 #define OR_TOOLS_MATH_OPT_CPP_KEY_TYPES_H_
42 
43 #include "absl/status/status.h"
44 #include "absl/strings/string_view.h"
46 
47 namespace operations_research {
48 namespace math_opt {
49 namespace internal {
50 
51 // The CHECK message to use when a KeyType::storage() is nullptr.
52 inline constexpr absl::string_view kKeyHasNullModelStorage =
53  "The input key has null .storage().";
54 
55 // The CHECK message to use when two KeyType with different storage() are used
56 // in the same collection.
57 inline constexpr absl::string_view kObjectsFromOtherModelStorage =
58  "The input objects belongs to another model.";
59 
60 // The Status message to use when an input KeyType is from an unexpected
61 // storage().
62 inline constexpr absl::string_view kInputFromInvalidModelStorage =
63  "the input does not belong to the same model";
64 
65 // Returns a failure when the input pointer is not nullptr and points to a
66 // different model storage than expected_storage (which must not be nullptr).
67 //
68 // Failure message is kInputFromInvalidModelStorage.
69 inline absl::Status CheckModelStorage(
70  const ModelStorage* const storage,
71  const ModelStorage* const expected_storage) {
72  if (expected_storage == nullptr) {
73  return absl::InternalError("expected_storage is nullptr");
74  }
75  if (storage != nullptr && storage != expected_storage) {
76  return absl::InvalidArgumentError(internal::kInputFromInvalidModelStorage);
77  }
78  return absl::OkStatus();
79 }
80 
81 } // namespace internal
82 } // namespace math_opt
83 } // namespace operations_research
84 
85 #endif // OR_TOOLS_MATH_OPT_CPP_KEY_TYPES_H_
constexpr absl::string_view kKeyHasNullModelStorage
Definition: key_types.h:52
constexpr absl::string_view kObjectsFromOtherModelStorage
Definition: key_types.h:57
absl::Status CheckModelStorage(const ModelStorage *const storage, const ModelStorage *const expected_storage)
Definition: key_types.h:69
constexpr absl::string_view kInputFromInvalidModelStorage
Definition: key_types.h:62
Collection of objects used to extend the Constraint Solver library.