Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add init flags and make init less intrusive by default (fixes #64) #163

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmake/geogram.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ if(NOT DEFINED GEOGRAM_WITH_EXPLORAGRAM)
else()
set(GEOGRAM_WITH_EXPLORAGRAM OFF)
endif()
endif()
endif()

if (GEOGRAM_WITH_GEOGRAMPLUS)
message(STATUS "addon: geogramplus")
Expand Down
2 changes: 2 additions & 0 deletions src/lib/geogram/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ include_directories(${PROJECT_BINARY_DIR}/src/lib)

add_library(geogram ${SOURCES} $<TARGET_OBJECTS:geogram_third_party>)

target_compile_features(geogram PUBLIC cxx_std_17)

target_include_directories(geogram PRIVATE
${PROJECT_SOURCE_DIR}/src/lib/geogram/third_party/amgcl
)
Expand Down
46 changes: 33 additions & 13 deletions src/lib/geogram/basic/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,34 @@

#include <sstream>
#include <iomanip>
#include <optional>

#ifdef GEO_OS_EMSCRIPTEN
#include <emscripten.h>
#endif

namespace GEO {

void initialize(int flags) {
static bool initialized = false;
namespace {

if(initialized) {
return;
}
struct MySingleton {
static std::optional<MySingleton>& instance(int flags) {
static std::optional<MySingleton> instance(flags);
return instance;
}

MySingleton(int flags) {

// When locale is set to non-us countries,
// this may cause some problems when reading
// floating-point numbers (some locale expect
// a decimal ',' instead of a '.').
// This restores the default behavior for
// reading floating-point numbers.
#ifdef GEO_OS_UNIX
setenv("LC_NUMERIC","POSIX",1);
if (flags & GEOGRAM_INSTALL_LOCALE) {
setenv("LC_NUMERIC","POSIX",1);
}
#endif

#ifndef GEOGRAM_PSM
Expand All @@ -99,16 +105,19 @@ namespace GEO {
Delaunay::initialize();

#ifndef GEOGRAM_PSM
Biblio::initialize();
if (flags & GEOGRAM_INSTALL_BIBLIO) {
Biblio::initialize();
}
#endif
atexit(GEO::terminate);

#ifndef GEOGRAM_PSM
mesh_io_initialize();
#endif

// Clear last system error
errno = 0;
if (flags & GEOGRAM_INSTALL_ERRNO) {
errno = 0;
}

#ifndef GEOGRAM_PSM
// Register attribute types that can be saved into files.
Expand Down Expand Up @@ -154,11 +163,10 @@ namespace GEO {
geo_declare_image_serializer<ImageSerializer_xpm>("xpm") ;
geo_declare_image_serializer<ImageSerializer_pgm>("pgm") ;
#endif
}

initialized = true;
}

void terminate() {
~MySingleton() {

if(
CmdLine::arg_is_declared("sys:stats") &&
CmdLine::get_arg_bool("sys:stats")
Expand All @@ -181,6 +189,18 @@ namespace GEO {
Logger::terminate();
FileSystem::terminate();
Environment::terminate();

}
};

}

void initialize(int flags) {
MySingleton::instance(flags);
}

void terminate() {
MySingleton::instance(GEOGRAM_INSTAL_NONE).reset();
}
}

28 changes: 24 additions & 4 deletions src/lib/geogram/basic/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,25 @@ namespace GEO {
* \brief Symbolic constants for GEO::initialize()
*/
enum {
GEOGRAM_NO_HANDLER = 0,
GEOGRAM_INSTALL_HANDLERS = 1
};
/// Do not install error handlers
GEOGRAM_INSTAL_NONE = 0,
/// Install Geogram's signal handlers
GEOGRAM_INSTALL_HANDLERS = 1,
/// Sets the locale to POSIX
GEOGRAM_INSTALL_LOCALE = 2,
/// Reset errno to 0
GEOGRAM_INSTALL_ERRNO = 4,
/// Enable or disable FPE during initialization
GEOGRAM_INSTALL_FPE = 8,
/// Enable global citation database
GEOGRAM_INSTALL_BIBLIO = 16,
/// Install everything
GEOGRAM_INSTALL_ALL = GEOGRAM_INSTALL_HANDLERS
| GEOGRAM_INSTALL_LOCALE
| GEOGRAM_INSTALL_ERRNO
| GEOGRAM_INSTALL_FPE
| GEOGRAM_INSTALL_BIBLIO
};

/**
* \brief Initialize Geogram
Expand All @@ -76,13 +92,17 @@ namespace GEO {
* test suite. Else continuous integration tests hang because of the dialog
* box. Normal users may want to keep the default Windows behavior, since
* geogram error handlers may make debugging more difficult under Windows.
* - GEOGRAM_INSTALL_LOCALE to set the locale to POSIX.
* - GEOGRAM_INSTALL_ERRNO to clear the last system error.
* - GEOGRAM_INSTALL_FPE to enable/disable floating point exceptions.
* - GEOGRAM_INSTALL_BIBLIO to enable global citation database.
* \details This function must be called once at the very beginning of a
* program to initialize the Vorpaline library. It also installs a exit()
* handler that calls function terminate() when the program exists
* normally. If it is called multiple times, then the supplemental calls
* have no effect.
*/
void GEOGRAM_API initialize(int flags = GEOGRAM_INSTALL_HANDLERS);
void GEOGRAM_API initialize(int flags = GEOGRAM_INSTAL_NONE);

/**
* \brief Cleans up Geogram
Expand Down
4 changes: 3 additions & 1 deletion src/lib/geogram/basic/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,9 @@ namespace GEO {
// Initialize Process default values
enable_multithreading(multithreading_enabled_);
set_max_threads(number_of_cores());
enable_FPE(fpe_enabled_);
if (flags & GEOGRAM_INSTALL_FPE) {
enable_FPE(fpe_enabled_);
}
enable_cancel(cancel_enabled_);

start_time_ = Stopwatch::now();
Expand Down
Loading