Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
safe_attr_ops.h
Go to the documentation of this file.
1// Copyright 2010-2025 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// Common code between python & C bindings.
15#ifndef OR_TOOLS_MATH_OPT_ELEMENTAL_SAFE_ATTR_OPS_H_
16#define OR_TOOLS_MATH_OPT_ELEMENTAL_SAFE_ATTR_OPS_H_
17
18#include <cstdint>
19#include <vector>
20
21#include "absl/status/status.h"
22#include "absl/status/statusor.h"
23#include "absl/strings/str_cat.h"
27
29
30template <typename AttrType>
31struct AttrOp {
34 using ValueType = typename Descriptor::ValueType;
35
36 static absl::StatusOr<AttrType> SafeCastAttr(int attr) {
37 if (attr < 0 || attr >= Descriptor::NumAttrs()) {
38 return absl::InvalidArgumentError(
39 absl::StrCat("invalid attribute ", attr));
40 }
41 return static_cast<AttrType>(attr);
42 }
43
44 static absl::StatusOr<ValueType> Get(Elemental* e, int attr, Key key) {
45 ASSIGN_OR_RETURN(const auto typed_attr, SafeCastAttr(attr));
46 return e->GetAttr<Elemental::StatusPolicy>(typed_attr, key);
47 }
48
49 static absl::Status Set(Elemental* e, int attr, Key key, ValueType value) {
50 ASSIGN_OR_RETURN(const auto typed_attr, SafeCastAttr(attr));
51 return e->SetAttr<Elemental::StatusPolicy>(typed_attr, key, value);
52 }
53
54 static absl::StatusOr<bool> IsNonDefault(Elemental* e, int attr, Key key) {
55 ASSIGN_OR_RETURN(const auto typed_attr, SafeCastAttr(attr));
56 return e->AttrIsNonDefault<Elemental::StatusPolicy>(typed_attr, key);
57 }
58
59 static absl::StatusOr<int64_t> NumNonDefaults(Elemental* e, int attr) {
60 ASSIGN_OR_RETURN(const auto typed_attr, SafeCastAttr(attr));
61 return e->AttrNumNonDefaults(typed_attr);
62 }
63
64 static absl::StatusOr<std::vector<Key>> GetNonDefaults(Elemental* e,
65 int attr) {
66 ASSIGN_OR_RETURN(const auto typed_attr, SafeCastAttr(attr));
67 return e->AttrNonDefaults(typed_attr);
68 }
69};
70
71} // namespace operations_research::math_opt
72
73#endif // OR_TOOLS_MATH_OPT_ELEMENTAL_SAFE_ATTR_OPS_H_
#define ASSIGN_OR_RETURN(lhs, rexpr)
Policy::CheckResultT SetAttr(AttrType a, AttrKeyFor< AttrType > key, ValueTypeFor< AttrType > value)
Definition elemental.h:448
std::vector< AttrKeyFor< AttrType > > AttrNonDefaults(const AttrType a) const
Return the vector of attribute keys where a is non-default.
Definition elemental.h:215
int64_t AttrNumNonDefaults(const AttrType a) const
Returns the number of keys where a is non-default.
Definition elemental.h:221
Policy::template Wrapped< bool > AttrIsNonDefault(AttrType a, AttrKeyFor< AttrType > key) const
Definition elemental.h:441
Policy::template Wrapped< ValueTypeFor< AttrType > > GetAttr(AttrType a, AttrKeyFor< AttrType > key) const
Definition elemental.h:434
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
AttrKey< AttrTypeDescriptorT< AttrType >::kNumKeyElements, typename AttrTypeDescriptorT< AttrType >::Symmetry > AttrKeyFor
The type of the AttrKey for attribute type AttrType.
AllAttrs::TypeDescriptor< AllAttrs::GetIndex< AttrT >()> AttrTypeDescriptorT
Returns the descriptor for attribute AttrT.
static absl::StatusOr< ValueType > Get(Elemental *e, int attr, Key key)
static absl::StatusOr< bool > IsNonDefault(Elemental *e, int attr, Key key)
static absl::StatusOr< AttrType > SafeCastAttr(int attr)
static absl::Status Set(Elemental *e, int attr, Key key, ValueType value)
AttrTypeDescriptorT< AttrType > Descriptor
typename Descriptor::ValueType ValueType
static absl::StatusOr< int64_t > NumNonDefaults(Elemental *e, int attr)
static absl::StatusOr< std::vector< Key > > GetNonDefaults(Elemental *e, int attr)