Reorganize some MPL and Utility classes and files

This commit is contained in:
Vinnie Falco
2013-09-19 21:37:47 -07:00
parent 69c26a180e
commit 7efb6a3ab8
16 changed files with 456 additions and 347 deletions

View File

@@ -22,6 +22,7 @@
#include "mpl/AddConst.h"
#include "mpl/IfCond.h"
#include "mpl/IsCallPossible.h"
#include "mpl/PointerToOther.h"
#include "mpl/RemoveConst.h"
#include "mpl/RemoveConstVolatile.h"

27
beast/Utility.h Normal file
View File

@@ -0,0 +1,27 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef BEAST_UTILITY_H_INCLUDED
#define BEAST_UTILITY_H_INCLUDED
#include "utility/EnableIf.h"
#include "utility/Journal.h"
#endif

270
beast/mpl/IsCallPossible.h Normal file
View File

@@ -0,0 +1,270 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef BEAST_MPL_ISCALLPOSSIBLE_H_INCLUDED
#define BEAST_MPL_ISCALLPOSSIBLE_H_INCLUDED
namespace beast {
namespace mpl {
// inspired by Roman Perepelitsa's presentation from comp.lang.c++.moderated
// based on the implementation here: http://www.rsdn.ru/forum/cpp/2759773.1.aspx
//
namespace is_call_possible_detail
{
template<typename T>
struct add_reference
{
typedef T& type;
};
template<typename T>
struct add_reference<T&>
{
typedef T& type;
};
template <typename T> class void_exp_result {};
template <typename T, typename U>
U const& operator,(U const&, void_exp_result<T>);
template <typename T, typename U>
U& operator,(U&, void_exp_result<T>);
template <typename src_type, typename dest_type>
struct clone_constness
{
typedef dest_type type;
};
template <typename src_type, typename dest_type>
struct clone_constness<const src_type, dest_type>
{
typedef const dest_type type;
};
}
#define BEAST_DEFINE_HAS_MEMBER_FUNCTION(trait_name, member_function_name) \
template<typename T, typename Signature> class trait_name; \
\
template<typename T, typename Result> \
class trait_name<T, Result(void)> \
{ \
class yes { char m; }; \
class no { yes m[2]; }; \
struct base_mixin \
{ \
Result member_function_name(); \
}; \
struct base : public T, public base_mixin { private: base(); }; \
template <typename U, U t> class helper{}; \
template <typename U> \
static no deduce(U*, helper<Result (base_mixin::*)(), &U::member_function_name>* = 0); \
static yes deduce(...); \
public: \
static const bool value = sizeof(yes) == sizeof(deduce(static_cast<base*>(0))); \
}; \
\
template<typename T, typename Result, typename Arg> \
class trait_name<T, Result(Arg)> \
{ \
class yes { char m; }; \
class no { yes m[2]; }; \
struct base_mixin \
{ \
Result member_function_name(Arg); \
}; \
struct base : public T, public base_mixin { private: base(); }; \
template <typename U, U t> class helper{}; \
template <typename U> \
static no deduce(U*, helper<Result (base_mixin::*)(Arg), &U::member_function_name>* = 0); \
static yes deduce(...); \
public: \
static const bool value = sizeof(yes) == sizeof(deduce(static_cast<base*>(0))); \
}; \
\
template<typename T, typename Result, typename Arg1, typename Arg2> \
class trait_name<T, Result(Arg1,Arg2)> \
{ \
class yes { char m; }; \
class no { yes m[2]; }; \
struct base_mixin \
{ \
Result member_function_name(Arg1,Arg2); \
}; \
struct base : public T, public base_mixin { private: base(); }; \
template <typename U, U t> class helper{}; \
template <typename U> \
static no deduce(U*, helper<Result (base_mixin::*)(Arg1,Arg2), &U::member_function_name>* = 0); \
static yes deduce(...); \
public: \
static const bool value = sizeof(yes) == sizeof(deduce(static_cast<base*>(0))); \
}; \
\
template<typename T, typename Result, typename Arg1, typename Arg2, typename Arg3> \
class trait_name<T, Result(Arg1,Arg2,Arg3)> \
{ \
class yes { char m; }; \
class no { yes m[2]; }; \
struct base_mixin \
{ \
Result member_function_name(Arg1,Arg2,Arg3); \
}; \
struct base : public T, public base_mixin { private: base(); }; \
template <typename U, U t> class helper{}; \
template <typename U> \
static no deduce(U*, helper<Result (base_mixin::*)(Arg1,Arg2,Arg3), &U::member_function_name>* = 0); \
static yes deduce(...); \
public: \
static const bool value = sizeof(yes) == sizeof(deduce(static_cast<base*>(0))); \
}; \
\
template<typename T, typename Result, typename Arg1, typename Arg2, typename Arg3, typename Arg4> \
class trait_name<T, Result(Arg1,Arg2,Arg3,Arg4)> \
{ \
class yes { char m; }; \
class no { yes m[2]; }; \
struct base_mixin \
{ \
Result member_function_name(Arg1,Arg2,Arg3,Arg4); \
}; \
struct base : public T, public base_mixin { private: base(); }; \
template <typename U, U t> class helper{}; \
template <typename U> \
static no deduce(U*, helper<Result (base_mixin::*)(Arg1,Arg2,Arg3,Arg4), &U::member_function_name>* = 0); \
static yes deduce(...); \
public: \
static const bool value = sizeof(yes) == sizeof(deduce(static_cast<base*>(0))); \
}
#define BEAST_DEFINE_IS_CALL_POSSIBLE(trait_name, member_function_name) \
struct trait_name##_detail \
{ \
BEAST_DEFINE_HAS_MEMBER_FUNCTION(has_member, member_function_name); \
}; \
\
template <typename T, typename Signature> \
struct trait_name \
{ \
private: \
class yes {}; \
class no { yes m[2]; }; \
struct derived : public T \
{ \
using T::member_function_name; \
no member_function_name(...) const; \
private: derived (); \
}; \
\
typedef typename beast::mpl::is_call_possible_detail::clone_constness<T, derived>::type derived_type; \
\
template <typename U, typename Result> \
struct return_value_check \
{ \
static yes deduce(Result); \
static no deduce(...); \
static no deduce(no); \
static no deduce(beast::mpl::is_call_possible_detail::void_exp_result<T>); \
}; \
\
template <typename U> \
struct return_value_check<U, void> \
{ \
static yes deduce(...); \
static no deduce(no); \
}; \
\
template <bool has_the_member_of_interest, typename F> \
struct impl \
{ \
static const bool value = false; \
}; \
\
template <typename Result, typename Arg> \
struct impl<true, Result(Arg)> \
{ \
static typename beast::mpl::is_call_possible_detail::add_reference<derived_type>::type test_me; \
static typename beast::mpl::is_call_possible_detail::add_reference<Arg>::type arg; \
\
static const bool value = \
sizeof( \
return_value_check<T, Result>::deduce( \
(test_me.member_function_name(arg), beast::mpl::is_call_possible_detail::void_exp_result<T>()) \
) \
) == sizeof(yes); \
}; \
\
template <typename Result, typename Arg1, typename Arg2> \
struct impl<true, Result(Arg1,Arg2)> \
{ \
static typename beast::mpl::is_call_possible_detail::add_reference<derived_type>::type test_me; \
static typename beast::mpl::is_call_possible_detail::add_reference<Arg1>::type arg1; \
static typename beast::mpl::is_call_possible_detail::add_reference<Arg2>::type arg2; \
\
static const bool value = \
sizeof( \
return_value_check<T, Result>::deduce( \
(test_me.member_function_name(arg1,arg2), beast::mpl::is_call_possible_detail::void_exp_result<T>()) \
) \
) == sizeof(yes); \
}; \
\
template <typename Result, typename Arg1, typename Arg2, typename Arg3> \
struct impl<true, Result(Arg1,Arg2,Arg3)> \
{ \
static typename beast::mpl::is_call_possible_detail::add_reference<derived_type>::type test_me; \
static typename beast::mpl::is_call_possible_detail::add_reference<Arg1>::type arg1; \
static typename beast::mpl::is_call_possible_detail::add_reference<Arg2>::type arg2; \
static typename beast::mpl::is_call_possible_detail::add_reference<Arg3>::type arg3; \
\
static const bool value = \
sizeof( \
return_value_check<T, Result>::deduce( \
(test_me.member_function_name(arg1,arg2,arg3), beast::mpl::is_call_possible_detail::void_exp_result<T>()) \
) \
) == sizeof(yes); \
}; \
\
template <typename Result, typename Arg1, typename Arg2, typename Arg3, typename Arg4> \
struct impl<true, Result(Arg1,Arg2,Arg3,Arg4)> \
{ \
static typename beast::mpl::is_call_possible_detail::add_reference<derived_type>::type test_me; \
static typename beast::mpl::is_call_possible_detail::add_reference<Arg1>::type arg1; \
static typename beast::mpl::is_call_possible_detail::add_reference<Arg2>::type arg2; \
static typename beast::mpl::is_call_possible_detail::add_reference<Arg3>::type arg3; \
static typename beast::mpl::is_call_possible_detail::add_reference<Arg4>::type arg4; \
\
static const bool value = \
sizeof( \
return_value_check<T, Result>::deduce( \
(test_me.member_function_name(arg1,arg2,arg3,arg4), \
beast::mpl::is_call_possible_detail::void_exp_result<T>()) \
) \
) == sizeof(yes); \
}; \
\
public: \
static const bool value = impl<trait_name##_detail::has_member<T,Signature>::value, Signature>::value; \
}
}
}
#endif

View File

@@ -24,6 +24,7 @@
#include <istream>
#include <ostream>
#include "../CStdInt.h"
#include "../mpl/IfCond.h"
namespace beast

View File

@@ -19,6 +19,8 @@
#include "BeastConfig.h"
#include "../../modules/beast_core/beast_core.h"
#include "../../modules/beast_core/beast_core.h" // for UnitTest
#include "../Config.h"
#include "impl/IPEndpoint.cpp"

View File

@@ -26,10 +26,10 @@ namespace beast
{
template <bool Enable, class T = void>
struct EnableIfBool : FalseType { typedef T type; };
struct EnableIfBool : TrueType { typedef T type; };
template <class T>
struct EnableIfBool <false, T> { };
struct EnableIfBool <false, T> : FalseType { };
template <class Cond, class T = void>
struct EnableIf : public EnableIfBool <Cond::value, T> { };

168
beast/utility/Journal.h Normal file
View File

@@ -0,0 +1,168 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef BEAST_UTILITY_JOURNAL_H_INCLUDED
#define BEAST_UTILITY_JOURNAL_H_INCLUDED
#include <sstream>
namespace beast
{
/** A generic endpoint for log messages. */
class Journal
{
public:
class Sink;
private:
Sink* m_sink;
public:
/** Severity level of the message. */
enum Severity
{
kTrace,
kDebug,
kInfo,
kWarning,
kError,
kFatal
};
//--------------------------------------------------------------------------
/** Abstraction for the underlying message destination. */
class Sink
{
public:
virtual ~Sink () { }
/** Write text to the sink at the specified severity. */
virtual void write (Severity severity, std::string const& text) = 0;
/** Returns `true` if text at the passed severity produces output.
The default implementation always returns `true`.
*/
virtual bool active (Severity);
};
/** Returns a Sink which does nothing. */
static Sink& getNullSink ();
//--------------------------------------------------------------------------
class Stream;
/** Scoped ostream-based container for writing messages to a Journal. */
class ScopedStream
{
public:
explicit ScopedStream (Stream const& stream);
ScopedStream (ScopedStream const& other);
template <typename T>
ScopedStream (Stream const& stream, T const& t)
: m_sink (stream.sink())
, m_severity (stream.severity())
{
m_ostream << t;
}
ScopedStream (Stream const& stream, std::ostream& manip (std::ostream&));
~ScopedStream ();
std::ostringstream& ostream () const;
std::ostream& operator<< (std::ostream& manip (std::ostream&)) const;
template <typename T>
std::ostream& operator<< (T const& t) const
{
return m_ostream << t;
}
private:
ScopedStream& operator= (ScopedStream const&); // disallowed
Sink& m_sink;
Severity const m_severity;
std::ostringstream mutable m_ostream;
};
//--------------------------------------------------------------------------
class Stream
{
public:
/** Construct a stream which produces no logging output. */
Stream ();
Stream (Sink& sink, Severity severity);
Stream (Stream const& other);
Stream& operator= (Stream const& other);
/** Returns `true` if the sink logs messages at the severity of this stream. */
bool active() const;
Sink& sink() const;
Severity severity() const;
ScopedStream operator<< (std::ostream& manip (std::ostream&)) const;
template <typename T>
ScopedStream operator<< (T const& t) const
{
return ScopedStream (*this, t);
}
private:
Sink* m_sink;
Severity m_severity;
};
//--------------------------------------------------------------------------
Journal ();
explicit Journal (Sink& sink);
Journal (Journal const& other);
~Journal ();
/** Returns a stream for this sink, with the specified severity. */
Stream stream (Severity severity) const;
/** Returns `true` if the sink logs messages at that severity. */
bool active (Severity severity) const;
/** Convenience sink streams for each severity level. */
Stream const trace;
Stream const debug;
Stream const info;
Stream const warning;
Stream const error;
Stream const fatal;
private:
Journal& operator= (Journal const& other); // disallowed
};
}
#endif

24
beast/utility/Utility.cpp Normal file
View File

@@ -0,0 +1,24 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include "BeastConfig.h"
#include "../../modules/beast_core/beast_core.h"
#include "impl/Journal.cpp"

View File

@@ -0,0 +1,189 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include "../Journal.h"
namespace beast
{
bool Journal::Sink::active (Severity)
{
return true;
}
//------------------------------------------------------------------------------
Journal::Sink& Journal::getNullSink ()
{
// A Sink that does nothing.
class NullSink : public Sink
{
public:
void write (Severity, std::string const&)
{
}
bool active (Severity)
{
return false;
}
};
return *SharedSingleton <NullSink>::get (
SingletonLifetime::neverDestroyed);
}
//------------------------------------------------------------------------------
Journal::ScopedStream::ScopedStream (Stream const& stream)
: m_sink (stream.sink())
, m_severity (stream.severity())
{
}
Journal::ScopedStream::ScopedStream (ScopedStream const& other)
: m_sink (other.m_sink)
, m_severity (other.m_severity)
{
}
Journal::ScopedStream::ScopedStream (Stream const& stream, std::ostream& manip (std::ostream&))
: m_sink (stream.sink())
, m_severity (stream.severity())
{
m_ostream << manip;
}
Journal::ScopedStream::~ScopedStream ()
{
if (m_sink.active (m_severity))
{
if (! m_ostream.str().empty())
m_sink.write (m_severity, m_ostream.str());
}
}
std::ostream& Journal::ScopedStream::operator<< (std::ostream& manip (std::ostream&)) const
{
return m_ostream << manip;
}
std::ostringstream& Journal::ScopedStream::ostream () const
{
return m_ostream;
}
//------------------------------------------------------------------------------
Journal::Stream::Stream ()
: m_sink (&getNullSink ())
, m_severity (kFatal)
{
}
Journal::Stream::Stream (Sink& sink, Severity severity)
: m_sink (&sink)
, m_severity (severity)
{
}
Journal::Stream::Stream (Stream const& other)
: m_sink (other.m_sink)
, m_severity (other.m_severity)
{
}
bool Journal::Stream::active () const
{
return m_sink->active (m_severity);
}
Journal::Sink& Journal::Stream::sink () const
{
return *m_sink;
}
Journal::Severity Journal::Stream::severity () const
{
return m_severity;
}
Journal::Stream& Journal::Stream::operator= (Stream const& other)
{
m_sink = other.m_sink;
m_severity = other.m_severity;
return *this;
}
Journal::ScopedStream Journal::Stream::operator<< (std::ostream& manip (std::ostream&)) const
{
return ScopedStream (*this, manip);
}
//------------------------------------------------------------------------------
Journal::Journal ()
: m_sink (&getNullSink())
, trace (stream (kTrace))
, debug (stream (kDebug))
, info (stream (kInfo))
, warning (stream (kWarning))
, error (stream (kError))
, fatal (stream (kFatal))
{
}
Journal::Journal (Sink& sink)
: m_sink (&sink)
, trace (stream (kTrace))
, debug (stream (kDebug))
, info (stream (kInfo))
, warning (stream (kWarning))
, error (stream (kError))
, fatal (stream (kFatal))
{
}
Journal::Journal (Journal const& other)
: m_sink (other.m_sink)
, trace (stream (kTrace))
, debug (stream (kDebug))
, info (stream (kInfo))
, warning (stream (kWarning))
, error (stream (kError))
, fatal (stream (kFatal))
{
}
Journal::~Journal ()
{
}
Journal::Stream Journal::stream (Severity severity) const
{
return Stream (*m_sink, severity);
}
/** Returns `true` if the sink logs messages at that severity. */
bool Journal::active (Severity severity) const
{
return m_sink->active (severity);
}
}