summaryrefslogtreecommitdiff
path: root/include/util
diff options
context:
space:
mode:
authorAndreas Baumann <abaumann@yahoo.com>2012-09-05 18:51:26 +0200
committerAndreas Baumann <abaumann@yahoo.com>2012-09-05 18:51:26 +0200
commitdf2c44401f8dd736a903e73813e5f83fb15b36b6 (patch)
treeb845f54b2dbb3f14dab435d2640c58b7b85e7975 /include/util
parent43aaf39cc828f4e1cec12a944560571993fb24f7 (diff)
downloadcrawler-df2c44401f8dd736a903e73813e5f83fb15b36b6.tar.gz
crawler-df2c44401f8dd736a903e73813e5f83fb15b36b6.tar.bz2
split away util, logger, and module
made a liblogger adapted all tests
Diffstat (limited to 'include/util')
-rw-r--r--include/util/Exportable.hpp21
-rwxr-xr-xinclude/util/NonCopyable.hpp24
-rw-r--r--include/util/Noreturn.hpp11
-rw-r--r--include/util/ScopedPtr.hpp33
-rwxr-xr-xinclude/util/Singleton.hpp65
-rw-r--r--include/util/TypeDetect.hpp15
-rwxr-xr-xinclude/util/TypeInfo.hpp82
-rw-r--r--include/util/TypeList.hpp28
-rw-r--r--include/util/TypeTraits.hpp9
9 files changed, 288 insertions, 0 deletions
diff --git a/include/util/Exportable.hpp b/include/util/Exportable.hpp
new file mode 100644
index 0000000..28ac7ff
--- /dev/null
+++ b/include/util/Exportable.hpp
@@ -0,0 +1,21 @@
+#ifndef __EXPORTABLE_H
+#define __EXPORTABLE_H
+
+#ifndef _WIN32
+
+#define SINGLETON_EXPORT
+#define SINGLETON_EXTERN
+
+#else
+
+#ifndef SHARED
+#define SINGLETON_EXPORT __declspec(dllexport)
+#define SINGLETON_EXTERN
+#else
+#define SINGLETON_EXPORT __declspec(dllimport)
+#define SINGLETON_EXTERN extern
+#endif
+
+#endif // _WIN32
+
+#endif
diff --git a/include/util/NonCopyable.hpp b/include/util/NonCopyable.hpp
new file mode 100755
index 0000000..44d1a93
--- /dev/null
+++ b/include/util/NonCopyable.hpp
@@ -0,0 +1,24 @@
+#ifndef __NONCOPYABLE_H
+#define __NONCOPYABLE_H
+
+#include "Exportable.hpp"
+
+namespace __dont_touch
+{
+
+class SINGLETON_EXPORT noncopyable
+{
+ protected:
+ noncopyable( ) { }
+ ~noncopyable( ) { }
+
+ private:
+ noncopyable( const noncopyable & );
+ const noncopyable & operator=( const noncopyable & );
+};
+
+}
+
+typedef __dont_touch::noncopyable noncopyable;
+
+#endif
diff --git a/include/util/Noreturn.hpp b/include/util/Noreturn.hpp
new file mode 100644
index 0000000..9f9862c
--- /dev/null
+++ b/include/util/Noreturn.hpp
@@ -0,0 +1,11 @@
+#ifndef __NORETURN_H_
+#define __NORETURN_H
+
+#ifdef __GNUC__
+#define CRAWLER_NORETURN __attribute__((noreturn))
+#else
+#define CRAWLER_NORETURN
+#endif
+
+#endif
+
diff --git a/include/util/ScopedPtr.hpp b/include/util/ScopedPtr.hpp
new file mode 100644
index 0000000..19a41d2
--- /dev/null
+++ b/include/util/ScopedPtr.hpp
@@ -0,0 +1,33 @@
+#ifndef __SCOPEDPTR_H
+#define __SCOPEDPTR_H
+
+#include "NonCopyable.hpp"
+
+template< typename T >
+class scopedPtr : private noncopyable
+{
+ public:
+ explicit scopedPtr( T *p = 0 ) : m_p( p ) { }
+
+ ~scopedPtr( ) { delete m_p; }
+
+ T& operator*( ) const { return *m_p; }
+ T* operator->( ) const { return m_p; }
+ T* get( ) const { return m_p; }
+
+ bool operator==( const T* p ) const { return m_p == p; }
+ bool operator!=( const T* p ) const { return m_p != p; }
+
+ void reset( T *p = 0 )
+ {
+ if( m_p != p ) {
+ delete m_p;
+ m_p = p;
+ }
+ }
+
+ private:
+ T *m_p;
+};
+
+#endif
diff --git a/include/util/Singleton.hpp b/include/util/Singleton.hpp
new file mode 100755
index 0000000..1bfb460
--- /dev/null
+++ b/include/util/Singleton.hpp
@@ -0,0 +1,65 @@
+#ifndef __SINGLETON_H
+#define __SINGLETON_H
+
+#include "ScopedPtr.hpp"
+#include "NonCopyable.hpp"
+#include "Exportable.hpp"
+#include "Noreturn.hpp"
+
+#include <cstdlib>
+#include <stdexcept>
+
+#define DECLARE_SINGLETON( T ) \
+ friend class scopedPtr< T >; \
+ friend class Singleton< T >;
+
+#define DEFINE_SINGLETON( T ) \
+ SINGLETON_EXTERN template class SINGLETON_EXPORT Singleton< T >;
+
+template< class T >
+class Singleton : private noncopyable
+{
+ public:
+ static T& instance( )
+ {
+ if( destroyed ) {
+ onDeadReference( );
+ }
+
+ if( t == 0 ) {
+ create( );
+ }
+
+ return *t;
+ }
+
+ protected:
+ Singleton( )
+ {
+ }
+
+ virtual ~Singleton( )
+ {
+ destroyed = true;
+ }
+
+ private:
+ static void create( )
+ {
+ t.reset( new T );
+ }
+
+ static void onDeadReference( ) CRAWLER_NORETURN
+ {
+ throw std::runtime_error( "singleton has already been destroyed!" );
+ }
+
+ static scopedPtr<T> t;
+ static bool destroyed;
+};
+
+template< class T > scopedPtr<T> Singleton<T>::t( 0 );
+template< class T > bool Singleton<T>::destroyed( false );
+
+#endif
+
diff --git a/include/util/TypeDetect.hpp b/include/util/TypeDetect.hpp
new file mode 100644
index 0000000..7db714b
--- /dev/null
+++ b/include/util/TypeDetect.hpp
@@ -0,0 +1,15 @@
+#ifndef __TYPE_DETECTION_H
+#define __TYPE_DETECTION_H
+
+#include "RewindInputStream.hpp"
+#include "MIMEType.hpp"
+
+class TypeDetect
+{
+ public:
+ virtual ~TypeDetect( ) { };
+
+ virtual MIMEType detect( RewindInputStream *s ) = 0;
+};
+
+#endif
diff --git a/include/util/TypeInfo.hpp b/include/util/TypeInfo.hpp
new file mode 100755
index 0000000..3ca4b57
--- /dev/null
+++ b/include/util/TypeInfo.hpp
@@ -0,0 +1,82 @@
+#ifndef __TYPEINFO_H
+#define __TYPEINFO_H
+
+#include <typeinfo>
+#include <string>
+#include <stdexcept>
+#include <cstdlib>
+
+#if defined( __GNUG__ ) && defined( __GLIBCXX__ )
+
+#include <cxxabi.h>
+
+std::string demangle( const std::type_info &info )
+{
+ enum { BUFLEN = 200 };
+ char *buf;
+ std::size_t buflen = BUFLEN;
+ int status;
+ char *res;
+
+ // pass a malloced buffer (required by cxa_demangle)
+ buf = (char *)malloc( BUFLEN );
+
+ // res may point to buf or be realloced depending on whether the
+ // demangling had enough space or not
+ res = __cxxabiv1::__cxa_demangle( info.name( ), buf, &buflen, &status );
+
+ // throw exception if this goes wrong, because we don't want to have
+ // have code reacting on failed demangling or wrong type information!
+ if( status != 0 || res == NULL ) {
+ throw std::runtime_error( "__cxa_demangle failed!" );
+ }
+
+ // construct string on stack and return it, free original buffer
+ std::string s( res );
+ free( res );
+
+ return s;
+}
+
+#else
+
+#ifdef _WIN32
+
+// TODO: maybe extract into a generic stringutils module
+void replaceAll( std::string &s, const std::string &from, const std::string &to )
+{
+ if( from.empty( ) ) {
+ return;
+ }
+
+ size_t pos = 0;
+ while( ( pos = s.find( from, pos ) ) != std::string::npos ) {
+ s.replace( pos, from.length( ), to );
+ pos += to.length( );
+ }
+}
+
+std::string demangle( const std::type_info &info )
+{
+ std::string name = info.name( );
+
+ // MSVC marks metatypes, nice, but gcc doesn't, falling
+ // back as we can't do the same for gcc
+ replaceAll( name, "class ", "" );
+ // TODO: much more to follow, this is currently just enough
+ // for the module registry structure with base class
+ // signature only
+
+ return name;
+}
+
+#else
+
+#error "type.name() demangling not implemented!"
+
+#endif // _WIN32
+
+
+#endif // defined( __GNUG__ ) && defined( __GLIBCXX__ )
+
+#endif // __TYPEINFO_H
diff --git a/include/util/TypeList.hpp b/include/util/TypeList.hpp
new file mode 100644
index 0000000..bc8c49b
--- /dev/null
+++ b/include/util/TypeList.hpp
@@ -0,0 +1,28 @@
+#ifndef __TYPELIST_H
+#define __TYPELIST_H
+
+class NullType {};
+
+template< class T, class U >
+struct TypeList {
+ typedef T Head;
+ typedef U Tail;
+};
+
+#define TYPELIST_1( T1 ) TypeList< T1, NullType >
+#define TYPELIST_2( T1, T2 ) TypeList< T1, TYPELIST_1( T2 ) >
+#define TYPELIST_3( T1, T2, T3 ) TypeList< T1, TYPELIST_2( T2, T3 ) >
+#define TYPELIST_4( T1, T2, T3, T4 ) TypeList< T1, TYPELIST_3( T2, T3, T4 ) >
+
+template< class T> struct Length;
+template< > struct Length< NullType >
+{
+ enum { value = 0 };
+};
+template< class T, class U >
+struct Length< TypeList< T, U > >
+{
+ enum { value = 1 + Length< U >::value };
+};
+
+#endif
diff --git a/include/util/TypeTraits.hpp b/include/util/TypeTraits.hpp
new file mode 100644
index 0000000..b01051e
--- /dev/null
+++ b/include/util/TypeTraits.hpp
@@ -0,0 +1,9 @@
+#ifndef __TYPETRAITS_H
+#define __TYPETRAITS_H
+
+template< typename T >
+class TypeTraits {
+ typedef typename
+};
+
+#endif