diff options
Diffstat (limited to 'protocols/Telegram/tdlib/td/tdutils/td/utils/port/CxCli.h')
-rw-r--r-- | protocols/Telegram/tdlib/td/tdutils/td/utils/port/CxCli.h | 65 |
1 files changed, 47 insertions, 18 deletions
diff --git a/protocols/Telegram/tdlib/td/tdutils/td/utils/port/CxCli.h b/protocols/Telegram/tdlib/td/tdutils/td/utils/port/CxCli.h index b7f01fa401..f88ef5bad8 100644 --- a/protocols/Telegram/tdlib/td/tdutils/td/utils/port/CxCli.h +++ b/protocols/Telegram/tdlib/td/tdutils/td/utils/port/CxCli.h @@ -1,28 +1,37 @@ // -// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018 +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022 // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #pragma once +#pragma managed(push, off) #include "td/utils/port/config.h" +#pragma managed(pop) #include "td/utils/common.h" -#undef small #if TD_WINRT +#pragma managed(push, off) #include "td/utils/port/wstring_convert.h" +#pragma managed(pop) #include "collection.h" +#pragma managed(push, off) #include <cstdint> #include <map> #include <mutex> +#pragma managed(pop) + +#undef small #define REF_NEW ref new #define CLRCALL +#define DEPRECATED_ATTRIBUTE(message) \ + ::Windows::Foundation::Metadata::Deprecated(message, ::Windows::Foundation::Metadata::DeprecationType::Deprecate, 0x0) namespace CxCli { @@ -38,8 +47,9 @@ using Platform::String; using Platform::NullReferenceException; -template <class Key, class Value> class ConcurrentDictionary { -public: +template <class Key, class Value> +class ConcurrentDictionary { + public: bool TryGetValue(Key key, Value &value) { std::lock_guard<std::mutex> guard(mutex_); auto it = impl_.find(key); @@ -59,11 +69,12 @@ public: impl_.erase(it); return true; } - Value &operator [] (Key key) { + Value &operator[](Key key) { std::lock_guard<std::mutex> guard(mutex_); return impl_[key]; } -private: + + private: std::mutex mutex_; std::map<Key, Value> impl_; }; @@ -72,26 +83,31 @@ inline std::int64_t Increment(volatile std::int64_t &value) { return InterlockedIncrement64(&value); } -inline std::string string_to_unmanaged(String^ str) { +inline std::string string_to_unmanaged(String ^ str) { if (!str) { return std::string(); } - return td::from_wstring(str->Data(), str->Length()).ok(); + auto r_unmanaged_str = td::from_wstring(str->Data(), str->Length()); + if (r_unmanaged_str.is_error()) { + return std::string(); + } + return r_unmanaged_str.move_as_ok(); } -inline String^ string_from_unmanaged(const std::string &from) { +inline String ^ string_from_unmanaged(const std::string &from) { auto tmp = td::to_wstring(from).ok(); return REF_NEW String(tmp.c_str(), static_cast<unsigned>(tmp.size())); } -} // namespace CxCli +} // namespace CxCli #elif TD_CLI -#include <msclr\marshal_cppstd.h> +#undef small #define REF_NEW gcnew #define CLRCALL __clrcall +#define DEPRECATED_ATTRIBUTE(message) System::ObsoleteAttribute(message) namespace CxCli { @@ -113,21 +129,34 @@ using System::NullReferenceException; using System::Collections::Concurrent::ConcurrentDictionary; -inline std::int64_t Increment(std::int64_t %value) { +inline std::int64_t Increment(std::int64_t % value) { return System::Threading::Interlocked::Increment(value); } -inline std::string string_to_unmanaged(String^ str) { - if (!str) { +inline std::string string_to_unmanaged(String ^ str) { + if (!str || str->Length == 0) { return std::string(); } - return msclr::interop::marshal_as<std::string>(str); + + Array<System::Byte> ^ bytes = System::Text::Encoding::UTF8->GetBytes(str); + cli::pin_ptr<System::Byte> pinned_ptr = &bytes[0]; + std::string result(reinterpret_cast<const char *>(&pinned_ptr[0]), bytes->Length); + return result; } -inline String^ string_from_unmanaged(const std::string &from) { - return msclr::interop::marshal_as<String^>(from); +inline String ^ string_from_unmanaged(const std::string &from) { + if (from.empty()) { + return String::Empty; + } + + Array<System::Byte> ^ bytes = REF_NEW Vector<System::Byte>(static_cast<ArrayIndexType>(from.size())); + cli::pin_ptr<System::Byte> pinned_ptr = &bytes[0]; + for (size_t i = 0; i < from.size(); ++i) { + pinned_ptr[i] = from[i]; + } + return System::Text::Encoding::UTF8->GetString(bytes); } -} // namespace CxCli +} // namespace CxCli #endif |