Skip to content

Commit

Permalink
IsolatedCommon
Browse files Browse the repository at this point in the history
IsolatedDXEndpoint::getState
  • Loading branch information
AnatolyKalin committed Mar 4, 2024
1 parent 7a97efe commit f065b17
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 71 deletions.
2 changes: 2 additions & 0 deletions include/dxfeed_graal_cpp_api/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ DXFCXX_DISABLE_MSC_WARNINGS_PUSH(4251 4996)
#include "isolated/exceptions/JavaException.hpp"
#include "isolated/exceptions/GraalException.hpp"

#include "isolated/api/IsolatedDXEndpoint.hpp"

#include "ondemand/OnDemandService.hpp"

DXFCXX_DISABLE_MSC_WARNINGS_POP()
7 changes: 0 additions & 7 deletions include/dxfeed_graal_cpp_api/isolated/Isolated.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ namespace api {
struct DXEndpoint {
static bool close(/* dxfg_endpoint_t* */ const JavaObjectHandle<dxfcpp::DXEndpoint> &endpoint) noexcept;

// Throws:
// std::invalid_argument if endpoint handle is invalid.
// JavaException if something happened with the dxFeed API backend
// GraalException if something happened with the GraalVM
static dxfcpp::DXEndpoint::State
getState(/* dxfg_endpoint_t* */ const JavaObjectHandle<dxfcpp::DXEndpoint> &endpoint);

// Throws:
// std::invalid_argument if endpoint handle is invalid.
// JavaException if something happened with the dxFeed API backend
Expand Down
66 changes: 66 additions & 0 deletions include/dxfeed_graal_cpp_api/isolated/IsolatedCommon.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2024 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0

#pragma once

#include "../internal/Conf.hpp"

DXFCXX_DISABLE_MSC_WARNINGS_PUSH(4251)

#include "exceptions/JavaException.hpp"

struct __graal_isolatethread_t;
typedef struct __graal_isolatethread_t graal_isolatethread_t;

DXFCPP_BEGIN_NAMESPACE

namespace isolated {

inline constexpr dxfcpp::DXEndpoint::State graalStateToState(dxfg_endpoint_state_t state) noexcept {
switch (state) {
case DXFG_ENDPOINT_STATE_NOT_CONNECTED:
return dxfcpp::DXEndpoint::State::NOT_CONNECTED;
case DXFG_ENDPOINT_STATE_CONNECTING:
return dxfcpp::DXEndpoint::State::CONNECTING;
case DXFG_ENDPOINT_STATE_CONNECTED:
return dxfcpp::DXEndpoint::State::CONNECTED;
case DXFG_ENDPOINT_STATE_CLOSED:
return dxfcpp::DXEndpoint::State::CLOSED;
}

return dxfcpp::DXEndpoint::State::NOT_CONNECTED;
}

constexpr auto throwIfNullptr = [](auto v) {
return JavaException::throwIfNullptr(v);
};

constexpr auto throwIfLessThanZero = [](auto v) {
return JavaException::throwIfLessThanZero(v);
};

constexpr auto runGraalFunctionAndThrow(auto resultCheckerConverter, auto graalFunction, auto &&...params) {
return runIsolatedThrow(
[](auto threadHandle, auto &&resultCheckerConverter, auto &&graalFunction, auto &&...params) {
return resultCheckerConverter(graalFunction(static_cast<graal_isolatethread_t *>(threadHandle), params...));
},
resultCheckerConverter, graalFunction, params...);
}

constexpr auto runGraalFunctionAndThrowIfNullptr(auto graalFunction, auto &&...params) {
return runIsolatedThrow(
[](auto threadHandle, auto &&graalFunction, auto &&...params) {
return throwIfNullptr(graalFunction(static_cast<graal_isolatethread_t *>(threadHandle), params...));
},
graalFunction, params...);
}

constexpr auto runGraalFunctionAndThrowIfLessThanZero(auto graalFunction, auto &&...params) {
return runGraalFunctionAndThrow(throwIfLessThanZero, graalFunction, params...);
}

} // namespace isolated

DXFCPP_END_NAMESPACE

DXFCXX_DISABLE_MSC_WARNINGS_POP()
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ namespace IsolatedDXEndpoint {
// std::invalid_argument if endpoint handle is invalid.
// JavaException if something happened with the dxFeed API backend
// GraalException if something happened with the GraalVM
static dxfcpp::DXEndpoint::State
getState(/* dxfg_endpoint_t* */ const JavaObjectHandle<dxfcpp::DXEndpoint> &endpoint);
dxfcpp::DXEndpoint::State getState(/* dxfg_endpoint_t* */ const JavaObjectHandle<dxfcpp::DXEndpoint> &endpoint);

}
} // namespace IsolatedDXEndpoint

}
} // namespace isolated::api

DXFCPP_END_NAMESPACE

Expand Down
2 changes: 1 addition & 1 deletion src/api/DXEndpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ std::shared_ptr<DXEndpoint> DXEndpoint::create(void *endpointHandle, DXEndpoint:
}

DXEndpoint::State DXEndpoint::getState() const {
return isolated::api::DXEndpoint::getState(handle_);
return isolated::api::IsolatedDXEndpoint::getState(handle_);
}

std::shared_ptr<DXEndpoint> DXEndpoint::user(const std::string &user) {
Expand Down
55 changes: 2 additions & 53 deletions src/internal/Isolate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <dxfeed_graal_cpp_api/api.hpp>

#include <dxfeed_graal_cpp_api/isolated/IsolatedCommon.hpp>

DXFCPP_BEGIN_NAMESPACE

CEntryPointErrorsEnum Isolate::IsolateThread::detach() noexcept {
Expand Down Expand Up @@ -160,34 +162,6 @@ constexpr auto runGraalFunction(auto resultCheckerConverter, auto graalFunction,
defaultValue, resultCheckerConverter, graalFunction, params...);
}

constexpr auto throwIfNullptr = [](auto v) {
return JavaException::throwIfNullptr(v);
};

constexpr auto throwIfLessThanZero = [](auto v) {
return JavaException::throwIfLessThanZero(v);
};

constexpr auto runGraalFunctionAndThrow(auto resultCheckerConverter, auto graalFunction, auto &&...params) {
return runIsolatedThrow(
[](auto threadHandle, auto &&resultCheckerConverter, auto &&graalFunction, auto &&...params) {
return resultCheckerConverter(graalFunction(static_cast<graal_isolatethread_t *>(threadHandle), params...));
},
resultCheckerConverter, graalFunction, params...);
}

constexpr auto runGraalFunctionAndThrowIfNullptr(auto graalFunction, auto &&...params) {
return runIsolatedThrow(
[](auto threadHandle, auto &&graalFunction, auto &&...params) {
return throwIfNullptr(graalFunction(static_cast<graal_isolatethread_t *>(threadHandle), params...));
},
graalFunction, params...);
}

constexpr auto runGraalFunctionAndThrowIfLessThanZero(auto graalFunction, auto &&...params) {
return runGraalFunctionAndThrow(throwIfLessThanZero, graalFunction, params...);
}

bool String::release(const char *string) noexcept {
if (!string) {
// TODO: Improve error handling [EN-8232]
Expand Down Expand Up @@ -292,21 +266,6 @@ Tools::parseSymbols(const std::string &symbolList) noexcept {

namespace api {

dxfcpp::DXEndpoint::State graalStateToState(dxfg_endpoint_state_t state) noexcept {
switch (state) {
case DXFG_ENDPOINT_STATE_NOT_CONNECTED:
return dxfcpp::DXEndpoint::State::NOT_CONNECTED;
case DXFG_ENDPOINT_STATE_CONNECTING:
return dxfcpp::DXEndpoint::State::CONNECTING;
case DXFG_ENDPOINT_STATE_CONNECTED:
return dxfcpp::DXEndpoint::State::CONNECTED;
case DXFG_ENDPOINT_STATE_CLOSED:
return dxfcpp::DXEndpoint::State::CLOSED;
}

return dxfcpp::DXEndpoint::State::NOT_CONNECTED;
}

bool DXEndpoint::close(/* dxfg_endpoint_t* */ const JavaObjectHandle<dxfcpp::DXEndpoint> &endpoint) noexcept {
if (!endpoint) {
// TODO: Improve error handling [EN-8232]
Expand All @@ -316,16 +275,6 @@ bool DXEndpoint::close(/* dxfg_endpoint_t* */ const JavaObjectHandle<dxfcpp::DXE
return runGraalFunction(equalsToZero, dxfg_DXEndpoint_close, false, static_cast<dxfg_endpoint_t *>(endpoint.get()));
}

dxfcpp::DXEndpoint::State
DXEndpoint::getState(/* dxfg_endpoint_t* */ const JavaObjectHandle<dxfcpp::DXEndpoint> &endpoint) {
if (!endpoint) {
throw std::invalid_argument("Unable to get state. The `endpoint` handle is invalid");
}

return graalStateToState(runGraalFunctionAndThrowIfLessThanZero(dxfg_DXEndpoint_getState,
static_cast<dxfg_endpoint_t *>(endpoint.get())));
}

bool DXEndpoint::user(/* dxfg_endpoint_t* */ const JavaObjectHandle<dxfcpp::DXEndpoint> &endpoint,
const std::string &user) {
if (!endpoint) {
Expand Down
16 changes: 10 additions & 6 deletions src/isolated/api/IsolatedDXEndpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@

#include <dxfeed_graal_cpp_api/api.hpp>

#include <dxfeed_graal_cpp_api/isolated/IsolatedCommon.hpp>

#include <fmt/format.h>

DXFCPP_BEGIN_NAMESPACE

namespace isolated::api {

namespace IsolatedDXEndpoint {
namespace isolated::api::IsolatedDXEndpoint {
dxfcpp::DXEndpoint::State getState(/* dxfg_endpoint_t* */ const JavaObjectHandle<dxfcpp::DXEndpoint> &endpoint) {
if (!endpoint) {
throw std::invalid_argument("Unable to get state. The `endpoint` handle is invalid");
}

return graalStateToState(runGraalFunctionAndThrowIfLessThanZero(dxfg_DXEndpoint_getState,
static_cast<dxfg_endpoint_t *>(endpoint.get())));
}

}

} // namespace isolated::api::IsolatedDXEndpoint

DXFCPP_END_NAMESPACE

0 comments on commit f065b17

Please sign in to comment.