diff --git a/launchers/macosx/include/PidWatcher.h b/launchers/macosx/include/PidWatcher.h index 8ae10e09f..ed738686d 100644 --- a/launchers/macosx/include/PidWatcher.h +++ b/launchers/macosx/include/PidWatcher.h @@ -8,7 +8,7 @@ using callbackType = void (CFFileDescriptorRef, CFOptionFlags, void *); using HandleFunction = std::function; -static void noteProcDeath(CFFileDescriptorRef fdref, CFOptionFlags callBackTypes, void *info) { +static inline void noteProcDeath(CFFileDescriptorRef fdref, CFOptionFlags callBackTypes, void *info) { struct kevent kev; int fd = CFFileDescriptorGetNativeDescriptor(fdref); kevent(fd, NULL, 0, &kev, 1, NULL); diff --git a/launchers/macosx/include/sharedqueue.h b/launchers/macosx/include/sharedqueue.h new file mode 100644 index 000000000..34a66acc7 --- /dev/null +++ b/launchers/macosx/include/sharedqueue.h @@ -0,0 +1,65 @@ +#ifndef SHAREDQUEUE_H__ +#define SHAREDQUEUE_H__ + +#include +#include +#include +#include + +/** Multiple producer, multiple consumer thread safe queue + * Since 'return by reference' is used this queue won't throw */ +template +class shared_queue +{ + std::queue queue_; + mutable std::mutex m_; + std::condition_variable data_cond_; + + shared_queue& operator=(const shared_queue&); + shared_queue(const shared_queue& other); + +public: + shared_queue(){} + + void push(T item){ + { + std::lock_guard lock(m_); + queue_.push(item); + } + data_cond_.notify_one(); + } + + /// \return immediately, with true if successful retrieval + bool try_and_pop(T& popped_item){ + std::lock_guard lock(m_); + if(queue_.empty()){ + return false; + } + popped_item=std::move(queue_.front()); + queue_.pop(); + return true; + } + + /// Try to retrieve, if no items, wait till an item is available and try again + void wait_and_pop(T& popped_item){ + std::unique_lock lock(m_); // note: unique_lock is needed for std::condition_variable::wait + while(queue_.empty()) + { // The 'while' loop below is equal to + data_cond_.wait(lock); //data_cond_.wait(lock, [](bool result){return !queue_.empty();}); + } + popped_item=std::move(queue_.front()); + queue_.pop(); + } + + bool empty() const{ + std::lock_guard lock(m_); + return queue_.empty(); + } + + unsigned size() const{ + std::lock_guard lock(m_); + return queue_.size(); + } +}; + +#endif // SHAREDQUEUE_H__ diff --git a/launchers/macosx/include/strutil.hpp b/launchers/macosx/include/strutil.hpp index ab52a1c01..085464fad 100644 --- a/launchers/macosx/include/strutil.hpp +++ b/launchers/macosx/include/strutil.hpp @@ -10,6 +10,8 @@ #include #include "optional.h" +#import + #include #include #include @@ -98,6 +100,15 @@ static inline std::string trim_copy(std::string s) { return s; } +NSString* stdStringToNSString(std::string &stdstr) { + return [NSString stringWithUTF8String:stdstr.c_str()]; +} + +std::string nsStringToStd(NSString* nsStr) { + const char *charlist = [nsStr UTF8String]; + return std::string(charlist); +} + #if __cplusplus > 201402L diff --git a/launchers/macosx/include/subprocess.hpp b/launchers/macosx/include/subprocess.hpp index 7cb78734d..70b7af14e 100644 --- a/launchers/macosx/include/subprocess.hpp +++ b/launchers/macosx/include/subprocess.hpp @@ -34,6 +34,8 @@ Documentation for C++ subprocessing libraray. #ifndef SUBPROCESS_HPP #define SUBPROCESS_HPP +#ifdef __cplusplus + #include #include #include @@ -697,19 +699,19 @@ template struct param_pack{}; template struct has_type; template -struct has_type> { +struct has_type > { static constexpr bool value = false; }; template -struct has_type> { +struct has_type > { static constexpr bool value = true; }; template -struct has_type> { +struct has_type > { static constexpr bool value = - std::is_same::type>::value ? true : has_type>::value; + std::is_same::type>::value ? true : has_type >::value; }; //---- @@ -1528,7 +1530,7 @@ namespace detail template OutBuffer check_output_impl(F& farg, Args&&... args) { - static_assert(!detail::has_type>::value, "output not allowed in args"); + static_assert(!detail::has_type >::value, "output not allowed in args"); auto p = Popen(std::forward(farg), std::forward(args)..., output{PIPE}); auto res = p.communicate(); auto retcode = p.poll(); @@ -1627,4 +1629,6 @@ OutBuffer pipeline(Args&&... args) }; +#endif // __cplusplus + #endif // SUBPROCESS_HPP