removed boost exception from header, shaved 15 seconds off of build time (33%)

This commit is contained in:
Daniel Larimer 2012-09-14 22:47:37 -04:00
parent c12ac793b3
commit 66838956dd
12 changed files with 187 additions and 136 deletions

View file

@ -15,75 +15,75 @@ namespace fc {
*/
template<typename T>
struct datastream {
datastream( T start, uint32_t s )
:m_start(start),m_pos(start),m_end(start+s){};
datastream( T start, uint32_t s )
:m_start(start),m_pos(start),m_end(start+s){};
template<typename DATA>
inline datastream& operator<<(const DATA& d) {
static_assert( !fc::is_class<DATA>::value, "no serialization defined" );
write( (const char*)&d, sizeof(d) );
return *this;
}
template<typename DATA>
inline datastream& operator<<(const DATA& d) {
static_assert( !fc::is_class<DATA>::type::value, "no serialization defined" );
write( (const char*)&d, sizeof(d) );
return *this;
}
template<typename DATA>
inline datastream& operator>>(DATA& d) {
static_assert( !fc::is_class<DATA>::value, "no serialization defined" );
read((char*)&d, sizeof(d) );
return *this;
}
template<typename DATA>
inline datastream& operator>>(DATA& d) {
static_assert( !fc::is_class<DATA>::type::value, "no serialization defined" );
read((char*)&d, sizeof(d) );
return *this;
}
inline void skip( uint32_t s ){ m_pos += s; }
inline bool read( char* d, uint32_t s ) {
if( m_end - m_pos >= (int32_t)s ) {
memcpy( d, m_pos, s );
m_pos += s;
return true;
}
FC_THROW( "Attempt to read %d bytes beyond end of buffer of size %d", -((m_end-m_pos) - s),(m_end-m_start) );
return false;
}
inline void skip( uint32_t s ){ m_pos += s; }
inline bool read( char* d, uint32_t s ) {
if( m_end - m_pos >= (int32_t)s ) {
memcpy( d, m_pos, s );
m_pos += s;
return true;
}
FC_THROW_MSG( "Attempt to read %s bytes beyond end of buffer of size %s", -((m_end-m_pos) - s),(m_end-m_start) );
return false;
}
inline bool write( const char* d, uint32_t s ) {
if( m_end - m_pos >= (int32_t)s ) {
memcpy( m_pos, d, s );
m_pos += s;
return true;
}
FC_THROW( "Attempt to write %d bytes beyond end of buffer of size %d",%-((m_end-m_pos) - s),(m_end-m_start) );
return false;
}
inline bool write( const char* d, uint32_t s ) {
if( m_end - m_pos >= (int32_t)s ) {
memcpy( m_pos, d, s );
m_pos += s;
return true;
}
FC_THROW_MSG( "Attempt to write %s bytes beyond end of buffer of size %s", -((m_end-m_pos) - s),(m_end-m_start) );
return false;
}
inline bool put(char c) {
if( m_pos < m_end ) {
*m_pos = c;
++m_pos;
return true;
}
FC_THROW( "Attempt to write %d byte beyond end of buffer of size %d", -((m_end-m_pos) - 1), (m_end-m_start) );
return false;
}
inline bool put(char c) {
if( m_pos < m_end ) {
*m_pos = c;
++m_pos;
return true;
}
FC_THROW_MSG( "Attempt to write %s byte beyond end of buffer of size %s", -((m_end-m_pos) - 1), (m_end-m_start) );
return false;
}
inline bool get( unsigned char& c ) { return get( *(char*)&c ); }
inline bool get( char& c ) {
if( m_pos < m_end ) {
c = *m_pos;
++m_pos;
return true;
}
FC_THROW( "Attempt to read %d byte beyond end of buffer of size %d", -((m_end-m_pos) - 1), (m_end-m_start) );
++m_pos;
return false;
}
inline bool get( unsigned char& c ) { return get( *(char*)&c ); }
inline bool get( char& c ) {
if( m_pos < m_end ) {
c = *m_pos;
++m_pos;
return true;
}
FC_THROW_MSG( "Attempt to read %s byte beyond end of buffer of size %s", -((m_end-m_pos) - 1), (m_end-m_start) );
++m_pos;
return false;
}
T pos()const { return m_pos; }
inline bool valid()const { return m_pos <= m_end && m_pos >= m_start; }
inline bool seekp(uint32_t p) { m_pos = m_start + p; return m_pos <= m_end; }
inline uint32_t tellp()const { return m_pos - m_start; }
inline uint32_t remaining()const { return m_end - m_pos; }
private:
T m_start;
T m_pos;
T m_end;
T pos()const { return m_pos; }
inline bool valid()const { return m_pos <= m_end && m_pos >= m_start; }
inline bool seekp(uint32_t p) { m_pos = m_start + p; return m_pos <= m_end; }
inline uint32_t tellp()const { return m_pos - m_start; }
inline uint32_t remaining()const { return m_end - m_pos; }
private:
T m_start;
T m_pos;
T m_end;
};
template<>
@ -91,7 +91,7 @@ struct datastream<size_t> {
datastream( size_t init_size = 0):m_size(init_size){};
template<typename DATA>
inline datastream& operator<<(const DATA& d) {
static_assert( !fc::is_class<DATA>::value, "no serialzation defined" );
static_assert( !fc::is_class<DATA>::type::value, "no serialzation defined" );
m_size += sizeof(d);
return *this;
}

View file

@ -2,11 +2,10 @@
#define _FC_EXCEPTION_HPP_
#include <fc/shared_ptr.hpp>
#include <fc/string.hpp>
#include <boost/current_function.hpp>
// TODO: Remove boost exception dependency here!!
// TODO: Remove boost format dependency here!!
#include <boost/format.hpp>
#include <boost/exception/all.hpp>
// provided for easy integration with boost.
namespace boost { class exception_ptr; }
@ -51,18 +50,46 @@ namespace fc {
}
void rethrow_exception( const exception_ptr& e );
void throw_exception( const char* func, const char* file, int line, const char* msg );
void throw_exception( const char* func, const char* file, int line, const char* msg,
const fc::string& a1 );
void throw_exception( const char* func, const char* file, int line, const char* msg,
const fc::string& a1, const fc::string& a2 );
void throw_exception( const char* func, const char* file, int line, const char* msg,
const fc::string& a1, const fc::string& a2, const fc::string& a3 );
void throw_exception( const char* func, const char* file, int line, const char* msg,
const fc::string& a1, const fc::string& a2, const fc::string& a3, const fc::string& a4 );
template<typename T>
fc::string to_string( T&& v ) { return fc::string(fc::forward<T>(v)); }
fc::string to_string( char v ); // { return fc::string(&v,1); }
fc::string to_string( uint64_t v );
fc::string to_string( int64_t v );
fc::string to_string( double v );
fc::string to_string( float v );
fc::string to_string( int32_t v );
fc::string to_string( uint32_t v );
fc::string to_string( int16_t v );
fc::string to_string( uint16_t v );
fc::string to_string( size_t v );
fc::string to_string( long int v );
template<typename T>
void throw_exception( const char* func, const char* file, int line, const char* msg, T&& a1 ) {
throw_exception( func, file, line, msg, to_string(fc::forward<T>(a1) ) );
}
template<typename T1, typename T2>
void throw_exception( const char* func, const char* file, int line, const char* msg, T1&& a1, T2&& a2 ) {
throw_exception( func, file, line, msg, to_string(fc::forward<T1>(a1) ), to_string( fc::forward<T2>(a2) ) );
}
typedef boost::error_info<struct err_msg_,std::string> err_msg;
struct exception : public virtual boost::exception, public virtual std::exception {
const char* what()const throw() { return "exception"; }
virtual void rethrow()const { BOOST_THROW_EXCEPTION(*this); }
const std::string& message()const { return *boost::get_error_info<fc::err_msg>(*this); }
};
} // namespace fc
#define FC_THROW(X,...) throw X
#define FC_THROW_MSG( MSG, ... ) \
#define FC_THROW(X) throw X
#define FC_THROW_MSG( ... ) \
do { \
BOOST_THROW_EXCEPTION( fc::exception() << fc::err_msg( (boost::format( MSG ) __VA_ARGS__ ).str() ) );\
fc::throw_exception( BOOST_CURRENT_FUNCTION, __FILE__, __LINE__, __VA_ARGS__ ); \
} while(0)

View file

@ -64,7 +64,7 @@ namespace fc {
private:
// force alignment... to 8 byte boundaries
double _value[(sizeof(T)+sizeof(double)-1)/sizeof(double)];
double _value[8 * ((sizeof(T)+7)/8)];
bool _valid;
};

View file

@ -169,6 +169,7 @@ namespace fc {
template<typename T>
class reflector : public detail::reflector_impl<T, reflector<T> >{
public:
enum _is_defined { is_defined = 0 };
virtual const char* name()const { return get_typename<T>::name(); }
virtual void visit( void* s, const abstract_visitor& v )const {
v.visit( *((T*)s) );

View file

@ -21,6 +21,7 @@ namespace fc {
namespace fc{ \
template<> class reflector<TYPE> : virtual public detail::reflector_impl<TYPE, reflector<TYPE> > { \
public:\
enum _is_defined { is_defined = 1 }; \
virtual const char* name()const; \
virtual void visit( void* s, const abstract_visitor& v )const; \
virtual void visit( const void* s, const abstract_const_visitor& v )const; \

View file

@ -1,6 +1,6 @@
/**
* @file mace/reflect/reflect.hpp
* @file fc/static_reflect.hpp
*
* @brief Defines types and macros used to provide reflection.
*
@ -16,28 +16,13 @@
#include <boost/preprocessor/stringize.hpp>
#include <boost/preprocessor/tuple/elem.hpp>
#include <boost/preprocessor/facilities/empty.hpp>
#include <boost/type_traits/remove_const.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/typeof/typeof.hpp>
#include <boost/type_traits/remove_pointer.hpp>
#include <string>
#include <typeinfo>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <stdint.h>
#include <boost/fusion/container/vector.hpp>
#include <boost/function_types/result_type.hpp>
//#include <mace/void.hpp>
//#include <mace/reflect/typeinfo.hpp>
namespace mace {
/**
* @brief types, methods associated with the MACE.Reflect Library
*/
namespace reflect {
namespace fc {
/**
* @brief defines visit functions for T
@ -49,11 +34,10 @@ namespace reflect {
* class for your type.
*/
template<typename T>
struct reflector{
struct static_reflector{
typedef T type;
typedef boost::fusion::vector<> base_class_types;
typedef boost::false_type is_defined;
typedef boost::false_type is_enum;
typedef fc::false_type is_defined;
typedef fc::false_type is_enum;
/**
* @tparam Visitor a function object of the form:
@ -83,7 +67,7 @@ struct reflector{
#endif // DOXYGEN
};
} } // namespace mace::reflect
} // namespace fc
#ifndef DOXYGEN
@ -97,9 +81,9 @@ struct reflector{
#else
#define TEMPLATE
#endif
#include <boost/typeof/typeof.hpp>
#define FC_STATIC_REFLECT_VISIT_MEMBER( r, visitor, elem ) \
visitor.TEMPLATE operator()<BOOST_TYPEOF(&type::elem), &type::elem>( BOOST_PP_STRINGIZE(elem) );
visitor.TEMPLATE operator()<decltype(((type*)0)->elem), type, &type::elem>( BOOST_PP_STRINGIZE(elem) );
#define FC_STATIC_REFLECT_BASE_MEMBER_COUNT( r, OP, elem ) \
@ -131,12 +115,10 @@ void fc::static_reflector<TYPE>::visit( const Visitor& v ) { \
if( strcmp( s, BOOST_PP_STRINGIZE(elem) ) == 0 ) return elem;
#define FC_STATIC_REFLECT_ENUM( ENUM, FIELDS ) \
FC_STATIC_REFLECT_TYPEINFO(ENUM) \
namespace mace { namespace reflect { \
template<> struct reflector<ENUM> { \
typedef boost::true_type is_defined; \
typedef boost::true_type is_enum; \
typedef boost::fusion::vector<> base_class_types; \
namespace fc { \
template<> struct static_reflector<ENUM> { \
typedef fc::true_type is_defined; \
typedef fc::true_type is_enum; \
template<typename Visitor> \
static inline void visit( const Visitor& v ) { \
BOOST_PP_SEQ_FOR_EACH( FC_STATIC_REFLECT_VISIT_ENUM, v, FIELDS ) \
@ -145,12 +127,12 @@ template<> struct reflector<ENUM> { \
switch( ENUM(i) ) { \
BOOST_PP_SEQ_FOR_EACH( FC_STATIC_REFLECT_ENUM_TO_STRING, v, FIELDS ) \
default: \
FC_STATIC_REFLECT_THROW( mace::reflect::unknown_field(), "%1% not in enum '%2%'", %i %BOOST_PP_STRINGIZE(ENUM) ); \
FC_STATIC_REFLECT_THROW( fc::reflect::unknown_field(), "%1% not in enum '%2%'", %i %BOOST_PP_STRINGIZE(ENUM) ); \
}\
} \
static ENUM from_string( const char* s ) { \
BOOST_PP_SEQ_FOR_EACH( FC_STATIC_REFLECT_ENUM_FROM_STRING, v, FIELDS ) \
FC_STATIC_REFLECT_THROW( mace::reflect::unknown_field(), "%1% in enum %2%", %s %BOOST_PP_STRINGIZE(ENUM) ); \
FC_STATIC_REFLECT_THROW( fc::reflect::unknown_field(), "%1% in enum %2%", %s %BOOST_PP_STRINGIZE(ENUM) ); \
} \
}; \
} }
@ -167,12 +149,11 @@ template<> struct reflector<ENUM> { \
* @param MEMBERS - a sequence of member names. (field1)(field2)(field3)
*/
#define FC_STATIC_REFLECT_DERIVED( TYPE, INHERITS, MEMBERS ) \
FC_STATIC_REFLECT_TYPEINFO(TYPE) \
namespace fc { \
template<> struct static_reflector<TYPE> {\
typedef TYPE type; \
typedef boost::true_type is_defined; \
typedef boost::false_type is_enum; \
typedef fc::true_type is_defined; \
typedef fc::false_type is_enum; \
enum member_count_enum { \
local_member_count = BOOST_PP_SEQ_SIZE(MEMBERS), \
total_member_count = local_member_count BOOST_PP_SEQ_FOR_EACH( FC_STATIC_REFLECT_BASE_MEMBER_COUNT, +, INHERITS )\
@ -193,18 +174,16 @@ template<> struct static_reflector<TYPE> {\
FC_STATIC_REFLECT_DERIVED( TYPE, BOOST_PP_SEQ_NIL, MEMBERS )
#define FC_STATIC_REFLECT_FWD( TYPE ) \
FC_STATIC_REFLECT_TYPEINFO(TYPE) \
namespace mace { namespace reflect { \
namespace fc { \
template<> struct static_reflector<TYPE> {\
typedef TYPE type; \
typedef boost::true_type is_defined; \
typedef fc::true_type is_defined; \
enum member_count_enum { \
local_member_count = BOOST_PP_SEQ_SIZE(MEMBERS), \
total_member_count = local_member_count BOOST_PP_SEQ_FOR_EACH( FC_STATIC_REFLECT_BASE_MEMBER_COUNT, +, INHERITS )\
}; \
typedef boost::fusion::vector<BOOST_PP_SEQ_ENUM(INHERITS)> base_class_types; \
template<typename Visitor> static void visit( const Visitor& v ); \
}; } }
}; }
#define FC_STATIC_REFLECT_DERIVED_IMPL( TYPE, MEMBERS ) \

View file

@ -20,6 +20,7 @@ namespace fc {
string( const string& c );
string( string&& c );
string( const char* c );
string( const char* c, int s );
string( const_iterator b, const_iterator e );
~string();

View file

@ -22,14 +22,16 @@ namespace fc {
template<typename T, typename U>
inline T&& forward( U&& u ) { return static_cast<T&&>(u); }
struct true_type { enum _value { value = 1 }; };
struct false_type { enum _value { value = 0 }; };
namespace detail {
template<typename T> char is_class_helper(void(T::*)());
template<typename T> double is_class_helper(...);
template<typename T> fc::true_type is_class_helper(void(T::*)());
template<typename T> fc::false_type is_class_helper(...);
}
template<typename T>
struct is_class {
enum { value = sizeof(char) == sizeof(detail::is_class_helper<T>(0)) };
};
struct is_class { typedef decltype(detail::is_class_helper<T>(0)) type; };
template<typename T>
void swap( T& a, T& b ) {

View file

@ -52,7 +52,7 @@ namespace fc {
data(){};
};
template<typename T, bool IsClass=false>
template<typename T, typename IsClass=fc::false_type>
struct vector_impl {
public:
vector_impl():_data(nullptr){}
@ -170,7 +170,7 @@ namespace fc {
};
template<typename T>
class vector_impl<T,true> {
class vector_impl<T,fc::true_type> {
public:
vector_impl():_data(nullptr){}
vector_impl( vector_impl&& c):_data(c._data){c._data =nullptr; }
@ -338,12 +338,12 @@ namespace fc {
}
template<typename T>
class vector : public detail::vector_impl<T, fc::is_class<T>::value> {
class vector : public detail::vector_impl<T, typename fc::is_class<T>::type> {
public:
vector(){}
vector( uint64_t s ):detail::vector_impl<T, fc::is_class<T>::value>(s){}
vector( const vector& v ):detail::vector_impl<T, fc::is_class<T>::value>(v){}
vector( vector&& v ):detail::vector_impl<T, fc::is_class<T>::value>(fc::move(v)){}
vector( uint64_t s ):detail::vector_impl<T, typename fc::is_class<T>::type>(s){}
vector( const vector& v ):detail::vector_impl<T, typename fc::is_class<T>::type>(v){}
vector( vector&& v ):detail::vector_impl<T, typename fc::is_class<T>::type>(fc::move(v)){}
vector& operator=( vector&& v ) {
*((base*)this) = fc::move(v);
@ -354,7 +354,7 @@ namespace fc {
return *this;
}
private:
typedef detail::vector_impl<T, fc::is_class<T>::value> base;
typedef detail::vector_impl<T, typename fc::is_class<T>::type> base;
};
};

View file

@ -1,5 +1,7 @@
#include <fc/exception.hpp>
#include <fc/error.hpp>
#include <boost/exception/all.hpp>
#include <boost/lexical_cast.hpp>
namespace fc {
#define bexcept void* e = &my[0]; (*((boost::exception_ptr*)e))
@ -66,4 +68,38 @@ namespace fc {
const void* e = &my[0];
return (*((boost::exception_ptr*)e));
}
fc::string to_string( char v ) { return fc::string(&v,1); }
fc::string to_string( uint64_t v ) { return boost::lexical_cast<std::string>(v).c_str(); }
fc::string to_string( int64_t v ){ return boost::lexical_cast<std::string>(v).c_str(); }
fc::string to_string( double v ){ return boost::lexical_cast<std::string>(v).c_str(); }
fc::string to_string( float v ){ return boost::lexical_cast<std::string>(v).c_str(); }
fc::string to_string( int32_t v ){ return boost::lexical_cast<std::string>(v).c_str(); }
fc::string to_string( uint32_t v ){ return boost::lexical_cast<std::string>(v).c_str(); }
fc::string to_string( int16_t v ){ return boost::lexical_cast<std::string>(v).c_str(); }
fc::string to_string( uint16_t v ){ return boost::lexical_cast<std::string>(v).c_str(); }
fc::string to_string( size_t v ){ return boost::lexical_cast<std::string>(v).c_str(); }
fc::string to_string( long int v ){ return boost::lexical_cast<std::string>(v).c_str(); }
void throw_exception( const char* func, const char* file, int line, const char* msg ) {
::boost::exception_detail::throw_exception_(fc::generic_exception(msg),func, file, line );
}
void throw_exception( const char* func, const char* file, int line, const char* msg,
const fc::string& a1 ) {
::boost::exception_detail::throw_exception_(fc::generic_exception(msg),func, file, line );
}
void throw_exception( const char* func, const char* file, int line, const char* msg,
const fc::string& a1, const fc::string& a2 ) {
::boost::exception_detail::throw_exception_(fc::generic_exception(msg),func, file, line );
}
void throw_exception( const char* func, const char* file, int line, const char* msg,
const fc::string& a1, const fc::string& a2, const fc::string& a3 ) {
::boost::exception_detail::throw_exception_(fc::generic_exception(msg),func, file, line );
}
void throw_exception( const char* func, const char* file, int line, const char* msg,
const fc::string& a1, const fc::string& a2, const fc::string& a3, const fc::string& a4 ) {
::boost::exception_detail::throw_exception_(fc::generic_exception(msg),func, file, line );
}
}

View file

@ -10,7 +10,7 @@ namespace fc {
return c - 'a' + 10;
if( c >= 'A' && c <= 'F' )
return c - 'A' + 10;
FC_THROW( "Invalid hex character '%c'", c );
FC_THROW_MSG( "Invalid hex character '%s'", c );
return 0;
}

View file

@ -15,6 +15,10 @@ namespace detail {
namespace fc {
string::string(const char* s, int l) {
static_assert( sizeof(*this) >= sizeof(std::string), "failed to reserve enough space" );
new (this) std::string(s,l);
}
string::string() {
static_assert( sizeof(*this) >= sizeof(std::string), "failed to reserve enough space" );
new (this) std::string();