Tesseract
3.02
|
00001 00002 // File: svutil.h 00003 // Description: ScrollView Utilities 00004 // Author: Joern Wanke 00005 // Created: Thu Nov 29 2007 00006 // 00007 // (C) Copyright 2007, Google Inc. 00008 // Licensed under the Apache License, Version 2.0 (the "License"); 00009 // you may not use this file except in compliance with the License. 00010 // You may obtain a copy of the License at 00011 // http://www.apache.org/licenses/LICENSE-2.0 00012 // Unless required by applicable law or agreed to in writing, software 00013 // distributed under the License is distributed on an "AS IS" BASIS, 00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 // See the License for the specific language governing permissions and 00016 // limitations under the License. 00017 // 00019 // 00020 // SVUtil contains the SVSync, SVSemaphore, SVMutex and SVNetwork 00021 // classes, which are used for thread/process creation & synchronization 00022 // and network connection. 00023 00024 #ifndef TESSERACT_VIEWER_SVUTIL_H__ 00025 #define TESSERACT_VIEWER_SVUTIL_H__ 00026 00027 #ifdef _WIN32 00028 #ifndef __GNUC__ 00029 #include <windows.h> 00030 #define snprintf _snprintf 00031 #if (_MSC_VER <= 1400) 00032 #define vsnprintf _vsnprintf 00033 #endif 00034 #pragma warning(disable:4786) 00035 #else 00036 #include "platform.h" 00037 #include <windows.h> 00038 #endif 00039 #else 00040 #include <pthread.h> 00041 #include <semaphore.h> 00042 #endif 00043 00044 #include <string> 00045 00046 #ifndef MAX 00047 #define MAX(a, b) ((a > b) ? a : b) 00048 #endif 00049 00050 #ifndef MIN 00051 #define MIN(a, b) ((a < b) ? a : b) 00052 #endif 00053 00055 class SVSync { 00056 public: 00058 static void StartThread(void *(*func)(void*), void* arg); 00060 static void ExitThread(); 00062 static void StartProcess(const char* executable, const char* args); 00063 }; 00064 00067 class SVSemaphore { 00068 public: 00070 SVSemaphore(); 00072 void Signal(); 00074 void Wait(); 00075 private: 00076 #ifdef _WIN32 00077 HANDLE semaphore_; 00078 #else 00079 sem_t semaphore_; 00080 #endif 00081 }; 00082 00085 class SVMutex { 00086 public: 00088 SVMutex(); 00090 void Lock(); 00092 void Unlock(); 00093 private: 00094 #ifdef _WIN32 00095 HANDLE mutex_; 00096 #else 00097 pthread_mutex_t mutex_; 00098 #endif 00099 }; 00100 00105 class SVNetwork { 00106 public: 00108 SVNetwork(const char* hostname, int port); 00109 00111 ~SVNetwork(); 00112 00114 void Send(const char* msg); 00115 00118 char* Receive(); 00119 00121 void Close(); 00122 00124 void Flush(); 00125 00126 private: 00128 SVMutex* mutex_send_; 00130 int stream_; 00132 char* msg_buffer_in_; 00133 00135 std::string msg_buffer_out_; 00136 00137 bool has_content; // Win32 (strtok) 00139 char* buffer_ptr_; // Unix (strtok_r) 00140 }; 00141 00142 #endif // TESSERACT_VIEWER_SVUTIL_H__