LMMS
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1/* Calf DSP Library
2 * Utilities
3 *
4 * Copyright (C) 2008 Krzysztof Foltman
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02111-1307, USA.
20 */
21#ifndef __CALF_UTILS_H
22#define __CALF_UTILS_H
23
24#include <errno.h>
25
26#include <map>
27#include <string>
28#include <sys/types.h>
29#include <vector>
30
31#ifdef _MSC_VER
32#include <malloc.h>
33#else
34#include <dirent.h>
35#include <pthread.h>
36#endif
37
38namespace calf_utils
39{
40
41#ifdef _MSC_VER
42#define STACKALLOC(type, name, size) type *name = static_cast<type*>(_alloca((size)*sizeof(type)))
43//TODO: add replacements for ptmutex,...
44#else
45#define STACKALLOC(type, name, size) type name[size]
48{
49public:
50 pthread_mutex_t pm;
51
52 ptmutex(int type = PTHREAD_MUTEX_RECURSIVE)
53 {
54 pthread_mutexattr_t attr;
55 pthread_mutexattr_init(&attr);
56 pthread_mutexattr_settype(&attr, type);
57 pthread_mutex_init(&pm, &attr);
58 pthread_mutexattr_destroy(&attr);
59 }
60
61 bool lock()
62 {
63 return pthread_mutex_lock(&pm) == 0;
64 }
65
66 bool trylock()
67 {
68 return pthread_mutex_trylock(&pm) == 0;
69 }
70
71 void unlock()
72 {
73 pthread_mutex_unlock(&pm);
74 }
75
77 {
78 pthread_mutex_destroy(&pm);
79 }
80};
81
83{
84protected:
86 bool locked;
87
89 : mutex(_m)
90 , locked(false)
91 {
92 }
93
94public:
95 bool is_locked()
96 {
97 return locked;
98 }
99 void unlock()
100 {
101 mutex.unlock();
102 locked = false;
103 }
104 void unlocked()
105 {
106 locked = false;
107 }
109 {
110 if (locked)
111 mutex.unlock();
112 }
113};
114
116class ptlock: public ptlock_base
117{
118public:
120 {
121 locked = mutex.lock();
122 }
123};
124
127{
128public:
130 {
131 locked = mutex.trylock();
132 }
133};
134#endif
136template<class T, class Tref = T&>
138{
139 Tref data;
141public:
142 scope_assign(Tref _data, T new_value)
143 : data(_data), old_value(_data)
144 {
145 data = new_value;
146 }
148 {
149 data = old_value;
150 }
151};
152
153struct text_exception: public std::exception
154{
155 const char *text;
156 std::string container;
157public:
158 text_exception(const std::string &t) : container(t) { text = container.c_str(); }
159 virtual const char *what() const throw () { return text; }
160 virtual ~text_exception() throw () {}
161};
162
163struct file_exception: public std::exception
164{
165 const char *text;
167public:
168 file_exception(const std::string &f);
169 file_exception(const std::string &f, const std::string &t);
170 virtual const char *what() const throw () { return text; }
171 virtual ~file_exception() throw () {}
172};
173
175typedef std::map<std::string, std::string> dictionary;
176
178extern std::string encode_map(const dictionary &data);
180extern void decode_map(dictionary &data, const std::string &src);
181
183extern std::string i2s(int value);
184
186extern std::string f2s(double value);
187
189extern std::string ff2s(double value);
190
192std::string to_xml_attr(const std::string &key, const std::string &value);
193
195std::string xml_escape(const std::string &src);
196
198std::string load_file(const std::string &src);
199
201std::string indent(const std::string &src, const std::string &indent);
202
204struct direntry {
205 std::string name;
206 std::string full_path;
207 std::string directory;
208};
209std::vector <direntry> list_directory(const std::string &path);
210
211};
212
213#endif
CAdPlugDatabase::CRecord::RecordType type
Definition adplugdb.cpp:93
ptmutex & mutex
Definition utils.h:85
~ptlock_base()
Definition utils.h:108
bool is_locked()
Definition utils.h:95
bool locked
Definition utils.h:86
void unlocked()
Definition utils.h:104
ptlock_base(ptmutex &_m)
Definition utils.h:88
void unlock()
Definition utils.h:99
ptlock(ptmutex &_m)
Definition utils.h:119
Pthreads based mutex class.
Definition utils.h:48
ptmutex(int type=PTHREAD_MUTEX_RECURSIVE)
Definition utils.h:52
bool lock()
Definition utils.h:61
void unlock()
Definition utils.h:71
~ptmutex()
Definition utils.h:76
pthread_mutex_t pm
Definition utils.h:50
bool trylock()
Definition utils.h:66
pttrylock(ptmutex &_m)
Definition utils.h:129
scope_assign(Tref _data, T new_value)
Definition utils.h:142
vector< bool >::reference data
Definition utils.h:139
~scope_assign()
Definition utils.h:147
struct huft * t
Definition inflate.c:943
unsigned f
Definition inflate.c:1572
static PuglViewHint int value
Definition pugl.h:1708
JSAMPIMAGE data
Definition jpeglib.h:945
Definition gui_config.h:8
std::string ff2s(double value)
float-to-string-that-doesn't-resemble-an-int
Definition utils.cpp:127
std::string f2s(double value)
float-to-string
Definition utils.cpp:120
void decode_map(dictionary &data, const std::string &src)
Deserialize a dictionary from a string.
std::map< std::string, std::string > dictionary
String-to-string mapping.
Definition utils.h:175
std::vector< direntry > list_directory(const std::string &path)
std::string to_xml_attr(const std::string &key, const std::string &value)
Encode a key-value pair as XML attribute.
Definition utils.cpp:86
std::string indent(const std::string &src, const std::string &indent)
Indent a string by another string (prefix each line).
Definition utils.cpp:135
std::string xml_escape(const std::string &src)
Escape a string to be used in XML file.
Definition utils.cpp:73
std::string encode_map(const dictionary &data)
Serialize a dictionary to a string.
Definition utils.cpp:44
std::string i2s(int value)
int-to-string
Definition utils.cpp:112
std::string load_file(const std::string &src)
Load file from disk into a std::string blob, or throw file_exception.
Definition utils.cpp:91
#define false
Definition ordinals.h:83
List contents of a directory.
Definition utils.h:204
std::string name
Definition utils.h:205
std::string directory
Definition utils.h:207
std::string full_path
Definition utils.h:206
std::string container
Definition utils.h:166
virtual ~file_exception()
Definition utils.h:171
const char * text
Definition utils.h:165
file_exception(const std::string &f)
Definition utils.cpp:153
std::string message
Definition utils.h:166
virtual const char * what() const
Definition utils.h:170
std::string filename
Definition utils.h:166
text_exception(const std::string &t)
Definition utils.h:158
virtual const char * what() const
Definition utils.h:159
std::string container
Definition utils.h:156
virtual ~text_exception()
Definition utils.h:160
const char * text
Definition utils.h:155
ZCONST char * key
Definition crypt.c:587