summaryrefslogtreecommitdiffstats
path: root/host/include
diff options
context:
space:
mode:
authorJosh Blum <josh@joshknows.com>2010-03-27 01:02:58 -0700
committerJosh Blum <josh@joshknows.com>2010-03-27 01:02:58 -0700
commit52df9afd679fd0f42edeef29f0bbc0d7bd76559e (patch)
treebcb28585a0195560e0551012ec2cfb33833ef917 /host/include
parentdd41206f2a5127871fc4c9911a748f7f4e3b6c51 (diff)
downloaduhd-52df9afd679fd0f42edeef29f0bbc0d7bd76559e.tar.gz
uhd-52df9afd679fd0f42edeef29f0bbc0d7bd76559e.tar.bz2
uhd-52df9afd679fd0f42edeef29f0bbc0d7bd76559e.zip
Split utils.hpp into subdir with multiple files.
static for static block and static instance (singleton) assert for assertion and throwing related stuff algorithm for my addons to std::algorithm (has) and a new one, safe main, for having a main catch-all
Diffstat (limited to 'host/include')
-rw-r--r--host/include/uhd/CMakeLists.txt2
-rw-r--r--host/include/uhd/utils/CMakeLists.txt24
-rw-r--r--host/include/uhd/utils/algorithm.hpp60
-rw-r--r--host/include/uhd/utils/assert.hpp (renamed from host/include/uhd/utils.hpp)74
-rw-r--r--host/include/uhd/utils/safe_main.hpp43
-rw-r--r--host/include/uhd/utils/static.hpp35
6 files changed, 173 insertions, 65 deletions
diff --git a/host/include/uhd/CMakeLists.txt b/host/include/uhd/CMakeLists.txt
index 84e7b441b..2203ea83e 100644
--- a/host/include/uhd/CMakeLists.txt
+++ b/host/include/uhd/CMakeLists.txt
@@ -18,6 +18,7 @@
ADD_SUBDIRECTORY(transport)
ADD_SUBDIRECTORY(usrp)
+ADD_SUBDIRECTORY(utils)
INSTALL(FILES
config.hpp
@@ -30,7 +31,6 @@ INSTALL(FILES
simple_device.hpp
time_spec.hpp
types.hpp
- utils.hpp
wax.hpp
DESTINATION ${INCLUDE_DIR}/uhd
)
diff --git a/host/include/uhd/utils/CMakeLists.txt b/host/include/uhd/utils/CMakeLists.txt
new file mode 100644
index 000000000..f6ed87701
--- /dev/null
+++ b/host/include/uhd/utils/CMakeLists.txt
@@ -0,0 +1,24 @@
+#
+# Copyright 2010 Ettus Research LLC
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+
+INSTALL(FILES
+ algorithm.hpp
+ assert.hpp
+ safe_main.hpp
+ static.hpp
+ DESTINATION ${INCLUDE_DIR}/uhd/utils
+)
diff --git a/host/include/uhd/utils/algorithm.hpp b/host/include/uhd/utils/algorithm.hpp
new file mode 100644
index 000000000..6635c8a4a
--- /dev/null
+++ b/host/include/uhd/utils/algorithm.hpp
@@ -0,0 +1,60 @@
+//
+// Copyright 2010 Ettus Research LLC
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+//
+
+#ifndef INCLUDED_UHD_UTILS_ALGORITHM_HPP
+#define INCLUDED_UHD_UTILS_ALGORITHM_HPP
+
+#include <algorithm>
+
+/*!
+ * Useful templated functions and classes that I like to pretend are part of stl
+ */
+namespace std{
+
+ template<class T, class InputIterator, class Function>
+ T reduce(InputIterator first, InputIterator last, Function fcn, T init = 0){
+ T tmp = init;
+ for ( ; first != last; ++first ){
+ tmp = fcn(tmp, *first);
+ }
+ return tmp;
+ }
+
+ template<class T, class Iterable, class Function>
+ T reduce(Iterable iterable, Function fcn, T init = 0){
+ return reduce(iterable.begin(), iterable.end(), fcn, init);
+ }
+
+ template<class T, class InputIterator>
+ bool has(InputIterator first, InputIterator last, const T &elem){
+ return last != std::find(first, last, elem);
+ }
+
+ template<class T, class Iterable>
+ bool has(const Iterable &iterable, const T &elem){
+ return has(iterable.begin(), iterable.end(), elem);
+ }
+
+ template<typename T> T signum(T n){
+ if (n < 0) return -1;
+ if (n > 0) return 1;
+ return 0;
+ }
+
+}//namespace std
+
+#endif /* INCLUDED_UHD_UTILS_ALGORITHM_HPP */
diff --git a/host/include/uhd/utils.hpp b/host/include/uhd/utils/assert.hpp
index e5333539f..842ed8dfa 100644
--- a/host/include/uhd/utils.hpp
+++ b/host/include/uhd/utils/assert.hpp
@@ -15,85 +15,31 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
-#ifndef INCLUDED_UHD_UTILS_HPP
-#define INCLUDED_UHD_UTILS_HPP
+#ifndef INCLUDED_UHD_UTILS_ASSERT_HPP
+#define INCLUDED_UHD_UTILS_ASSERT_HPP
-#include <uhd/config.hpp>
+#include <uhd/utils/algorithm.hpp>
#include <boost/format.hpp>
+#include <boost/foreach.hpp>
+#include <boost/lexical_cast.hpp>
#include <boost/current_function.hpp>
#include <stdexcept>
-#include <algorithm>
-
-/*!
- * Defines a function that implements the "construct on first use" idiom
- * \param _t the type definition for the instance
- * \param _x the name of the defined function
- * \return a reference to the lazy instance
- */
-#define STATIC_INSTANCE(_t, _x) static _t &_x(){static _t _x; return _x;}
-/*!
- * Defines a static code block that will be called before main()
- * \param _x the name of the defined struct (must be unique in file)
- */
-#define STATIC_BLOCK(_x) static struct _x{_x();}_x;_x::_x()
-
-/*!
- * Useful templated functions and classes that I like to pretend are part of stl
- */
-namespace std{
+namespace uhd{
class assert_error : public std::logic_error{
public:
- explicit assert_error(const string& what_arg) : logic_error(what_arg){
+ explicit assert_error(const std::string& what_arg) : logic_error(what_arg){
/* NOP */
}
};
#define ASSERT_THROW(_x) if (not (_x)) { \
- throw std::assert_error(str(boost::format( \
+ throw uhd::assert_error(str(boost::format( \
"Assertion Failed:\n %s:%d\n %s\n ---> %s <---" \
) % __FILE__ % __LINE__ % BOOST_CURRENT_FUNCTION % std::string(#_x))); \
}
- template<class T, class InputIterator, class Function>
- T reduce(InputIterator first, InputIterator last, Function fcn, T init = 0){
- T tmp = init;
- for ( ; first != last; ++first ){
- tmp = fcn(tmp, *first);
- }
- return tmp;
- }
-
- template<class T, class Iterable, class Function>
- T reduce(Iterable iterable, Function fcn, T init = 0){
- return reduce(iterable.begin(), iterable.end(), fcn, init);
- }
-
- template<class T, class InputIterator>
- bool has(InputIterator first, InputIterator last, const T &elem){
- return last != std::find(first, last, elem);
- }
-
- template<class T, class Iterable>
- bool has(const Iterable &iterable, const T &elem){
- return has(iterable.begin(), iterable.end(), elem);
- }
-
- template<typename T> T signum(T n){
- if (n < 0) return -1;
- if (n > 0) return 1;
- return 0;
- }
-
-}//namespace std
-
-#include <boost/format.hpp>
-#include <boost/foreach.hpp>
-#include <boost/lexical_cast.hpp>
-
-namespace uhd{
-
/*!
* Check that an element is found in a container.
* If not, throw a meaningful assertion error.
@@ -116,7 +62,7 @@ namespace uhd{
if (e != iterable.begin()[0]) possible_values += ", ";
possible_values += boost::lexical_cast<std::string>(e);
}
- throw std::assert_error(str(boost::format(
+ throw uhd::assert_error(str(boost::format(
"Error: %s is not a valid %s. "
"Possible values are: [%s]."
)
@@ -127,4 +73,4 @@ namespace uhd{
}//namespace uhd
-#endif /* INCLUDED_UHD_UTILS_HPP */
+#endif /* INCLUDED_UHD_UTILS_ASSERT_HPP */
diff --git a/host/include/uhd/utils/safe_main.hpp b/host/include/uhd/utils/safe_main.hpp
new file mode 100644
index 000000000..dd738f2e3
--- /dev/null
+++ b/host/include/uhd/utils/safe_main.hpp
@@ -0,0 +1,43 @@
+//
+// Copyright 2010 Ettus Research LLC
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+//
+
+#ifndef INCLUDED_UHD_UTILS_SAFE_MAIN_HPP
+#define INCLUDED_UHD_UTILS_SAFE_MAIN_HPP
+
+#include <iostream>
+#include <stdexcept>
+
+/*!
+ * Defines a safe wrapper that places a catch-all around main.
+ * If an exception is thrown, it prints to stderr and returns.
+ * Usage: int UHD_SAFE_MAIN(int argc, char *argv[]){ main code here }
+ * \param _argc the declaration for argc
+ * \param _argv the declaration for argv
+ */
+#define UHD_SAFE_MAIN(_argc, _argv) _main(int, char*[]); \
+int main(int argc, char *argv[]){ \
+ try { \
+ return _main(argc, argv); \
+ } catch(const std::exception &e) { \
+ std::cerr << "Error: " << e.what() << std::endl; \
+ } catch(...) { \
+ std::cerr << "Error: unknown exception" << std::endl; \
+ } \
+ return ~0; \
+} int _main(_argc, _argv)
+
+#endif /* INCLUDED_UHD_UTILS_SAFE_MAIN_HPP */
diff --git a/host/include/uhd/utils/static.hpp b/host/include/uhd/utils/static.hpp
new file mode 100644
index 000000000..63db5a247
--- /dev/null
+++ b/host/include/uhd/utils/static.hpp
@@ -0,0 +1,35 @@
+//
+// Copyright 2010 Ettus Research LLC
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+//
+
+#ifndef INCLUDED_UHD_UTILS_STATIC_HPP
+#define INCLUDED_UHD_UTILS_STATIC_HPP
+
+/*!
+ * Defines a function that implements the "construct on first use" idiom
+ * \param _t the type definition for the instance
+ * \param _x the name of the defined function
+ * \return a reference to the lazy instance
+ */
+#define UHD_SINGLETON_FCN(_t, _x) static _t &_x(){static _t _x; return _x;}
+
+/*!
+ * Defines a static code block that will be called before main()
+ * \param _x the name of the defined struct (must be unique in file)
+ */
+#define UHD_STATIC_BLOCK(_x) static struct _x{_x();}_x;_x::_x()
+
+#endif /* INCLUDED_UHD_UTILS_STATIC_HPP */