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
32 /// Pthreads based mutex class
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
);
49 return pthread_mutex_lock(&pm
) == 0;
54 return pthread_mutex_trylock(&pm
) == 0;
59 pthread_mutex_unlock(&pm
);
64 pthread_mutex_destroy(&pm
);
74 ptlock_base(ptmutex
&_m
)
101 /// Exception-safe mutex lock
102 class ptlock
: public ptlock_base
105 ptlock(ptmutex
&_m
) : ptlock_base(_m
)
107 locked
= mutex
.lock();
111 /// Exception-safe polling mutex lock
112 class pttrylock
: public ptlock_base
115 pttrylock(ptmutex
&_m
) : ptlock_base(_m
)
117 locked
= mutex
.trylock();
121 /// Exception-safe temporary assignment
122 template<class T
, class Tref
= T
&>
128 scope_assign(Tref _data
, T new_value
)
129 : data(_data
), old_value(_data
)
139 struct text_exception
: public std::exception
142 std::string container
;
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
152 std::string message
, filename
, container
;
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
);
169 extern std::string
i2s(int value
);
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
);