Skip to content

Commit

Permalink
C++ Client: vendor the "fmt" project and start to use it
Browse files Browse the repository at this point in the history
  • Loading branch information
kosak committed Nov 26, 2023
1 parent 8cfe8a8 commit e35a87f
Show file tree
Hide file tree
Showing 24 changed files with 22,410 additions and 34 deletions.
22 changes: 19 additions & 3 deletions cpp-client/deephaven/dhcore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ set(ALL_FILES
src/ticking/shift_processor.cc
src/ticking/space_mapper.cc
src/ticking/ticking.cc
src/utility/cython_support.cc
src/utility/cython_support.cc
src/utility/utility.cc

include/private/deephaven/dhcore/ticking/immer_table_state.h
Expand All @@ -49,7 +49,7 @@ set(ALL_FILES
include/public/deephaven/dhcore/container/row_sequence.h
include/public/deephaven/dhcore/ticking/barrage_processor.h
include/public/deephaven/dhcore/ticking/ticking.h
include/public/deephaven/dhcore/utility/cython_support.h
include/public/deephaven/dhcore/utility/cython_support.h
include/public/deephaven/dhcore/utility/utility.h

flatbuf/deephaven/flatbuf/Barrage_generated.h
Expand All @@ -73,7 +73,21 @@ set(ALL_FILES
third_party/flatbuffers/include/flatbuffers/verifier.h

third_party/roaring/include/roaring/roaring.c
)

third_party/fmt/include/fmt/args.h
third_party/fmt/include/fmt/chrono.h
third_party/fmt/include/fmt/color.h
third_party/fmt/include/fmt/compile.h
third_party/fmt/include/fmt/core.h
third_party/fmt/include/fmt/format-inl.h
third_party/fmt/include/fmt/format.h
third_party/fmt/include/fmt/os.h
third_party/fmt/include/fmt/ostream.h
third_party/fmt/include/fmt/printf.h
third_party/fmt/include/fmt/ranges.h
third_party/fmt/include/fmt/std.h
third_party/fmt/include/fmt/xchar.h
)

add_library(dhcore_objlib OBJECT ${ALL_FILES})
# In order to make a shared library suitable for Cython.
Expand All @@ -83,6 +97,7 @@ target_compile_options(dhcore_objlib PRIVATE -Wall -Werror -Wno-deprecated-decla

target_include_directories(dhcore_objlib PRIVATE include/private)
target_include_directories(dhcore_objlib PRIVATE third_party/flatbuffers/include)
target_include_directories(dhcore_objlib PRIVATE third_party/fmt/include)
target_include_directories(dhcore_objlib PRIVATE third_party/roaring/include)
target_include_directories(dhcore_objlib PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/public>)

Expand All @@ -104,6 +119,7 @@ add_library(dhcore SHARED $<TARGET_OBJECTS:dhcore_objlib>)
# TODO: How to avoid repetition here for target_include_directories?
target_include_directories(dhcore PRIVATE include/private)
target_include_directories(dhcore PRIVATE third_party/flatbuffers/include)
target_include_directories(dhcore PRIVATE third_party/fmt/include)
target_include_directories(dhcore PRIVATE third_party/roaring/include)
target_include_directories(dhcore PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/public>)
target_link_libraries(dhcore PUBLIC ${object_link_libs})
Expand Down
34 changes: 17 additions & 17 deletions cpp-client/deephaven/dhcore/src/utility/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@
#include <string>
#include <vector>

#define FMT_HEADER_ONLY
#include "fmt/chrono.h"
#include "fmt/core.h"

#ifdef __GNUG__
#include <cstdlib>
#include <memory>
#include <cxxabi.h>
#endif

static_assert(FMT_VERSION >= 100000);

namespace deephaven::dhcore::utility {

namespace {
Expand Down Expand Up @@ -196,20 +202,16 @@ void dumpTillPercentOrEnd(std::ostream &result, const char **fmt) {
} // namespace

std::string EpochMillisToStr(int64_t epoch_millis) {
time_t time_secs = epoch_millis / 1000;
auto millis = epoch_millis % 1000;
struct tm tm = {};
localtime_r(&time_secs, &tm);
char date_buffer[32]; // ample
char millis_buffer[32]; // ample
char tz_buffer[32]; // ample
strftime(date_buffer, sizeof(date_buffer), "%FT%T", &tm);
snprintf(millis_buffer, sizeof(millis_buffer), ".%03zd", millis);
strftime(tz_buffer, sizeof(tz_buffer), "%z", &tm);

SimpleOstringstream s;
s << date_buffer << millis_buffer << tz_buffer;
return std::move(s.str());
std::chrono::milliseconds ms(epoch_millis);
// Make a system_clock with a resolution of milliseconds so that the date is formatted with 3
// digits of fractional precision in the seconds field. Note also that system_clock is assumed by
// fmt to be UTC (this is what we want).
auto tp = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>(ms);
// %F - Equivalent to %Y-%m-%d, e.g. “1955-11-12”.
// T - literal 'T'
// %T - Equivalent to %H:%M:%S
// Z - literal 'Z'
return fmt::format("{:%FT%TZ}", tp);
}

std::int64_t
Expand Down Expand Up @@ -242,9 +244,7 @@ std::string demangle(const char* name) {
#endif

std::string ObjectId(const std::string &class_short_name, void *this_ptr) {
SimpleOstringstream s;
s << class_short_name << '(' << this_ptr << ')';
return std::move(s.str());
return fmt::format("{}({})", class_short_name, this_ptr);
}

std::string
Expand Down
8 changes: 8 additions & 0 deletions cpp-client/deephaven/dhcore/third_party/fmt/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Run manually to reformat a file:
# clang-format -i --style=file <file>
Language: Cpp
BasedOnStyle: Google
IndentPPDirectives: AfterHash
IndentCaseLabels: false
AlwaysBreakTemplateDeclarations: false
DerivePointerAlignment: false
Loading

0 comments on commit e35a87f

Please sign in to comment.