tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
svutil.h
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 #include <windows.h>
29 #include "platform.h"
30 #else
31 #include <pthread.h>
32 #include <semaphore.h>
33 #endif
34 
35 #include <string>
36 
37 #ifndef MAX
38 #define MAX(a, b) ((a > b) ? a : b)
39 #endif
40 
41 #ifndef MIN
42 #define MIN(a, b) ((a < b) ? a : b)
43 #endif
44 
46 class SVSync {
47  public:
49  static void StartThread(void *(*func)(void*), void* arg);
51  static void ExitThread();
53  static void StartProcess(const char* executable, const char* args);
54 };
55 
58 class SVSemaphore {
59  public:
61  SVSemaphore();
63  void Signal();
65  void Wait();
66  private:
67 #ifdef _WIN32
68  HANDLE semaphore_;
69 #elif defined(__APPLE__)
70  sem_t *semaphore_;
71 #else
72  sem_t semaphore_;
73 #endif
74 };
75 
78 class SVMutex {
79  public:
81  SVMutex();
83  void Lock();
85  void Unlock();
86  private:
87 #ifdef _WIN32
88  HANDLE mutex_;
89 #else
90  pthread_mutex_t mutex_;
91 #endif
92 };
93 
94 // Auto-unlocking object that locks a mutex on construction and unlocks it
95 // on destruction.
96 class SVAutoLock {
97  public:
98  explicit SVAutoLock(SVMutex* mutex) : mutex_(mutex) { mutex->Lock(); }
99  ~SVAutoLock() { mutex_->Unlock(); }
100 
101  private:
103 };
104 
109 class SVNetwork {
110  public:
112  SVNetwork(const char* hostname, int port);
113 
115  ~SVNetwork();
116 
118  void Send(const char* msg);
119 
122  char* Receive();
123 
125  void Close();
126 
128  void Flush();
129 
130  private:
134  int stream_;
137 
139  std::string msg_buffer_out_;
140 
141  bool has_content; // Win32 (strtok)
143  char* buffer_ptr_; // Unix (strtok_r)
144 };
145 
146 #endif // TESSERACT_VIEWER_SVUTIL_H_
pthread_mutex_t mutex_
Definition: svutil.h:90
static void StartProcess(const char *executable, const char *args)
Starts a new process.
Definition: svutil.cpp:120
std::string msg_buffer_out_
Stores the messages which are supposed to go out.
Definition: svutil.h:139
Definition: svutil.h:58
static void StartThread(void *(*func)(void *), void *arg)
Create new thread.
Definition: svutil.cpp:87
The SVSync class provides functionality for Thread & Process Creation.
Definition: svutil.h:46
SVMutex mutex_send_
The mutex for access to Send() and Flush().
Definition: svutil.h:132
~SVAutoLock()
Definition: svutil.h:99
SVAutoLock(SVMutex *mutex)
Definition: svutil.h:98
bool has_content
Definition: svutil.h:141
int stream_
The actual stream_ to the server.
Definition: svutil.h:134
SVMutex * mutex_
Definition: svutil.h:102
sem_t semaphore_
Definition: svutil.h:72
Definition: svutil.h:78
Definition: svutil.h:109
void Lock()
Locks on a mutex.
Definition: svutil.cpp:70
Definition: svutil.h:96
static void ExitThread()
Signals a thread to exit.
Definition: svutil.cpp:111
char * buffer_ptr_
Where we are at in our msg_buffer_in_.
Definition: svutil.h:143
char * msg_buffer_in_
Stores the last received message-chunk from the server.
Definition: svutil.h:136