Skip to content

Commit

Permalink
Update decimal (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
geseq authored Dec 25, 2023
1 parent c4b1101 commit 64b8029
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 46 deletions.
27 changes: 14 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
cmake_minimum_required(VERSION 3.20 FATAL_ERROR)

project("orderbook" LANGUAGES CXX)
set(CPP_ORDERBOOK orderbook)
project(${CPP_ORDERBOOK} LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -std=c++17 -stdlib=libc++ -lc++abi")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -stdlib=libc++")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -std=c++20 -stdlib=libc++ -lc++abi")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -stdlib=libc++")


set(CMAKE_THREAD_LIBS_INIT "-lpthread")
Expand All @@ -25,21 +26,21 @@ CPMUsePackageLock(package-lock.cmake)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
CPMAddPackage( NAME Boost VERSION 1.80.0 GITHUB_REPOSITORY "boostorg/boost" GIT_TAG "boost-1.80.0")

CPMAddPackage( NAME udecimal VERSION 0.6.1 GITHUB_REPOSITORY "geseq/cpp-udecimal" GIT_TAG "v0.7.0")
CPMAddPackage( NAME Boost VERSION 1.80.0 GITHUB_REPOSITORY "boostorg/boost" GIT_TAG "boost-1.80.0")
CPMAddPackage( NAME decimal VERSION 2.1.0 GITHUB_REPOSITORY "geseq/cpp-decimal" GIT_TAG "v2.1.0")
CPMAddPackage( NAME pool VERSION 0.6.1 GITHUB_REPOSITORY "geseq/cpp-pool" GIT_TAG "v0.5.0")

include_directories(udecimal_SOURCE_DIR)
include_directories(decimal_SOURCE_DIR)
include_directories(pool_SOURCE_DIR)

file(GLOB SOURCES "src/*.cpp")

include_directories(include)
add_library(orderbook STATIC ${SOURCES})
target_sources(orderbook INTERFACE include/orderbook.hpp)
target_include_directories(orderbook INTERFACE include/)
target_link_libraries(orderbook PRIVATE Boost::intrusive udecimal pool)
add_library(${CPP_ORDERBOOK} STATIC ${SOURCES})
target_sources(${CPP_ORDERBOOK} INTERFACE include/orderbook.hpp)
target_include_directories(${CPP_ORDERBOOK} INTERFACE include/)
target_link_libraries(${CPP_ORDERBOOK} PRIVATE Boost::intrusive decimal pool)

if (ENABLE_TESTING)
CPMAddPackage( NAME googletest GITHUB_REPOSITORY google/googletest VERSION 1.14.0 )
Expand All @@ -52,10 +53,10 @@ if (ENABLE_TESTING)
get_filename_component(test_name ${test} NAME)
message("Adding test: " ${test_name})
add_executable(${test_name} ${SOURCES} ${PROJECT_SOURCE_DIR}/test/${test_name})
target_link_libraries(${test_name} PRIVATE ${CMAKE_THREAD_LIBS_INIT} orderbook gtest gtest_main Boost::algorithm Boost::intrusive udecimal pool)
target_link_libraries(${test_name} PRIVATE ${CMAKE_THREAD_LIBS_INIT} ${CPP_ORDERBOOK} gtest gtest_main Boost::algorithm Boost::intrusive decimal pool)
add_test(${test_name} ${test_name})
set_property(TEST ${test_name} PROPERTY LABELS "test")
ENDFOREACH ()
endif()

target_link_libraries(main PRIVATE Boost::intrusive udecimal pool)
target_link_libraries(main PRIVATE Boost::intrusive decimal pool)
4 changes: 2 additions & 2 deletions include/orderbook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,15 @@ std::string OrderBook<Notification>::toString() {
auto loop = (b != bids.end() || a != asks.end());
while (loop) {
if (b != bids.end()) {
ss << b->totalQty().to_string() << "\t" << b->price().to_string();
ss << b->totalQty() << "\t" << b->price();
++b;
} else {
ss << "\t\t\t";
}

ss << " | ";
if (a != asks.end()) {
ss << a->price().to_string() << "\t" << a->totalQty().to_string();
ss << a->price() << "\t" << a->totalQty();
++a;
}

Expand Down
4 changes: 2 additions & 2 deletions include/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#include <cstdint>
#include <iostream>

#include "udecimal.hpp"
#include "decimal.hpp"

namespace orderbook {

using Decimal = udecimal::Decimal<8>;
using Decimal = decimal::U8;
using OrderID = uint64_t;

enum class Type : uint8_t {
Expand Down
23 changes: 0 additions & 23 deletions package-lock.cmake

This file was deleted.

12 changes: 6 additions & 6 deletions test/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ using orderbook::Type;

class NotificationBase {
public:
virtual ~NotificationBase() {}
virtual std::string to_string() const = 0;
virtual ~NotificationBase() = default;
[[nodiscard]] virtual std::string to_string() const = 0;
};

struct OrderNotification : public NotificationBase {
Expand All @@ -30,16 +30,16 @@ struct OrderNotification : public NotificationBase {
orderbook::Decimal qty_;
std::optional<orderbook::Error> error_;

OrderNotification(orderbook::MsgType msg_type, orderbook::OrderStatus status, orderbook::OrderID order_id, orderbook::Decimal qty,
OrderNotification(orderbook::MsgType msg_type, orderbook::OrderStatus status, orderbook::OrderID order_id, orderbook::Decimal& qty,
std::optional<orderbook::Error> error)
: msg_type_(msg_type), status_(status), order_id_(order_id), qty_(qty), error_(error){};

std::string to_string() const override {
[[nodiscard]] std::string to_string() const override {
std::ostringstream os;
if (error_.has_value()) {
os << msg_type_ << " " << status_ << " " << order_id_ << " " << qty_.to_string() << " Err" << *error_;
os << msg_type_ << " " << status_ << " " << order_id_ << " " << qty_ << " Err" << *error_;
} else {
os << msg_type_ << " " << status_ << " " << order_id_ << " " << qty_.to_string();
os << msg_type_ << " " << status_ << " " << order_id_ << " " << qty_;
}
return os.str();
}
Expand Down

0 comments on commit 64b8029

Please sign in to comment.