All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 #define snprintf _snprintf
31 #if (_MSC_VER <= 1400)
32 #define vsnprintf _vsnprintf
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 
107 class SVNetwork {
108  public:
110  SVNetwork(const char* hostname, int port);
111 
113  ~SVNetwork();
114 
116  void Send(const char* msg);
117 
120  char* Receive();
121 
123  void Close();
124 
126  void Flush();
127 
128  private:
130  SVMutex* mutex_send_;
132  int stream_;
134  char* msg_buffer_in_;
135 
137  std::string msg_buffer_out_;
138 
139  bool has_content; // Win32 (strtok)
141  char* buffer_ptr_; // Unix (strtok_r)
142 };
143 
144 #endif // TESSERACT_VIEWER_SVUTIL_H__
Definition: svutil.h:87
void Lock()
Locks on a mutex.
Definition: svutil.cpp:169
void Close()
Close the connection to the server.
Definition: svutil.cpp:271
void Flush()
Flush the buffer.
Definition: svutil.cpp:212
char * Receive()
Definition: svutil.cpp:223
static void StartProcess(const char *executable, const char *args)
Starts a new process.
Definition: svutil.cpp:75
SVMutex()
Sets up a new mutex.
Definition: svutil.cpp:161
SVSemaphore()
Sets up a semaphore.
Definition: svutil.cpp:125
void Send(const char *msg)
Put a message in the messagebuffer to the server and try to send it.
Definition: svutil.cpp:205
static void ExitThread()
Signals a thread to exit.
Definition: svutil.cpp:66
void Wait()
Wait on a semaphore.
Definition: svutil.cpp:151
void Signal()
Signal a semaphore.
Definition: svutil.cpp:141
static void StartThread(void *(*func)(void *), void *arg)
Create new thread.
Definition: svutil.cpp:187
~SVNetwork()
Destructor.
Definition: svutil.cpp:444
void Unlock()
Unlocks on a mutex.
Definition: svutil.cpp:177
The SVSync class provides functionality for Thread & Process Creation.
Definition: svutil.h:55
SVNetwork(const char *hostname, int port)
Set up a connection to hostname on port.
Definition: svutil.cpp:386