Update CxCli.h.
GitOrigin-RevId: 4b9fb613969a0eebca655168662d21c760778079
This commit is contained in:
parent
f6fb3c0cd0
commit
57a7d7506f
@ -18,21 +18,28 @@
|
|||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <queue>
|
|
||||||
|
|
||||||
#define REF_NEW ref new
|
#define REF_NEW ref new
|
||||||
#define CLRCALL
|
#define CLRCALL
|
||||||
#define CXCONST
|
#define CXCONST
|
||||||
|
|
||||||
namespace CxCli {
|
namespace CxCli {
|
||||||
using Platform::String;
|
|
||||||
using Windows::Foundation::Collections::IVector;
|
using Windows::Foundation::Collections::IVector;
|
||||||
#define Array IVector
|
#define Array IVector
|
||||||
using Platform::Collections::Vector;
|
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:
|
public:
|
||||||
bool TryGetValue(Key key, Value &value) {
|
bool TryGetValue(Key key, Value &value) {
|
||||||
|
std::lock_guard<std::mutex> guard(mutex_);
|
||||||
auto it = impl_.find(key);
|
auto it = impl_.find(key);
|
||||||
if (it == impl_.end()) {
|
if (it == impl_.end()) {
|
||||||
return false;
|
return false;
|
||||||
@ -41,70 +48,70 @@ public:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
void Remove(Key value) {
|
void Remove(Key value) {
|
||||||
|
std::lock_guard<std::mutex> guard(mutex_);
|
||||||
impl_.erase(value);
|
impl_.erase(value);
|
||||||
}
|
}
|
||||||
Value &operator [] (Key key) {
|
Value &operator [] (Key key) {
|
||||||
|
std::lock_guard<std::mutex> guard(mutex_);
|
||||||
return impl_[key];
|
return impl_[key];
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
|
std::mutex mutex_;
|
||||||
std::map<Key, Value> impl_;
|
std::map<Key, Value> impl_;
|
||||||
};
|
};
|
||||||
template <class Value> class ConcurrentQueue {
|
|
||||||
public:
|
inline std::int64_t Increment(volatile std::int64_t &value) {
|
||||||
void Enqueue(Value value) {
|
return InterlockedIncrement64(&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::string string_to_unmanaged(String^ string) {
|
inline std::string string_to_unmanaged(String^ string) {
|
||||||
return td::from_wstring(string->Data(), string->Length()).ok();
|
return td::from_wstring(string->Data(), string->Length()).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();
|
auto tmp = td::to_wstring(from).ok();
|
||||||
return REF_NEW String(tmp.c_str(), td::narrow_cast<unsigned>(tmp.size()));
|
return REF_NEW String(tmp.c_str(), td::narrow_cast<unsigned>(tmp.size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace CxCli
|
} // namespace CxCli
|
||||||
|
|
||||||
#elif TD_CLI
|
#elif TD_CLI
|
||||||
#include <msclr\lock.h>
|
|
||||||
#include <msclr\marshal_cppstd.h>
|
#include <msclr\marshal_cppstd.h>
|
||||||
|
|
||||||
#define REF_NEW gcnew
|
#define REF_NEW gcnew
|
||||||
#define CLRCALL __clrcall
|
#define CLRCALL __clrcall
|
||||||
#define CXCONST
|
#define CXCONST
|
||||||
|
|
||||||
#define Array array
|
|
||||||
#define Vector array
|
|
||||||
|
|
||||||
namespace CxCli {
|
namespace CxCli {
|
||||||
|
|
||||||
using uint8 = td::uint8;
|
using uint8 = td::uint8;
|
||||||
using int32 = td::int32;
|
using int32 = td::int32;
|
||||||
using int64 = td::int64;
|
using int64 = td::int64;
|
||||||
using float64 = double;
|
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::String;
|
||||||
using System::Collections::Concurrent::ConcurrentQueue;
|
|
||||||
using System::Collections::Generic::Dictionary;
|
using System::NullReferenceException;
|
||||||
using msclr::lock;
|
|
||||||
using msclr::interop::marshal_as;
|
using System::Collections::Concurrent::ConcurrentDictionary;
|
||||||
|
|
||||||
|
using System::Threading::Interlocked::Increment;
|
||||||
|
|
||||||
inline std::string string_to_unmanaged(String^ string) {
|
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) {
|
inline String^ string_from_unmanaged(const std::string &from) {
|
||||||
return marshal_as<String^>(from);
|
return msclr::interop::marshal_as<String^>(from);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace CxCli
|
} // namespace CxCli
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user