Update CxCli.h.

GitOrigin-RevId: 4b9fb613969a0eebca655168662d21c760778079
This commit is contained in:
levlam 2018-01-11 15:28:27 +03:00
parent f6fb3c0cd0
commit 57a7d7506f

View File

@ -18,21 +18,28 @@
#include <map>
#include <mutex>
#include <queue>
#define REF_NEW ref new
#define CLRCALL
#define CXCONST
namespace CxCli {
using Platform::String;
using Windows::Foundation::Collections::IVector;
#define Array IVector
using Platform::Collections::Vector;
#define ArraySize(arr) ((arr)->Size)
#define ArrayGet(arr, index) ((arr)->GetAt(index))
#define ArraySet(arr, index, value) ((arr)->SetAt((index), (value)))
template <class Key, class Value> class Dictionary {
using Platform::String;
using Platform::NullReferenceException;
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);
if (it == impl_.end()) {
return false;
@ -41,70 +48,70 @@ public:
return true;
}
void Remove(Key value) {
std::lock_guard<std::mutex> guard(mutex_);
impl_.erase(value);
}
Value &operator [] (Key key) {
std::lock_guard<std::mutex> guard(mutex_);
return impl_[key];
}
private:
std::mutex mutex_;
std::map<Key, Value> impl_;
};
template <class Value> class ConcurrentQueue {
public:
void Enqueue(Value value) {
std::lock_guard<std::mutex> lock(mutex);
queue.push(value);
}
bool TryDequeue(Value &value) {
std::lock_guard<std::mutex> lock(mutex);
if (queue.empty()) {
return false;
}
value = queue.front();
queue.pop();
return true;
}
private:
std::mutex mutex;
std::queue<Value> queue;
};
inline std::int64_t Increment(volatile std::int64_t &value) {
return InterlockedIncrement64(&value);
}
inline std::string string_to_unmanaged(String^ string) {
return td::from_wstring(string->Data(), string->Length()).ok();
}
inline String^ string_from_unmanaged(const std::string &from) {
auto tmp = td::to_wstring(from).ok();
return REF_NEW String(tmp.c_str(), td::narrow_cast<unsigned>(tmp.size()));
}
} // namespace CxCli
#elif TD_CLI
#include <msclr\lock.h>
#include <msclr\marshal_cppstd.h>
#define REF_NEW gcnew
#define CLRCALL __clrcall
#define CXCONST
#define Array array
#define Vector array
namespace CxCli {
using uint8 = td::uint8;
using int32 = td::int32;
using int64 = td::int64;
using float64 = double;
#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))
using System::String;
using System::Collections::Concurrent::ConcurrentQueue;
using System::Collections::Generic::Dictionary;
using msclr::lock;
using msclr::interop::marshal_as;
using System::NullReferenceException;
using System::Collections::Concurrent::ConcurrentDictionary;
using System::Threading::Interlocked::Increment;
inline std::string string_to_unmanaged(String^ string) {
return marshal_as<std::string>(string);
return msclr::interop::marshal_as<std::string>(string);
}
inline String^ string_from_unmanaged(const std::string &from) {
return marshal_as<String^>(from);
return msclr::interop::marshal_as<String^>(from);
}
} // namespace CxCli
#endif