2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2020-06-06 21:51:45 +02:00
|
|
|
#pragma managed(push, off)
|
2018-02-26 19:08:47 +01:00
|
|
|
#include "td/utils/port/config.h"
|
2020-06-06 21:51:45 +02:00
|
|
|
#pragma managed(pop)
|
2018-02-28 02:22:17 +01:00
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#if TD_WINRT
|
|
|
|
|
2020-06-06 21:51:45 +02:00
|
|
|
#pragma managed(push, off)
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/wstring_convert.h"
|
2020-06-06 21:51:45 +02:00
|
|
|
#pragma managed(pop)
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "collection.h"
|
|
|
|
|
2020-06-06 21:51:45 +02:00
|
|
|
#pragma managed(push, off)
|
2018-01-28 19:13:25 +01:00
|
|
|
#include <cstdint>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <map>
|
|
|
|
#include <mutex>
|
2020-06-06 21:51:45 +02:00
|
|
|
#pragma managed(pop)
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-03-31 23:36:01 +02:00
|
|
|
#undef small
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#define REF_NEW ref new
|
|
|
|
#define CLRCALL
|
2019-02-24 17:17:36 +01:00
|
|
|
#define DEPRECATED_ATTRIBUTE(message) ::Windows::Foundation::Metadata::Deprecated(message,\
|
|
|
|
::Windows::Foundation::Metadata::DeprecationType::Deprecate, 0x0)
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace CxCli {
|
2018-01-11 13:28:27 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
using Windows::Foundation::Collections::IVector;
|
|
|
|
#define Array IVector
|
|
|
|
using Platform::Collections::Vector;
|
2018-01-11 13:28:27 +01:00
|
|
|
#define ArraySize(arr) ((arr)->Size)
|
|
|
|
#define ArrayGet(arr, index) ((arr)->GetAt(index))
|
|
|
|
#define ArraySet(arr, index, value) ((arr)->SetAt((index), (value)))
|
2018-01-11 13:48:03 +01:00
|
|
|
#define ArrayIndexType unsigned
|
2018-01-11 13:28:27 +01:00
|
|
|
|
|
|
|
using Platform::String;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-01-11 13:28:27 +01:00
|
|
|
using Platform::NullReferenceException;
|
|
|
|
|
|
|
|
template <class Key, class Value> class ConcurrentDictionary {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
bool TryGetValue(Key key, Value &value) {
|
2018-01-11 13:28:27 +01:00
|
|
|
std::lock_guard<std::mutex> guard(mutex_);
|
2018-12-31 20:04:05 +01:00
|
|
|
auto it = impl_.find(key);
|
|
|
|
if (it == impl_.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
value = it->second;
|
|
|
|
return true;
|
|
|
|
}
|
2018-02-22 21:34:09 +01:00
|
|
|
bool TryRemove(Key key, Value &value) {
|
2018-01-11 13:28:27 +01:00
|
|
|
std::lock_guard<std::mutex> guard(mutex_);
|
2018-02-22 21:34:09 +01:00
|
|
|
auto it = impl_.find(key);
|
|
|
|
if (it == impl_.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
value = std::move(it->second);
|
|
|
|
impl_.erase(it);
|
|
|
|
return true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
Value &operator [] (Key key) {
|
2018-01-11 13:28:27 +01:00
|
|
|
std::lock_guard<std::mutex> guard(mutex_);
|
2018-12-31 20:04:05 +01:00
|
|
|
return impl_[key];
|
|
|
|
}
|
|
|
|
private:
|
2018-01-11 13:28:27 +01:00
|
|
|
std::mutex mutex_;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::map<Key, Value> impl_;
|
|
|
|
};
|
2018-01-11 13:28:27 +01:00
|
|
|
|
|
|
|
inline std::int64_t Increment(volatile std::int64_t &value) {
|
|
|
|
return InterlockedIncrement64(&value);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-03-11 21:49:38 +01:00
|
|
|
inline std::string string_to_unmanaged(String^ str) {
|
|
|
|
if (!str) {
|
|
|
|
return std::string();
|
|
|
|
}
|
2021-04-08 13:43:47 +02:00
|
|
|
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();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-11 13:28:27 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
inline String^ string_from_unmanaged(const std::string &from) {
|
|
|
|
auto tmp = td::to_wstring(from).ok();
|
2018-03-15 17:25:51 +01:00
|
|
|
return REF_NEW String(tmp.c_str(), static_cast<unsigned>(tmp.size()));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-11 13:28:27 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace CxCli
|
2018-01-11 13:28:27 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#elif TD_CLI
|
2018-01-11 13:28:27 +01:00
|
|
|
|
2020-03-31 23:36:01 +02:00
|
|
|
#undef small
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#define REF_NEW gcnew
|
|
|
|
#define CLRCALL __clrcall
|
2019-02-24 17:17:36 +01:00
|
|
|
#define DEPRECATED_ATTRIBUTE(message) System::ObsoleteAttribute(message)
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace CxCli {
|
2018-01-11 13:28:27 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
using uint8 = td::uint8;
|
|
|
|
using int32 = td::int32;
|
|
|
|
using int64 = td::int64;
|
|
|
|
using float64 = double;
|
|
|
|
|
2018-01-11 13:28:27 +01:00
|
|
|
#define Array array
|
|
|
|
#define Vector array
|
|
|
|
#define ArraySize(arr) ((arr)->Length)
|
|
|
|
#define ArrayGet(arr, index) ((arr)[index])
|
|
|
|
#define ArraySet(arr, index, value) ((arr)[index] = (value))
|
2018-01-11 13:48:03 +01:00
|
|
|
#define ArrayIndexType int
|
2018-01-11 13:28:27 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
using System::String;
|
2018-01-11 13:28:27 +01:00
|
|
|
|
|
|
|
using System::NullReferenceException;
|
|
|
|
|
|
|
|
using System::Collections::Concurrent::ConcurrentDictionary;
|
|
|
|
|
2018-02-16 12:22:19 +01:00
|
|
|
inline std::int64_t Increment(std::int64_t %value) {
|
|
|
|
return System::Threading::Interlocked::Increment(value);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-03-11 21:49:38 +01:00
|
|
|
inline std::string string_to_unmanaged(String^ str) {
|
2018-05-11 18:53:27 +02:00
|
|
|
if (!str || str->Length == 0) {
|
2018-03-11 21:49:38 +01:00
|
|
|
return std::string();
|
|
|
|
}
|
2018-05-11 18:53:27 +02:00
|
|
|
|
|
|
|
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;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline String^ string_from_unmanaged(const std::string &from) {
|
2018-05-11 18:53:27 +02:00
|
|
|
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);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-11 13:28:27 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace CxCli
|
2018-01-11 13:28:27 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif
|