Skip to content

Commit

Permalink
[MDAPI-128] [C++] Split isolated (with GraalVM) entities into multipl…
Browse files Browse the repository at this point in the history
…e translation units.

Normalize the CandleAlignment class structure.
  • Loading branch information
ttldtor committed Sep 20, 2024
1 parent 67ce5ab commit 3f1278b
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 77 deletions.
72 changes: 8 additions & 64 deletions include/dxfeed_graal_cpp_api/event/candle/CandleAlignment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

DXFCXX_DISABLE_MSC_WARNINGS_PUSH(4251)

#include "../../internal/utils/StringUtils.hpp"
#include "../../exceptions/InvalidArgumentException.hpp"
#include "../market/MarketEventSymbols.hpp"
#include "CandleSymbolAttribute.hpp"

#include <string>
Expand Down Expand Up @@ -58,13 +55,13 @@ struct DXFCPP_EXPORT CandleAlignment : public CandleSymbolAttribute {
static const std::unordered_map<std::string, std::reference_wrapper<const CandleAlignment>, dxfcpp::StringHash,
std::equal_to<>>
BY_STRING;

static const std::vector<std::reference_wrapper<const CandleAlignment>> VALUES;

private:
std::string string_{};

explicit CandleAlignment(const dxfcpp::StringLikeWrapper &string) noexcept : string_{string} {
}
explicit CandleAlignment(const dxfcpp::StringLikeWrapper &string) noexcept;

public:
CandleAlignment() noexcept = default;
Expand All @@ -76,10 +73,7 @@ struct DXFCPP_EXPORT CandleAlignment : public CandleSymbolAttribute {
* @param symbol The original candle event symbol.
* @return candle event symbol string with this candle alignment set.
*/
std::string changeAttributeForSymbol(const dxfcpp::StringLikeWrapper &symbol) const override {
return *this == DEFAULT ? MarketEventSymbols::removeAttributeStringByKey(symbol, ATTRIBUTE_KEY)
: MarketEventSymbols::changeAttributeStringByKey(symbol, ATTRIBUTE_KEY, toString());
}
std::string changeAttributeForSymbol(const dxfcpp::StringLikeWrapper &symbol) const override;

/**
* Returns string representation of this candle alignment.
Expand All @@ -88,13 +82,9 @@ struct DXFCPP_EXPORT CandleAlignment : public CandleSymbolAttribute {
*
* @return string representation of this candle alignment.
*/
std::string toString() const {
return string_;
}
std::string toString() const;

bool operator==(const CandleAlignment &candleAlignment) const noexcept {
return string_ == candleAlignment.string_;
}
bool operator==(const CandleAlignment &candleAlignment) const noexcept;

/**
* Parses string representation of candle alignment into object.
Expand All @@ -104,23 +94,7 @@ struct DXFCPP_EXPORT CandleAlignment : public CandleSymbolAttribute {
* @return The candle alignment (reference)
* @throws InvalidArgumentException if the string representation is invalid.
*/
static std::reference_wrapper<const CandleAlignment> parse(const dxfcpp::StringLikeWrapper &s) {
auto found = BY_STRING.find(s);

if (found != BY_STRING.end()) {
return found->second;
}

for (const auto &alignmentRef : VALUES) {
const auto &alignmentStr = alignmentRef.get().toString();

if (iEquals(alignmentStr, s)) {
return alignmentRef;
}
}

throw InvalidArgumentException("Unknown candle alignment: " + s);
}
static std::reference_wrapper<const CandleAlignment> parse(const dxfcpp::StringLikeWrapper &s);

/**
* Returns candle alignment of the given candle symbol string.
Expand All @@ -129,45 +103,15 @@ struct DXFCPP_EXPORT CandleAlignment : public CandleSymbolAttribute {
* @param symbol The candle symbol string.
* @return candle alignment of the given candle symbol string.
*/
static std::reference_wrapper<const CandleAlignment>
getAttributeForSymbol(const dxfcpp::StringLikeWrapper &symbol) {
auto stringOpt = MarketEventSymbols::getAttributeStringByKey(symbol, ATTRIBUTE_KEY);

return !stringOpt ? std::cref(DEFAULT) : parse(stringOpt.value());
}
static std::reference_wrapper<const CandleAlignment> getAttributeForSymbol(const dxfcpp::StringLikeWrapper &symbol);

/**
* Returns candle symbol string with the normalized representation of the candle alignment attribute.
*
* @param symbol The candle symbol string.
* @return candle symbol string with the normalized representation of the the candle alignment attribute.
*/
static std::string
normalizeAttributeForSymbol(const dxfcpp::StringLikeWrapper &symbol) {
auto a = MarketEventSymbols::getAttributeStringByKey(symbol, ATTRIBUTE_KEY);

if (!a) {
return symbol;
}

try {
auto other = parse(a.value());

if (other.get() == DEFAULT) {
return MarketEventSymbols::removeAttributeStringByKey(symbol, ATTRIBUTE_KEY);
}

if (a.value() != other.get().toString()) {
return MarketEventSymbols::changeAttributeStringByKey(symbol, ATTRIBUTE_KEY, other.get().toString());
}

return symbol;
} catch (const InvalidArgumentException &) {
return symbol;
} catch (const std::invalid_argument &) {
return symbol;
}
}
static std::string normalizeAttributeForSymbol(const dxfcpp::StringLikeWrapper &symbol);
};

DXFCPP_END_NAMESPACE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ DXFCXX_DISABLE_MSC_WARNINGS_PUSH(4251)

DXFCPP_BEGIN_NAMESPACE

struct StringLikeWrapper;

/**
* Attribute of the CandleSymbol.
*/
Expand Down
5 changes: 3 additions & 2 deletions include/dxfeed_graal_cpp_api/internal/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@ struct StringLikeWrapper {
}
};

/// Universal functional object that allows searching std::unordered_map for string-like keys.
struct StringHash {
using HashType = std::hash<std::string_view>;
using is_transparent = void;
Expand All @@ -783,8 +784,8 @@ struct StringHash {
return HashType{}(str);
}

std::size_t operator()(std::string_view str) const {
return HashType{}(str);
std::size_t operator()(std::string_view sw) const {
return HashType{}(sw);
}

std::size_t operator()(std::string const &str) const {
Expand Down
15 changes: 7 additions & 8 deletions src/event/candle/Candle.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
// Copyright (c) 2024 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0

#include <dxfg_api.h>
#include <dxfeed_graal_cpp_api/event/candle/Candle.hpp>

#include <dxfeed_graal_cpp_api/internal/TimeFormat.hpp>
#include <dxfeed_graal_cpp_api/exceptions/InvalidArgumentException.hpp>
#include <dxfeed_graal_cpp_api/event/EventTypeEnum.hpp>
#include <dxfeed_graal_cpp_api/internal/utils/debug/Debug.hpp>

#include <dxfg_api.h>
#include <dxfeed_graal_c_api/api.h>

#include <memory>
#include <utility>
#include <cassert>

#include <dxfeed_graal_cpp_api/internal/TimeFormat.hpp>

#include <dxfeed_graal_cpp_api/exceptions/InvalidArgumentException.hpp>

#include <dxfeed_graal_cpp_api/event/EventTypeEnum.hpp>
#include <dxfeed_graal_cpp_api/event/candle/Candle.hpp>

#include <fmt/format.h>

DXFCPP_BEGIN_NAMESPACE
Expand Down
95 changes: 92 additions & 3 deletions src/event/candle/CandleAlignment.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright (c) 2024 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0

#include <dxfg_api.h>
#include <dxfeed_graal_cpp_api/event/candle/CandleAlignment.hpp>

#include <dxfeed_graal_c_api/api.h>
#include <dxfeed_graal_cpp_api/api.hpp>
#include <dxfeed_graal_cpp_api/event/market/MarketEventSymbols.hpp>
#include <dxfeed_graal_cpp_api/exceptions/InvalidArgumentException.hpp>
#include <dxfeed_graal_cpp_api/internal/utils/StringUtils.hpp>

DXFCPP_BEGIN_NAMESPACE

Expand All @@ -25,4 +26,92 @@ const std::vector<std::reference_wrapper<const CandleAlignment>> CandleAlignment
std::cref(CandleAlignment::SESSION),
};

CandleAlignment::CandleAlignment(const dxfcpp::StringLikeWrapper &string) noexcept : string_{string} {
}

std::string CandleAlignment::changeAttributeForSymbol(const dxfcpp::StringLikeWrapper &symbol) const {
return *this == DEFAULT ? MarketEventSymbols::removeAttributeStringByKey(symbol, ATTRIBUTE_KEY)
: MarketEventSymbols::changeAttributeStringByKey(symbol, ATTRIBUTE_KEY, toString());
}

std::string CandleAlignment::toString() const {
return string_;
}

bool CandleAlignment::operator==(const CandleAlignment &candleAlignment) const noexcept {
return string_ == candleAlignment.string_;
}

/**
* Parses string representation of candle alignment into object.
* Any string that was returned by CandleAlignment::toString() can be parsed and case is ignored for parsing.
*
* @param s The string representation of candle alignment.
* @return The candle alignment (reference)
* @throws InvalidArgumentException if the string representation is invalid.
*/
std::reference_wrapper<const CandleAlignment> CandleAlignment::parse(const dxfcpp::StringLikeWrapper &s) {
auto found = BY_STRING.find(s);

if (found != BY_STRING.end()) {
return found->second;
}

for (const auto &alignmentRef : VALUES) {
const auto &alignmentStr = alignmentRef.get().toString();

if (iEquals(alignmentStr, s)) {
return alignmentRef;
}
}

throw InvalidArgumentException("Unknown candle alignment: " + s);
}

/**
* Returns candle alignment of the given candle symbol string.
* The result is CandleAlignment::DEFAULT if the symbol does not have candle alignment attribute.
*
* @param symbol The candle symbol string.
* @return candle alignment of the given candle symbol string.
*/
std::reference_wrapper<const CandleAlignment>
CandleAlignment::getAttributeForSymbol(const dxfcpp::StringLikeWrapper &symbol) {
auto stringOpt = MarketEventSymbols::getAttributeStringByKey(symbol, ATTRIBUTE_KEY);

return !stringOpt ? std::cref(DEFAULT) : parse(stringOpt.value());
}

/**
* Returns candle symbol string with the normalized representation of the candle alignment attribute.
*
* @param symbol The candle symbol string.
* @return candle symbol string with the normalized representation of the the candle alignment attribute.
*/
std::string CandleAlignment::normalizeAttributeForSymbol(const dxfcpp::StringLikeWrapper &symbol) {
auto a = MarketEventSymbols::getAttributeStringByKey(symbol, ATTRIBUTE_KEY);

if (!a) {
return symbol;
}

try {
auto other = parse(a.value());

if (other.get() == DEFAULT) {
return MarketEventSymbols::removeAttributeStringByKey(symbol, ATTRIBUTE_KEY);
}

if (a.value() != other.get().toString()) {
return MarketEventSymbols::changeAttributeStringByKey(symbol, ATTRIBUTE_KEY, other.get().toString());
}

return symbol;
} catch (const InvalidArgumentException &) {
return symbol;
} catch (const std::invalid_argument &) {
return symbol;
}
}

DXFCPP_END_NAMESPACE

0 comments on commit 3f1278b

Please sign in to comment.