Skip to content

Commit

Permalink
Merge branch 'v99bugfix' of github.com:google/or-tools into v99bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Oct 7, 2024
2 parents f22fed7 + c41dbb8 commit f89f6a7
Show file tree
Hide file tree
Showing 63 changed files with 11,607 additions and 294 deletions.
2 changes: 1 addition & 1 deletion examples/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ endif()
if(BUILD_CXX_EXAMPLES)
file(GLOB CXX_SRCS "*.cc")
foreach(FILE_NAME IN LISTS CXX_SRCS)
add_cxx_test(FILE_NAME ${FILE_NAME})
ortools_cxx_test(FILE_NAME ${FILE_NAME})
endforeach()
endif()

Expand Down
19 changes: 19 additions & 0 deletions ortools/base/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,16 @@ cc_library(
],
)

cc_library(
name = "parse_test_proto",
hdrs = ["parse_test_proto.h"],
deps = [
":gmock",
"@com_google_absl//absl/log:check",
"@com_google_protobuf//:protobuf",
],
)

cc_library(
name = "path",
srcs = ["path.cc"],
Expand Down Expand Up @@ -450,6 +460,15 @@ cc_library(
],
)

cc_library(
name = "proto_enum_utils",
hdrs = ["proto_enum_utils.h"],
deps = [
"@com_google_absl//absl/types:span",
"@com_google_protobuf//:protobuf",
],
)

cc_library(
name = "ptr_util",
hdrs = ["ptr_util.h"],
Expand Down
53 changes: 53 additions & 0 deletions ortools/base/parse_test_proto.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2010-2024 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
//
// 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 OR_TOOLS_BASE_PARSE_TEST_PROTO_H_
#define OR_TOOLS_BASE_PARSE_TEST_PROTO_H_

#include <memory>
#include <ostream>
#include <string>

#include "google/protobuf/message.h"
#include "google/protobuf/text_format.h"
#include "gtest/gtest.h"

namespace google::protobuf::contrib::parse_proto {

namespace parse_proto_internal {

class ParseProtoHelper {
public:
explicit ParseProtoHelper(std::string_view asciipb) : asciipb_(asciipb) {}
template <class T>
operator T() { // NOLINT(runtime/explicit)
T result;
const bool ok = ::google::protobuf::TextFormat::TextFormat::ParseFromString(
asciipb_, &result);
EXPECT_TRUE(ok) << "Failed to parse text proto: " << asciipb_;
return result;
}

private:
const std::string asciipb_;
};

} // namespace parse_proto_internal

parse_proto_internal::ParseProtoHelper ParseTestProto(std::string_view input) {
return parse_proto_internal::ParseProtoHelper(input);
}

} // namespace google::protobuf::contrib::parse_proto

#endif // OR_TOOLS_BASE_PARSE_TEST_PROTO_H_
27 changes: 27 additions & 0 deletions ortools/base/parse_text_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#ifndef OR_TOOLS_BASE_PARSE_TEXT_PROTO_H_
#define OR_TOOLS_BASE_PARSE_TEXT_PROTO_H_

#include <string_view>

#include "absl/log/absl_check.h"
#include "google/protobuf/message.h"
#include "google/protobuf/text_format.h"
Expand All @@ -32,6 +34,31 @@ T ParseTextOrDie(const std::string& input) {
return result;
}

namespace text_proto_internal {

class ParseProtoHelper {
public:
explicit ParseProtoHelper(std::string_view asciipb) : asciipb_(asciipb) {}
template <class T>
operator T() { // NOLINT(runtime/explicit)
T result;
const bool ok = ::google::protobuf::TextFormat::TextFormat::ParseFromString(
asciipb_, &result);
CHECK(ok) << "Failed to parse text proto: " << asciipb_;
return result;
}

private:
const std::string asciipb_;
};

} // namespace text_proto_internal

text_proto_internal::ParseProtoHelper ParseTextProtoOrDie(
std::string_view input) {
return text_proto_internal::ParseProtoHelper(input);
}

} // namespace google::protobuf::contrib::parse_proto

#endif // OR_TOOLS_BASE_PARSE_TEXT_PROTO_H_
209 changes: 209 additions & 0 deletions ortools/base/proto_enum_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
// Copyright 2010-2024 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
//
// 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 OR_TOOLS_BASE_PROTO_ENUM_UTILS_H_
#define OR_TOOLS_BASE_PROTO_ENUM_UTILS_H_

// Provides utility functions that help with handling Protocol Buffer enums.
//
// Examples:
//
// A function to easily iterate over all defined values of an enum known at
// compile-time:
//
// for (Proto::Enum e : EnumerateEnumValues<Proto::Enum>()) {
// ...
// }
//

#include <iterator>
#include <type_traits>

#include "absl/types/span.h"
#include "google/protobuf/descriptor.pb.h"

namespace google::protobuf::contrib::utils {

using google::protobuf::GetEnumDescriptor;
using google::protobuf::RepeatedField;

template <typename E>
class ProtoEnumIterator;

template <typename E>
class EnumeratedProtoEnumView;

template <typename E>
bool operator==(const ProtoEnumIterator<E>& a, const ProtoEnumIterator<E>& b);

template <typename E>
bool operator!=(const ProtoEnumIterator<E>& a, const ProtoEnumIterator<E>& b);

// Generic Proto enum iterator.
template <typename E>
class ProtoEnumIterator {
public:
typedef E value_type;
typedef std::forward_iterator_tag iterator_category;
typedef int difference_type;
typedef E* pointer;
typedef E& reference;

ProtoEnumIterator() : current_(0) {}

ProtoEnumIterator(const ProtoEnumIterator& other)
: current_(other.current_) {}

ProtoEnumIterator& operator=(const ProtoEnumIterator& other) {
current_ = other.current_;
return *this;
}

ProtoEnumIterator operator++(int) {
ProtoEnumIterator other(*this);
++(*this);
return other;
}

ProtoEnumIterator& operator++() {
++current_;
return *this;
}

E operator*() const {
return static_cast<E>(GetEnumDescriptor<E>()->value(current_)->number());
}

private:
explicit ProtoEnumIterator(int current) : current_(current) {}

int current_;

// Only EnumeratedProtoEnumView can instantiate ProtoEnumIterator.
friend class EnumeratedProtoEnumView<E>;
friend bool operator==
<>(const ProtoEnumIterator<E>& a, const ProtoEnumIterator<E>& b);
friend bool operator!=
<>(const ProtoEnumIterator<E>& a, const ProtoEnumIterator<E>& b);
};

template <typename E>
bool operator==(const ProtoEnumIterator<E>& a, const ProtoEnumIterator<E>& b) {
return a.current_ == b.current_;
}

template <typename E>
bool operator!=(const ProtoEnumIterator<E>& a, const ProtoEnumIterator<E>& b) {
return a.current_ != b.current_;
}

template <typename E>
class EnumeratedProtoEnumView {
public:
typedef E value_type;
typedef ProtoEnumIterator<E> iterator;
iterator begin() const { return iterator(0); }
iterator end() const {
return iterator(GetEnumDescriptor<E>()->value_count());
}
};

// Returns an EnumeratedProtoEnumView that can be iterated over:
// for (Proto::Enum e : EnumerateEnumValues<Proto::Enum>()) {
// ...
// }
template <typename E>
EnumeratedProtoEnumView<E> EnumerateEnumValues() {
return EnumeratedProtoEnumView<E>();
}

// Returns a view that allows to iterate directly over the enum values
// in an enum repeated field, wrapping the repeated field with a type-safe
// iterator that provides access to the enum values.
//
// for (Enum enum :
// REPEATED_ENUM_ADAPTER(message, repeated_enum_field)) {
// ...
// }
//
// It provides greater safety than iterating over the enum directly, as the
// following will fail to type-check:
//
// .proto
// RightEnum enum = 5;
//
// client .cc
// for (WrongEnum e : REPEATED_ENUM_ADAPTER(proto, enum)) { <- Error: Cannot
// cast from
// RightEnum to
// WrongEnum
// }
//
// NOTE: As per http://shortn/_CYfjpruK6N, unrecognized enum values are treated
// differently between proto2 and proto3.
//
// For proto2, they are stripped out from the message when read, so all
// unrecognized enum values from the wire format will be skipped when iterating
// over the wrapper (this is the same behavior as iterating over the
// RepeatedField<int> directly).
//
// For proto3, they are left as-is, so unrecognized enum values from the wire
// format will still be returned when iterating over the wrapper (this is the
// same behavior as iterating over the RepeatedField<int> directly).
//
#define REPEATED_ENUM_ADAPTER(var, field) \
google::protobuf::contrib::utils::internal::RepeatedEnumView< \
decltype(var.field(0))>(var.field())

// ==== WARNING TO USERS ====
// Below are internal implementations, not public API, and may change without
// notice. Do NOT use directly.

namespace internal {

// Implementation for REPEATED_ENUM_ADAPTER. This does not provide type safety
// thus should be used through REPEATED_ENUM_ADAPTER only. See cr/246914845 for
// context.
template <typename E>
class RepeatedEnumView {
public:
class Iterator : public std::iterator<std::input_iterator_tag, E> {
public:
explicit Iterator(RepeatedField<int>::const_iterator ptr) : ptr_(ptr) {}
bool operator==(const Iterator& it) const { return ptr_ == it.ptr_; }
bool operator!=(const Iterator& it) const { return ptr_ != it.ptr_; }
Iterator& operator++() {
++ptr_;
return *this;
}
E operator*() const { return static_cast<E>(*ptr_); }

private:
RepeatedField<int>::const_iterator ptr_;
};

explicit RepeatedEnumView(const RepeatedField<int>& repeated_field)
: repeated_field_(repeated_field) {}

Iterator begin() const { return Iterator(repeated_field_.begin()); }
Iterator end() const { return Iterator(repeated_field_.end()); }

private:
const RepeatedField<int>& repeated_field_;
};

} // namespace internal

} // namespace google::protobuf::contrib::utils

#endif // OR_TOOLS_BASE_PROTO_ENUM_UTILS_H_
6 changes: 4 additions & 2 deletions ortools/constraint_solver/samples/code_samples.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

"""Helper macro to compile and test code samples."""

load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test")

def code_sample_cc(name):
native.cc_binary(
cc_binary(
name = name + "_cc",
srcs = [name + ".cc"],
deps = [
Expand All @@ -25,7 +27,7 @@ def code_sample_cc(name):
],
)

native.cc_test(
cc_test(
name = name + "_cc_test",
size = "small",
srcs = [name + ".cc"],
Expand Down
2 changes: 1 addition & 1 deletion ortools/flatzinc/cp-sat.msc.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"mznlib": "../cp-sat",
"executable": "@FZ_REL_INSTALL_BINARY@",
"tags": ["cp-sat", "cp", "lcg", "int"],
"stdFlags": ["-a", "-f", "-p", "-r", "-s", "-v"],
"stdFlags": ["-a", "-i", "-f", "-p", "-r", "-s", "-v"],
"extraFlags": [
["--params", "Provide parameters interpreted as a text SatParameters proto", "string", ""]
],
Expand Down
Loading

0 comments on commit f89f6a7

Please sign in to comment.