tesseract  4.0.0-beta.1-59-g2cc4
svutil.h
Go to the documentation of this file.
1 // File: svutil.h
3 // Description: ScrollView Utilities
4 // Author: Joern Wanke
5 // Created: Thu Nov 29 2007
6 //
7 // (C) Copyright 2007, Google Inc.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
19 //
20 // SVUtil contains the SVSync, SVSemaphore, SVMutex and SVNetwork
21 // classes, which are used for thread/process creation & synchronization
22 // and network connection.
23 
24 #ifndef TESSERACT_VIEWER_SVUTIL_H_
25 #define TESSERACT_VIEWER_SVUTIL_H_
26 
27 #ifdef _WIN32
28 #ifndef __GNUC__
29 #include <windows.h>
30 #include "platform.h"
31 #if defined(_MSC_VER) && _MSC_VER < 1900
32 #define snprintf _snprintf
33 #endif
34 #pragma warning(disable:4786)
35 #else
36 #include "platform.h"
37 #include <windows.h>
38 #endif
39 #else
40 #include <pthread.h>
41 #include <semaphore.h>
42 #endif
43 
44 #include <string>
45 
46 #ifndef MAX
47 #define MAX(a, b) ((a > b) ? a : b)
48 #endif
49 
50 #ifndef MIN
51 #define MIN(a, b) ((a < b) ? a : b)
52 #endif
53 
55 class SVSync {
56  public:
58  static void StartThread(void *(*func)(void*), void* arg);
60  static void ExitThread();
62  static void StartProcess(const char* executable, const char* args);
63 };
64 
67 class SVSemaphore {
68  public:
70  SVSemaphore();
72  void Signal();
74  void Wait();
75  private:
76 #ifdef _WIN32
77  HANDLE semaphore_;
78 #elif defined(__APPLE__)
79  sem_t *semaphore_;
80 #else
81  sem_t semaphore_;
82 #endif
83 };
84 
87 class SVMutex {
88  public:
90  SVMutex();
92  void Lock();
94  void Unlock();
95  private:
96 #ifdef _WIN32
97  HANDLE mutex_;
98 #else
99  pthread_mutex_t mutex_;
100 #endif
101 };
102 
103 // Auto-unlocking object that locks a mutex on construction and unlocks it
104 // on destruction.
105 class SVAutoLock {
106  public:
107  explicit SVAutoLock(SVMutex* mutex) : mutex_(mutex) { mutex->Lock(); }
108  ~SVAutoLock() { mutex_->Unlock(); }
109 
110  private:
111  SVMutex* mutex_;
112 };
113 
118 class SVNetwork {
119  public:
121  SVNetwork(const char* hostname, int port);
122 
124  ~SVNetwork();
125 
127  void Send(const char* msg);
128 
131  char* Receive();
132 
134  void Close();
135 
137  void Flush();
138 
139  private:
141  SVMutex mutex_send_;
143  int stream_;
145  char* msg_buffer_in_;
146 
148  std::string msg_buffer_out_;
149 
150  bool has_content; // Win32 (strtok)
152  char* buffer_ptr_; // Unix (strtok_r)
153 };
154 
155 #endif // TESSERACT_VIEWER_SVUTIL_H_
static void StartThread(void *(*func)(void *), void *arg)
Create new thread.
Definition: svutil.cpp:87
SVAutoLock(SVMutex *mutex)
Definition: svutil.h:107
static void ExitThread()
Signals a thread to exit.
Definition: svutil.cpp:111
The SVSync class provides functionality for Thread & Process Creation.
Definition: svutil.h:55
void Lock()
Locks on a mutex.
Definition: svutil.cpp:70
Definition: svutil.h:87
static void StartProcess(const char *executable, const char *args)
Starts a new process.
Definition: svutil.cpp:120
~SVAutoLock()
Definition: svutil.h:108