Skip to content

Commit

Permalink
[DXFC-402] Implement adding multiple symbols and removing symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
AnatolyKalin committed May 24, 2023
1 parent 78eca87 commit f0d06fc
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 96 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ set(dxFeedNativeAPIInternalSources
src/internal/Isolate.cpp
src/internal/JavaObjectHandler.cpp
src/internal/EventClassList.cpp
src/internal/SymbolList.cpp
src/internal/Common.cpp
)

Expand Down
3 changes: 3 additions & 0 deletions include/dxfeed_graal_cpp_api/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
#include "internal/Common.hpp"
#include "internal/Enum.hpp"
#include "internal/EventClassList.hpp"
#include "internal/SymbolList.hpp"
#include "internal/Id.hpp"
#include "internal/JavaObjectHandler.hpp"
#include "internal/NonCopyable.hpp"
#include "internal/RawListWrapper.hpp"
#include "internal/context/ApiContext.hpp"
#include "internal/managers/DXEndpointManager.hpp"
Expand Down
5 changes: 5 additions & 0 deletions include/dxfeed_graal_cpp_api/api/DXFeedSubscription.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ class DXFeedSubscription : public SharedEntity {
}
}

template <typename SymbolIt>
void addSymbols(SymbolIt begin, SymbolIt end) noexcept {

}

template <typename SymbolsCollection> void addSymbols(SymbolsCollection &&collection) noexcept;

template <typename Symbol> void addSymbols(std::initializer_list<Symbol> collection) noexcept;
Expand Down
90 changes: 0 additions & 90 deletions include/dxfeed_graal_cpp_api/internal/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,56 +92,6 @@ inline auto now() {
.count();
}

namespace handler_utils {

struct SymbolsList {
template <typename SymbolIt>
static std::unique_ptr<SymbolsList> create(SymbolIt begin, SymbolIt end) noexcept {
auto size = std::distance(begin, end);

if (size <= 0) {
return {};
}

auto list = create(size);

if (list->isEmpty()) {
return {};
}

std::size_t i = 0;

for (auto it = begin; it != end; it++, i++) {
list->set(i, *it);
}

return list;
}

void set(std::size_t index, std::uint32_t id) noexcept;

[[nodiscard]] bool isEmpty() const noexcept;

[[nodiscard]] std::size_t size() const noexcept;

void *getHandler() noexcept;

~SymbolsList() noexcept;

private:
static std::unique_ptr<SymbolsList> create(std::size_t size) noexcept;

SymbolsList() noexcept;

struct Impl;

std::unique_ptr<Impl> impl_;
};

} // namespace handler_utils



template <typename M, typename F, typename... Args> inline void callWithLock(M &mtx, F &&f, Args &&...args) noexcept {
std::once_flag once{};

Expand Down Expand Up @@ -642,44 +592,4 @@ template <Integral F, Integral M, Integral S, Integral B> static constexpr F set
}
}

template <typename T> struct Id {
using ValueType = std::size_t;

private:
const ValueType value_{};

explicit Id(ValueType value) : value_{value} {}

public:
static Id<T> getNext() {
static std::atomic<ValueType> value{};

return Id<T>{value++};
}

[[nodiscard]] ValueType getValue() const { return value_; }

explicit operator ValueType() const { return value_; }

static Id<T> from(ValueType value) { return Id<T>{value}; }

template <typename U> bool operator==(const Id<U> &id) const { return getValue() == id.getValue(); }

template <typename U> auto operator<=>(const Id<U> &id) const { return getValue() <=> id.getValue(); }
};

template <class T> class NonCopyable {
public:
NonCopyable(const NonCopyable &) = delete;
T &operator=(const T &) = delete;

protected:
NonCopyable() = default;
~NonCopyable() = default;
};

} // namespace dxfcpp

template <typename T> struct std::hash<dxfcpp::Id<T>> {
std::size_t operator()(const dxfcpp::Id<T> &id) const noexcept { return id.getValue(); }
};
40 changes: 40 additions & 0 deletions include/dxfeed_graal_cpp_api/internal/Id.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2023 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0

#pragma once

#include <atomic>
#include <cstdint>

namespace dxfcpp {

template <typename T> struct Id {
using ValueType = std::size_t;

private:
const ValueType value_{};

explicit Id(ValueType value) : value_{value} {}

public:
static Id<T> getNext() {
static std::atomic<ValueType> value{};

return Id<T>{value++};
}

[[nodiscard]] ValueType getValue() const { return value_; }

explicit operator ValueType() const { return value_; }

static Id<T> from(ValueType value) { return Id<T>{value}; }

template <typename U> bool operator==(const Id<U> &id) const { return getValue() == id.getValue(); }

template <typename U> auto operator<=>(const Id<U> &id) const { return getValue() <=> id.getValue(); }
};
} // namespace dxfcpp

template <typename T> struct std::hash<dxfcpp::Id<T>> {
std::size_t operator()(const dxfcpp::Id<T> &id) const noexcept { return id.getValue(); }
};
18 changes: 18 additions & 0 deletions include/dxfeed_graal_cpp_api/internal/NonCopyable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2023 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0

#pragma once

namespace dxfcpp {

template <class T> class NonCopyable {
public:
NonCopyable(const NonCopyable &) = delete;
T &operator=(const T &) = delete;

protected:
NonCopyable() = default;
~NonCopyable() = default;
};

}
56 changes: 56 additions & 0 deletions include/dxfeed_graal_cpp_api/internal/SymbolList.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2023 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0

#pragma once

#include <cstdint>
#include <memory>
#include <utility>

namespace dxfcpp {

struct SymbolList {
template <typename SymbolIt> static std::unique_ptr<SymbolList> create(SymbolIt begin, SymbolIt end) noexcept {
auto size = std::distance(begin, end);

if (size <= 0) {
return {};
}

auto list = create(size);

if (list->isEmpty()) {
return {};
}

std::size_t i = 0;

for (auto it = begin; it != end; it++, i++) {
list->set(i, *it);
}

return list;
}

template <typename Symbol>
void set(std::size_t index, Symbol&& symbol) noexcept;

[[nodiscard]] bool isEmpty() const noexcept;

[[nodiscard]] std::size_t size() const noexcept;

void *getHandler() noexcept;

~SymbolList() noexcept;

private:
static std::unique_ptr<SymbolList> create(std::size_t size) noexcept;

SymbolList() noexcept;

struct Impl;

std::unique_ptr<Impl> impl_;
};

}
8 changes: 5 additions & 3 deletions include/dxfeed_graal_cpp_api/internal/utils/debug/Debug.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

#pragma once

#define DXFCPP_DEBUG 1
#define DXFCPP_TRACE_LISTS 1
#define DXFCPP_TRACE_ISOLATES 1
// #define DXFCPP_DEBUG 1
// #define DXFCPP_TRACE_LISTS 1
// #define DXFCPP_TRACE_ISOLATES 1

#ifndef DXFCPP_DEBUG
# define DXFCPP_DEBUG 0
Expand All @@ -26,6 +26,8 @@ namespace dxfcpp {

#if DXFCPP_DEBUG == 0

static inline std::string getDebugName() { return {}; }

struct Debugger {
static constexpr bool isDebug = false;
static constexpr bool traceIsolates = false;
Expand Down
6 changes: 3 additions & 3 deletions src/api/DXFeedSubscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void DXFeedSubscription::removeSymbolImpl(const char *symbol) noexcept {
[handler = bit_cast<dxfg_subscription_t *>(handler_.get()), symbol](auto threadHandle) {
dxfg_string_symbol_t s{{STRING}, symbol};

return dxfg_DXFeedSubscription_removeSymbol(threadHandle, handler, (dxfg_symbol_t *)&s) == 0;
return dxfg_DXFeedSubscription_removeSymbol(threadHandle, handler, bit_cast<dxfg_symbol_t *>(&s)) == 0;
},
false);
}
Expand Down Expand Up @@ -102,8 +102,8 @@ DXFeedSubscription::DXFeedSubscription(const EventTypeEnum &eventType) noexcept
nullptr));
}

JavaObjectHandler<DXFeedSubscription> DXFeedSubscription::createSubscriptionHandlerFromEventClassList(
const std::unique_ptr<EventClassList> &list) noexcept {
JavaObjectHandler<DXFeedSubscription>
DXFeedSubscription::createSubscriptionHandlerFromEventClassList(const std::unique_ptr<EventClassList> &list) noexcept {
return JavaObjectHandler<DXFeedSubscription>(
runIsolatedOrElse([listHandler = bit_cast<dxfg_event_clazz_list_t *>(list->getHandler())](
auto threadHandle) { return dxfg_DXFeedSubscription_new2(threadHandle, listHandler); },
Expand Down
63 changes: 63 additions & 0 deletions src/internal/SymbolList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2023 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0

#include <dxfg_api.h>

#include <dxfeed_graal_c_api/api.h>
#include <dxfeed_graal_cpp_api/api.hpp>

namespace dxfcpp {

// TODO: StringSymbol, WildcardSymbol, CandleSymbol, indexed_event_subscription_symbol, time_series_subscription_symbol,
// ::toGraal

struct SymbolList::Impl
: public RawListWrapper<dxfg_symbol_list,
[]<typename Symbol>(dxfg_symbol_list &list, std::size_t index, Symbol &&symbol) {
dxfg_symbol_t *dxfgSymbol;

if constexpr (std::is_same_v<std::decay_t<Symbol>, std::string>) {
dxfg_string_symbol_t s{{STRING}, symbol.c_str()};

dxfgSymbol = bit_cast<dxfg_symbol_t *>(&s);
}
#if __cpp_lib_string_view
else if constexpr (std::is_same_v<std::decay_t<Symbol>, std::string_view>) {
dxfg_string_symbol_t s{{STRING}, symbol.data()};

dxfgSymbol = bit_cast<dxfg_symbol_t *>(&s);
}
#endif
else {
dxfg_string_symbol_t s{{STRING}, symbol};

dxfgSymbol = bit_cast<dxfg_symbol_t *>(&s);
}

list.elements[index] = dxfgSymbol;
}> {
};

SymbolList::SymbolList() noexcept : impl_(std::make_unique<SymbolList::Impl>()) {}

std::unique_ptr<SymbolList> SymbolList::create(std::size_t size) noexcept {
auto result = std::unique_ptr<SymbolList>(new SymbolList{});

result->impl_->init(static_cast<std::uint32_t>(size));

return result;
}

template <typename Symbol> void SymbolList::set(std::size_t index, Symbol &&symbol) noexcept {
impl_->set(index, symbol);
}

bool SymbolList::isEmpty() const noexcept { return impl_->isEmpty(); }

std::size_t SymbolList::size() const noexcept { return impl_->size(); }

void *SymbolList::getHandler() noexcept { return impl_->getHandler(); }

SymbolList::~SymbolList() noexcept = default;

} // namespace dxfcpp

0 comments on commit f0d06fc

Please sign in to comment.