tesseract  5.0.0
svutil.h
Go to the documentation of this file.
1 // File: svutil.h
3 // Description: ScrollView Utilities
4 // Author: Joern Wanke
5 //
6 // (C) Copyright 2007, Google Inc.
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
18 //
19 // SVUtil contains the SVSync, SVSemaphore and SVNetwork
20 // classes, which are used for thread/process creation & synchronization
21 // and network connection.
22 
23 #ifndef TESSERACT_VIEWER_SVUTIL_H_
24 #define TESSERACT_VIEWER_SVUTIL_H_
25 
26 #ifdef _WIN32
27 # include "host.h" // also includes windows.h
28 #else
29 # include <semaphore.h>
30 #endif
31 
32 #include <mutex>
33 #include <string>
34 
35 namespace tesseract {
36 
38 class SVSync {
39 public:
41  static void StartProcess(const char *executable, const char *args);
42 };
43 
46 class SVSemaphore {
47 public:
49  SVSemaphore();
51  ~SVSemaphore();
53  void Signal();
55  void Wait();
56 
57 private:
58 #ifdef _WIN32
59  HANDLE semaphore_;
60 #elif defined(__APPLE__)
61  sem_t *semaphore_;
62 #else
63  sem_t semaphore_;
64 #endif
65 };
66 
71 class SVNetwork {
72 public:
74  SVNetwork(const char *hostname, int port);
75 
77  ~SVNetwork();
78 
80  void Send(const char *msg);
81 
84  char *Receive();
85 
87  void Close();
88 
90  void Flush();
91 
92 private:
94  std::mutex mutex_send_;
96  int stream_;
98  char *msg_buffer_in_;
99 
101  std::string msg_buffer_out_;
102 
104  char *buffer_ptr_; // strtok_r, strtok_s
105 };
106 
107 } // namespace tesseract
108 
109 #endif // TESSERACT_VIEWER_SVUTIL_H_
The SVSync class provides functionality for Thread & Process Creation.
Definition: svutil.h:38
static void StartProcess(const char *executable, const char *args)
Starts a new process.
Definition: svutil.cpp:67
void Signal()
Signal a semaphore.
Definition: svutil.cpp:143
~SVSemaphore()
Cleans up the mutex.
Definition: svutil.cpp:133
SVSemaphore()
Sets up a semaphore.
Definition: svutil.cpp:118
void Wait()
Wait on a semaphore.
Definition: svutil.cpp:153
void Flush()
Flush the buffer.
Definition: svutil.cpp:170
SVNetwork(const char *hostname, int port)
Set up a connection to hostname on port.
Definition: svutil.cpp:275
~SVNetwork()
Destructor.
Definition: svutil.cpp:349
void Send(const char *msg)
Put a message in the messagebuffer to the server and try to send it.
Definition: svutil.cpp:164
void Close()
Close the connection to the server.
Definition: svutil.cpp:225