Button: emit slopes (fix for stuck modulaor plug-ins)
[calf.git] / src / calf / utils.h
blob783a98ad357b44cf6ba1d4695c31a3e2524674c7
1 /* Calf DSP Library
2 * Utilities
4 * Copyright (C) 2008 Krzysztof Foltman
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.
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.
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.
21 #ifndef __CALF_UTILS_H
22 #define __CALF_UTILS_H
24 #include <errno.h>
25 #include <pthread.h>
26 #include <map>
27 #include <string>
29 namespace calf_utils
32 /// Pthreads based mutex class
33 class ptmutex
35 public:
36 pthread_mutex_t pm;
38 ptmutex(int type = PTHREAD_MUTEX_RECURSIVE)
40 pthread_mutexattr_t attr;
41 pthread_mutexattr_init(&attr);
42 pthread_mutexattr_settype(&attr, type);
43 pthread_mutex_init(&pm, &attr);
44 pthread_mutexattr_destroy(&attr);
47 bool lock()
49 return pthread_mutex_lock(&pm) == 0;
52 bool trylock()
54 return pthread_mutex_trylock(&pm) == 0;
57 void unlock()
59 pthread_mutex_unlock(&pm);
62 ~ptmutex()
64 pthread_mutex_destroy(&pm);
68 class ptlock_base
70 protected:
71 ptmutex &mutex;
72 bool locked;
74 ptlock_base(ptmutex &_m)
75 : mutex(_m)
76 , locked(false)
80 public:
81 bool is_locked()
83 return locked;
85 void unlock()
87 mutex.unlock();
88 locked = false;
90 void unlocked()
92 locked = false;
94 ~ptlock_base()
96 if (locked)
97 mutex.unlock();
101 /// Exception-safe mutex lock
102 class ptlock: public ptlock_base
104 public:
105 ptlock(ptmutex &_m) : ptlock_base(_m)
107 locked = mutex.lock();
111 /// Exception-safe polling mutex lock
112 class pttrylock: public ptlock_base
114 public:
115 pttrylock(ptmutex &_m) : ptlock_base(_m)
117 locked = mutex.trylock();
121 /// Exception-safe temporary assignment
122 template<class T, class Tref = T&>
123 class scope_assign
125 Tref data;
126 T old_value;
127 public:
128 scope_assign(Tref _data, T new_value)
129 : data(_data), old_value(_data)
131 data = new_value;
133 ~scope_assign()
135 data = old_value;
139 struct text_exception: public std::exception
141 const char *text;
142 std::string container;
143 public:
144 text_exception(const std::string &t) : container(t) { text = container.c_str(); }
145 virtual const char *what() const throw () { return text; }
146 virtual ~text_exception() throw () {}
149 struct file_exception: public std::exception
151 const char *text;
152 std::string message, filename, container;
153 public:
154 file_exception(const std::string &f);
155 file_exception(const std::string &f, const std::string &t);
156 virtual const char *what() const throw () { return text; }
157 virtual ~file_exception() throw () {}
160 /// String-to-string mapping
161 typedef std::map<std::string, std::string> dictionary;
163 /// Serialize a dictonary to a string
164 extern std::string encode_map(const dictionary &data);
165 /// Deserialize a dictonary from a string
166 extern void decode_map(dictionary &data, const std::string &src);
168 /// int-to-string
169 extern std::string i2s(int value);
171 /// float-to-string
172 extern std::string f2s(double value);
174 /// float-to-string-that-doesn't-resemble-an-int
175 extern std::string ff2s(double value);
177 /// Encode a key-value pair as XML attribute
178 std::string to_xml_attr(const std::string &key, const std::string &value);
180 /// Escape a string to be used in XML file
181 std::string xml_escape(const std::string &src);
183 /// Load file from disk into a std::string blob, or throw file_exception
184 std::string load_file(const std::string &src);
186 /// Indent a string by another string (prefix each line)
187 std::string indent(const std::string &src, const std::string &indent);
191 #endif