introduce struct to hold flags in cpp init code; rename classes

This commit is contained in:
Laurent Perron
2021-03-24 09:54:39 +01:00
parent f9f4ab4d5f
commit 6a562b0c29
6 changed files with 54 additions and 20 deletions

View File

@@ -12,6 +12,7 @@
// limitations under the License.
using System;
using Google.OrTools.Init;
using Google.OrTools.LinearSolver;
public class CsIntegerProgramming
@@ -100,7 +101,11 @@ public class CsIntegerProgramming
static void Main()
{
Google.OrTools.Init.Init.InitCppLogging("csintegerprogramming.cs", true, false);
CppBridge.InitLogging("csintegerprogramming.cs");
CppFlags flags = new CppFlags();
flags.logtostderr = true;
flags.log_prefix = false;
CppBridge.SetFlags(flags);
Console.WriteLine("---- Integer programming example with GLPK ----");
RunIntegerProgrammingExample("GLPK");

View File

@@ -116,7 +116,9 @@ def main():
if __name__ == '__main__':
init.Init.InitCppLogging('integer_programming.py',
/*logtostderr=*/True,
/*log_prefix=*/False);
init.CppBridge.InitLogging('integer_programming.py')
cpp_flags = init.CppFlags()
cpp_flags.logtostderr = True
cpp_flags.log_prefix = False
init.CppBridge.SetFlags(cpp_flags)
main()

View File

@@ -10,9 +10,16 @@
%unignore operations_research;
%unignore operations_research::Init;
%unignore operations_research::Init::InitCppLogging;
%unignore operations_research::Init::LoadGurobiSharedLibrary;
// Expose the flags structure.
%unignore operations_research::CppFlags;
%unignore operations_research::CppFlags::logtostderr;
%unignore operations_research::CppFlags::log_prefix;
// Expose the static methods of the bridge class.
%unignore operations_research::CppBridge;
%unignore operations_research::CppBridge::InitLogging;
%unignore operations_research::CppBridge::SetFlags;
%unignore operations_research::CppBridge::LoadGurobiSharedLibrary;
%include "ortools/init/init.h"

View File

@@ -8,19 +8,25 @@
namespace operations_research {
struct CppFlags {
bool logtostderr = false;
bool log_prefix = false;
};
// This class performs various C++ initialization.
// It is meant to be used once at the start of a program.
class Init {
class CppBridge {
public:
// Initialize the C++ logging layer.
// If logtostderr is false, all C++ logging will be ignored.
// If true, all logging will the displayed on stderr.
static void InitCppLogging(const std::string& program_name, bool logtostderr,
bool log_prefix) {
absl::SetFlag(&FLAGS_logtostderr, logtostderr);
absl::SetFlag(&FLAGS_log_prefix, log_prefix);
static void InitLogging(const std::string& program_name) {
google::InitGoogleLogging(program_name.c_str());
}
// Sets all the C++ flags contained in the CppFlags structure.
static void SetFlags(const CppFlags& flags) {
absl::SetFlag(&FLAGS_logtostderr, flags.logtostderr);
absl::SetFlag(&FLAGS_log_prefix, flags.log_prefix);
}
// Load the gurobi shared library.
// This is necessary if the library is installed in a non canonical

View File

@@ -10,9 +10,16 @@
%unignore operations_research;
%unignore operations_research::Init;
%rename (initCppLogging) operations_research::Init::InitCppLogging;
%rename (logGurobiSharedLibrary) operations_research::Init::LoadGurobiSharedLibrary;
// Expose the flags structure.
%unignore operations_research::CppFlags;
%unignore operations_research::CppFlags::logtostderr;
%unignore operations_research::CppFlags::log_prefix;
// Expose the static methods of the bridge class.
%unignore operations_research::CppBridge;
%rename (initLogging) operations_research::CppBridge::InitLogging;
%rename (setFlags) operations_research::CppBridge::SetFlags;
%rename (logGurobiSharedLibrary) operations_research::CppBridge::LoadGurobiSharedLibrary;
%include "ortools/init/init.h"

View File

@@ -10,9 +10,16 @@
%unignore operations_research;
%unignore operations_research::Init;
%unignore operations_research::Init::InitCppLogging;
%unignore operations_research::Init::LoadGurobiSharedLibrary;
// Expose the flags structure.
%unignore operations_research::CppFlags;
%unignore operations_research::CppFlags::logtostderr;
%unignore operations_research::CppFlags::log_prefix;
// Expose the static methods of the bridge class.
%unignore operations_research::CppBridge;
%unignore operations_research::CppBridge::InitLogging;
%unignore operations_research::CppBridge::SetFlags;
%unignore operations_research::CppBridge::LoadGurobiSharedLibrary;
%include "ortools/init/init.h"