2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-01-02 14:42:31 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
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
|
|
|
|
|
2018-02-26 19:08:47 +01:00
|
|
|
#include "td/utils/port/config.h"
|
2018-02-28 02:22:17 +01:00
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#undef small
|
|
|
|
|
|
|
|
#if TD_WINRT
|
|
|
|
|
|
|
|
#include "td/utils/port/wstring_convert.h"
|
|
|
|
|
|
|
|
#include "collection.h"
|
|
|
|
|
2018-01-28 19:13:25 +01:00
|
|
|
#include <cstdint>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <map>
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
#define REF_NEW ref new
|
|
|
|
#define CLRCALL
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
return td::from_wstring(str->Data(), str->Length()).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
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <msclr\marshal_cppstd.h>
|
2018-01-11 13:28:27 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#define REF_NEW gcnew
|
|
|
|
#define CLRCALL __clrcall
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if (!str) {
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
return msclr::interop::marshal_as<std::string>(str);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline String^ string_from_unmanaged(const std::string &from) {
|
2018-01-11 13:28:27 +01:00
|
|
|
return msclr::interop::marshal_as<String^>(from);
|
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
|