tesseract  3.05.02
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 "platform.h"
30 #include <windows.h>
31 #if defined(_MSC_VER) && _MSC_VER < 1900
32 #define snprintf _snprintf
33 #endif
34 #if (_MSC_VER <= 1400)
35 #define vsnprintf _vsnprintf
36 #endif
37 #pragma warning(disable:4786)
38 #else
39 #include "platform.h"
40 #include <windows.h>
41 #endif
42 #else
43 #include <pthread.h>
44 #include <semaphore.h>
45 #endif
46 
47 #include <string>
48 
49 #ifndef MAX
50 #define MAX(a, b) ((a > b) ? a : b)
51 #endif
52 
53 #ifndef MIN
54 #define MIN(a, b) ((a < b) ? a : b)
55 #endif
56 
58 class SVSync {
59  public:
61  static void StartThread(void *(*func)(void*), void* arg);
63  static void ExitThread();
65  static void StartProcess(const char* executable, const char* args);
66 };
67 
70 class SVSemaphore {
71  public:
73  SVSemaphore();
75  void Signal();
77  void Wait();
78  private:
79 #ifdef _WIN32
80  HANDLE semaphore_;
81 #elif defined(__APPLE__)
82  sem_t *semaphore_;
83 #else
84  sem_t semaphore_;
85 #endif
86 };
87 
90 class SVMutex {
91  public:
93  SVMutex();
95  void Lock();
97  void Unlock();
98  private:
99 #ifdef _WIN32
100  HANDLE mutex_;
101 #else
102  pthread_mutex_t mutex_;
103 #endif
104 };
105 
106 // Auto-unlocking object that locks a mutex on construction and unlocks it
107 // on destruction.
108 class SVAutoLock {
109  public:
110  explicit SVAutoLock(SVMutex* mutex) : mutex_(mutex) { mutex->Lock(); }
111  ~SVAutoLock() { mutex_->Unlock(); }
112 
113  private:
114  SVMutex* mutex_;
115 };
116 
121 class SVNetwork {
122  public:
124  SVNetwork(const char* hostname, int port);
125 
127  ~SVNetwork();
128 
130  void Send(const char* msg);
131 
134  char* Receive();
135 
137  void Close();
138 
140  void Flush();
141 
142  private:
144  SVMutex* mutex_send_;
146  int stream_;
148  char* msg_buffer_in_;
149 
151  std::string msg_buffer_out_;
152 
153  bool has_content; // Win32 (strtok)
155  char* buffer_ptr_; // Unix (strtok_r)
156 };
157 
158 #endif // TESSERACT_VIEWER_SVUTIL_H__
SVSemaphore()
Sets up a semaphore.
Definition: svutil.cpp:153
static void ExitThread()
Signals a thread to exit.
Definition: svutil.cpp:91
~SVNetwork()
Destructor.
Definition: svutil.cpp:452
SVMutex()
Sets up a new mutex.
Definition: svutil.cpp:62
void Wait()
Wait on a semaphore.
Definition: svutil.cpp:179
static void StartProcess(const char *executable, const char *args)
Starts a new process.
Definition: svutil.cpp:100
char * Receive()
Definition: svutil.cpp:231
SVAutoLock(SVMutex *mutex)
Definition: svutil.h:110
The SVSync class provides functionality for Thread & Process Creation.
Definition: svutil.h:58
void Send(const char *msg)
Put a message in the messagebuffer to the server and try to send it.
Definition: svutil.cpp:213
void Lock()
Locks on a mutex.
Definition: svutil.cpp:70
SVNetwork(const char *hostname, int port)
Set up a connection to hostname on port.
Definition: svutil.cpp:392
~SVAutoLock()
Definition: svutil.h:111
void Flush()
Flush the buffer.
Definition: svutil.cpp:220
Definition: svutil.h:90
void Unlock()
Unlocks on a mutex.
Definition: svutil.cpp:78
void Close()
Close the connection to the server.
Definition: svutil.cpp:279
void Signal()
Signal a semaphore.
Definition: svutil.cpp:169
static void StartThread(void *(*func)(void *), void *arg)
Create new thread.
Definition: svutil.cpp:192