mirror of
https://github.com/boostorg/system.git
synced 2025-07-30 04:27:14 +02:00
Avoid allocation on interop for system/generic categories; fix DLL interoperability on msvc-14.x by poking into std::system_category::_Addr
This commit is contained in:
@ -30,8 +30,20 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
explicit std_category( boost::system::error_category const * pc ): pc_( pc )
|
explicit std_category( boost::system::error_category const * pc, unsigned id ): pc_( pc )
|
||||||
{
|
{
|
||||||
|
if( id != 0 )
|
||||||
|
{
|
||||||
|
#if defined(_MSC_VER) && defined(_CPPLIB_VER) && _MSC_VER >= 1900 && _MSC_VER < 2000
|
||||||
|
|
||||||
|
// Poking into the protected _Addr member of std::error_category
|
||||||
|
// is not a particularly good programming practice, but what can
|
||||||
|
// you do
|
||||||
|
|
||||||
|
_Addr = id;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual const char * name() const BOOST_NOEXCEPT
|
virtual const char * name() const BOOST_NOEXCEPT
|
||||||
@ -55,24 +67,45 @@ public:
|
|||||||
|
|
||||||
inline std::error_category const & to_std_category( boost::system::error_category const & cat ) BOOST_SYMBOL_VISIBLE;
|
inline std::error_category const & to_std_category( boost::system::error_category const & cat ) BOOST_SYMBOL_VISIBLE;
|
||||||
|
|
||||||
|
struct cat_ptr_less
|
||||||
|
{
|
||||||
|
bool operator()( boost::system::error_category const * p1, boost::system::error_category const * p2 ) const BOOST_NOEXCEPT
|
||||||
|
{
|
||||||
|
return *p1 < *p2;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
inline std::error_category const & to_std_category( boost::system::error_category const & cat )
|
inline std::error_category const & to_std_category( boost::system::error_category const & cat )
|
||||||
{
|
{
|
||||||
typedef std::map< boost::system::error_category const *, std::unique_ptr<std_category> > map_type;
|
if( cat == boost::system::system_category() )
|
||||||
|
|
||||||
static map_type map_;
|
|
||||||
|
|
||||||
map_type::iterator i = map_.find( &cat );
|
|
||||||
|
|
||||||
if( i == map_.end() )
|
|
||||||
{
|
{
|
||||||
std::unique_ptr<std_category> p( new std_category( &cat ) );
|
static const std_category system_instance( &cat, 0x1F4D7 );
|
||||||
|
return system_instance;
|
||||||
std::pair<map_type::iterator, bool> r = map_.insert( map_type::value_type( &cat, std::move( p ) ) );
|
|
||||||
|
|
||||||
i = r.first;
|
|
||||||
}
|
}
|
||||||
|
else if( cat == boost::system::generic_category() )
|
||||||
|
{
|
||||||
|
static const std_category generic_instance( &cat, 0x1F4D3 );
|
||||||
|
return generic_instance;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
typedef std::map< boost::system::error_category const *, std::unique_ptr<std_category>, cat_ptr_less > map_type;
|
||||||
|
|
||||||
return *i->second;
|
static map_type map_;
|
||||||
|
|
||||||
|
map_type::iterator i = map_.find( &cat );
|
||||||
|
|
||||||
|
if( i == map_.end() )
|
||||||
|
{
|
||||||
|
std::unique_ptr<std_category> p( new std_category( &cat, 0 ) );
|
||||||
|
|
||||||
|
std::pair<map_type::iterator, bool> r = map_.insert( map_type::value_type( &cat, std::move( p ) ) );
|
||||||
|
|
||||||
|
i = r.first;
|
||||||
|
}
|
||||||
|
|
||||||
|
return *i->second;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool std_category::equivalent( int code, const std::error_condition & condition ) const BOOST_NOEXCEPT
|
inline bool std_category::equivalent( int code, const std::error_condition & condition ) const BOOST_NOEXCEPT
|
||||||
|
@ -68,4 +68,4 @@ lib std_single_instance_lib2 : std_single_instance_2.cpp : <link>shared:<define>
|
|||||||
|
|
||||||
system-run std_single_instance_test.cpp std_single_instance_1.cpp std_single_instance_2.cpp ;
|
system-run std_single_instance_test.cpp std_single_instance_1.cpp std_single_instance_2.cpp ;
|
||||||
run std_single_instance_test.cpp std_single_instance_lib1 std_single_instance_lib2 : : : <link>static : std_single_instance_lib_static ;
|
run std_single_instance_test.cpp std_single_instance_lib1 std_single_instance_lib2 : : : <link>static : std_single_instance_lib_static ;
|
||||||
run std_single_instance_test.cpp std_single_instance_lib1 std_single_instance_lib2 : : : <link>shared : std_single_instance_lib_shared ;
|
run std_single_instance_test.cpp std_single_instance_lib1 std_single_instance_lib2 : : : <link>shared <define>STD_SINGLE_INSTANCE_SHARED : std_single_instance_lib_shared ;
|
||||||
|
@ -31,4 +31,10 @@ EXPORT std::error_code get_generic_code()
|
|||||||
|
|
||||||
} // namespace lib1
|
} // namespace lib1
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
EXPORT void lib1_f()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -31,4 +31,10 @@ EXPORT std::error_code get_generic_code()
|
|||||||
|
|
||||||
} // namespace lib2
|
} // namespace lib2
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
EXPORT void lib2_f()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -4,11 +4,31 @@
|
|||||||
|
|
||||||
#include <boost/system/error_code.hpp>
|
#include <boost/system/error_code.hpp>
|
||||||
#include <boost/config/pragma_message.hpp>
|
#include <boost/config/pragma_message.hpp>
|
||||||
|
#include <boost/config/helper_macros.hpp>
|
||||||
|
|
||||||
#if !defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR)
|
#if !defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR)
|
||||||
|
|
||||||
BOOST_PRAGMA_MESSAGE( "BOOST_SYSTEM_HAS_SYSTEM_ERROR not defined, test will be skipped" )
|
BOOST_PRAGMA_MESSAGE( "BOOST_SYSTEM_HAS_SYSTEM_ERROR not defined, test will be skipped" )
|
||||||
|
int main() {}
|
||||||
|
|
||||||
|
#elif defined(STD_SINGLE_INSTANCE_SHARED) && defined(__CYGWIN__)
|
||||||
|
|
||||||
|
BOOST_PRAGMA_MESSAGE( "Skipping Windows/DLL test, __CYGWIN__" )
|
||||||
|
int main() {}
|
||||||
|
|
||||||
|
#elif defined(STD_SINGLE_INSTANCE_SHARED) && defined(_WIN32) && !defined(_MSC_VER)
|
||||||
|
|
||||||
|
BOOST_PRAGMA_MESSAGE( "Skipping Windows/DLL test, no _MSC_VER" )
|
||||||
|
int main() {}
|
||||||
|
|
||||||
|
#elif defined(STD_SINGLE_INSTANCE_SHARED) && defined(_WIN32) && !defined(_CPPLIB_VER)
|
||||||
|
|
||||||
|
BOOST_PRAGMA_MESSAGE( "Skipping Windows/DLL test, no _CPPLIB_VER" )
|
||||||
|
int main() {}
|
||||||
|
|
||||||
|
#elif defined(STD_SINGLE_INSTANCE_SHARED) && defined(_WIN32) && (_MSC_VER < 1900 || _MSC_VER >= 2000)
|
||||||
|
|
||||||
|
BOOST_PRAGMA_MESSAGE( "Skipping Windows/DLL test, _MSC_VER is " BOOST_STRINGIZE(_MSC_VER) )
|
||||||
int main() {}
|
int main() {}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
Reference in New Issue
Block a user