diff options
author | John Hodge <tpg@mutabah.net> | 2015-03-06 19:55:17 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2015-03-06 19:55:17 +0800 |
commit | 6d7c84bd9d8d5e2bec8bc86d22d53975bde6e833 (patch) | |
tree | dddee2de5214120a9808d2f7a367288807703b54 /src/include | |
parent | 414883e7b61d14edb242d320dcf0e5dea3e75e85 (diff) | |
download | mrust-6d7c84bd9d8d5e2bec8bc86d22d53975bde6e833.tar.gz |
Bugfixes exposed by fixing formatted output
Diffstat (limited to 'src/include')
-rw-r--r-- | src/include/debug.hpp | 26 | ||||
-rw-r--r-- | src/include/rustic.hpp | 100 |
2 files changed, 126 insertions, 0 deletions
diff --git a/src/include/debug.hpp b/src/include/debug.hpp new file mode 100644 index 00000000..012ef1bb --- /dev/null +++ b/src/include/debug.hpp @@ -0,0 +1,26 @@ +/* + */ +#pragma once +#include <sstream> + +extern int g_debug_indent_level; + +#define INDENT() do { g_debug_indent_level += 1; } while(0) +#define UNINDENT() do { g_debug_indent_level -= 1; } while(0) +#define DEBUG(ss) do{ if(debug_enabled()) { debug_output(g_debug_indent_level, __FUNCTION__) << ss << ::std::endl; } } while(0) + +extern bool debug_enabled(); +extern ::std::ostream& debug_output(int indent, const char* function); + +struct RepeatLitStr +{ + const char *s; + int n; + + friend ::std::ostream& operator<<(::std::ostream& os, const RepeatLitStr& r) { + for(int i = 0; i < r.n; i ++ ) + os << r.s; + return os; + } +}; + diff --git a/src/include/rustic.hpp b/src/include/rustic.hpp new file mode 100644 index 00000000..0e46777d --- /dev/null +++ b/src/include/rustic.hpp @@ -0,0 +1,100 @@ +/* + */ +#pragma once + +template<typename T> +class slice +{ + T* m_first; + unsigned int m_len; +public: + slice(::std::vector<T>& v): + m_first(&v[0]), + m_len(v.size()) + {} + + ::std::vector<T> to_vec() const { + return ::std::vector<T>(begin(), end()); + } + + unsigned int size() const { + return m_len; + } + T& operator[](unsigned int i) const { + assert(i < m_len); + return m_first[i]; + } + + T* begin() const { return m_first; } + T* end() const { return m_first + m_len; } +}; + +template<typename T> +::std::ostream& operator<<(::std::ostream& os, slice<T> s) { + if( s.size() > 0 ) + { + bool is_first = true; + for( const auto& i : s ) + { + if(!is_first) + os << ", "; + is_first = false; + os << i; + } + } + return os; +} + +namespace rust { + +template<typename T> +class option +{ + bool m_set; + T m_data; +public: + option(T ent): + m_set(true), + m_data( ::std::move(ent) ) + {} + option(): + m_set(false) + {} + + bool is_none() const { return !m_set; } + bool is_some() const { return m_set; } + + const T& unwrap() const { + assert(is_some()); + return m_data; + } +}; +template<typename T> +class option<T&> +{ + T* m_ptr; +public: + option(T& ent): + m_ptr(&ent) + {} + option(): + m_ptr(nullptr) + {} + + bool is_none() const { return m_ptr == nullptr; } + bool is_some() const { return m_ptr != nullptr; } + T& unwrap() const { + assert(is_some()); + return *m_ptr; + } +}; +template<typename T> +option<T> Some(T data) { + return option<T>( ::std::move(data) ); +} +template<typename T> +option<T> None() { + return option<T>( ); +} + +}; |