+ EQ-5: refactoring to eliminate code duplication, side effect: stereo meters
[calf.git] / src / calf / utils.h
blob406c71ee74986498098284f787b3185d41e1f623
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 /// Exception-safe mutex lock
69 class ptlock
71 ptmutex &mutex;
72 bool locked;
74 public:
75 ptlock(ptmutex &_m) : mutex(_m), locked(true)
77 mutex.lock();
79 void unlock()
81 mutex.unlock();
82 locked = false;
84 void unlocked()
86 locked = false;
88 ~ptlock()
90 if (locked)
91 mutex.unlock();
95 /// Exception-safe temporary assignment
96 template<class T>
97 class scope_assign
99 T &data, old_value;
100 public:
101 scope_assign(T &_data, T new_value)
102 : data(_data), old_value(_data)
104 data = new_value;
106 ~scope_assign()
108 data = old_value;
112 struct text_exception: public std::exception
114 const char *text;
115 std::string container;
116 public:
117 text_exception(const std::string &t) : container(t) { text = container.c_str(); }
118 virtual const char *what() const throw () { return text; }
119 virtual ~text_exception() throw () {}
122 struct file_exception: public std::exception
124 const char *text;
125 std::string message, filename, container;
126 public:
127 file_exception(const std::string &f) : message(strerror(errno)), filename(f), container(filename + ":" + message) { text = container.c_str(); }
128 file_exception(const std::string &f, const std::string &t) : message(t), filename(f), container(filename + ":" + message) { text = container.c_str(); }
129 virtual const char *what() const throw () { return text; }
130 virtual ~file_exception() throw () {}
133 /// String-to-string mapping
134 typedef std::map<std::string, std::string> dictionary;
136 /// Serialize a dictonary to a string
137 extern std::string encode_map(const dictionary &data);
138 /// Deserialize a dictonary from a string
139 extern void decode_map(dictionary &data, const std::string &src);
141 /// int-to-string
142 extern std::string i2s(int value);
144 /// float-to-string
145 extern std::string f2s(double value);
147 /// float-to-string-that-doesn't-resemble-an-int
148 extern std::string ff2s(double value);
150 /// Escape a string to be used in XML file
151 std::string xml_escape(const std::string &src);
153 /// Load file from disk into a std::string blob, or throw file_exception
154 std::string load_file(const std::string &src);
156 /// Indent a string by another string (prefix each line)
157 std::string indent(const std::string &src, const std::string &indent);
161 #endif