Move and add some template metaprogramming classes

This commit is contained in:
Vinnie Falco committed 2013-09-19 14:42:54 -07:00
1 parent 2a164f0165
commit 72fc42b60c
13 files changed
+497 -167

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
//------------------------------------------------------------------------------
/*
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_STATICASSERT_H_INCLUDED
#define BEAST_STATICASSERT_H_INCLUDED
#ifndef DOXYGEN
namespace beast
{
template <bool b>
struct BeastStaticAssert;
template <>
struct BeastStaticAssert <true>
{
static void dummy() {}
};
}
#endif
/** A compile-time assertion macro.
If the expression parameter is false, the macro will cause a compile error.
(The actual error message that the compiler generates may be completely
bizarre and seem to have no relation to the place where you put the
static_assert though!)
*/
#define static_bassert(expression) beast::BeastStaticAssert<expression>::dummy();
#endif
+28
View File
@@ -0,0 +1,28 @@
//------------------------------------------------------------------------------
/*
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_TYPETRAITS_H_INCLUDED
#define BEAST_TYPETRAITS_H_INCLUDED
#include "type_traits/IntegralConstant.h"
#include "type_traits/IsIntegral.h"
#include "type_traits/IsSigned.h"
#include "type_traits/RemoveSigned.h"
#endif
+14 -3
View File
@@ -21,6 +21,7 @@
#define BEAST_NET_IPENDPOINT_H_INCLUDED
#include <string>
#include <istream>
#include <ostream>
#include "../mpl/IfCond.h"
@@ -130,7 +131,8 @@ public:
return
((value&0xff000000)==0x0a000000) || // Prefix /8, 10.##.#.#
((value&0xfff00000)==0xac100000) || // Prefix /12 172.16.#.# - 172.31.#.#
((value&0xffff0000)==0xc0a80000) ; // Prefix /16 192.168.#.#
((value&0xffff0000)==0xc0a80000) || // Prefix /16 192.168.#.#
isLoopback();
}
/** Returns `true` if this is a broadcast address. */
@@ -602,13 +604,22 @@ inline std::ostream& operator<< (std::ostream &os, IPEndpoint::V6 const& addr)
return os;
}
inline std::ostream& operator<< (std::ostream &os, IPEndpoint const& endpoint)
inline std::ostream& operator<< (std::ostream &os, IPEndpoint const& ep)
{
os << endpoint.to_string();
os << ep.to_string();
return os;
}
/** @} */
/** Input stream conversions. */
/** @{ */
inline std::istream& operator>> (std::istream &is, IPEndpoint::V6&)
{
//is >> addr.to_string();
return is;
}
/** @} */
}
#endif
+157 -1
View File
@@ -22,9 +22,164 @@
namespace beast
{
namespace parse
{
/** Require and consume the specified character from the input.
@return `true` if the character matched.
*/
bool expect (std::istream& is, char v)
{
char c;
if (is.get(c) && v == c)
return true;
is.unget();
is.setstate (std::ios_base::failbit);
return false;
}
namespace detail
{
/** Used to disambiguate 8-bit integers from characters. */
template <typename IntType>
struct integer_holder
{
IntType* pi;
explicit integer_holder (IntType& i)
: pi (&i)
{
}
template <typename OtherIntType>
IntType& operator= (OtherIntType o) const
{
*pi = o;
return *pi;
}
};
/** Parse 8-bit unsigned integer. */
std::istream& operator>> (std::istream& is, integer_holder <uint8> const& i)
{
uint16 v;
is >> v;
if (! (v>=0 && v<=255))
{
is.setstate (std::ios_base::failbit);
return is;
}
i = uint8(v);
return is;
}
}
/** Free function for template argument deduction. */
template <typename IntType>
detail::integer_holder <IntType> integer (IntType& i)
{
return detail::integer_holder <IntType> (i);
}
}
/** Parse IPv4 address. */
std::istream& operator>> (std::istream &is, IPEndpoint::V4& addr)
{
uint8 octets [4];
is >> parse::integer (octets [0]);
for (int i = 1; i < 4; ++i)
{
if (!is || !parse::expect (is, '.'))
return is;
is >> parse::integer (octets [i]);
if (!is)
return is;
}
addr = IPEndpoint::V4 (octets[0], octets[1], octets[2], octets[3]);
return is;
}
/** Parse an IPEndpoint.
@note Currently only IPv4 addresses are supported.
*/
inline std::istream& operator>> (std::istream &is, IPEndpoint& ep)
{
IPEndpoint::V4 v4;
is >> v4;
if (is.fail())
return is;
if (is.rdbuf()->in_avail()>0)
{
char c;
is.get(c);
if (c != ':')
{
is.unget();
ep = IPEndpoint (v4);
return is;
}
uint16 port;
is >> port;
if (is.fail())
return is;
ep = IPEndpoint (v4, port);
}
else
{
ep = IPEndpoint (v4);
}
return is;
}
//------------------------------------------------------------------------------
class IPEndpointTests : public UnitTest
{
public:
bool parse (char const* text, IPEndpoint& ep)
{
std::string input (text);
std::istringstream stream (input);
stream >> ep;
return !stream.fail();
}
void shouldPass (char const* text)
{
IPEndpoint ep;
expect (parse (text, ep));
expect (ep.to_string() == std::string(text));
}
void shouldFail (char const* text)
{
IPEndpoint ep;
unexpected (parse (text, ep));
}
void testParse ()
{
beginTestCase ("parse");
shouldPass ("0.0.0.0");
shouldPass ("192.168.0.1");
shouldPass ("168.127.149.132");
shouldPass ("168.127.149.132:80");
shouldPass ("168.127.149.132:54321");
shouldFail ("");
shouldFail ("255");
shouldFail ("512");
shouldFail ("1.2.3.256");
shouldFail ("1.2.3:80");
}
void testPrint ()
{
beginTestCase ("addresses");
@@ -36,7 +191,7 @@ public:
expect ( ep.isPrivate());
expect (!ep.isBroadcast());
expect (!ep.isMulticast());
expect (!ep.isLoopback());
expect ( ep.isLoopback());
expect (ep.to_string() == "127.0.0.1:80");
ep = IPEndpoint::V4(10,0,0,1);
@@ -60,6 +215,7 @@ public:
void runTest ()
{
testPrint();
testParse();
}
IPEndpointTests () : UnitTest ("IPEndpoint", "beast")
+41
View File
@@ -0,0 +1,41 @@
//------------------------------------------------------------------------------
/*
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_TYPE_TRAITS_INTEGRALCONSTANT_H_INCLUDED
#define BEAST_TYPE_TRAITS_INTEGRALCONSTANT_H_INCLUDED
namespace beast
{
// ideas from boost
template <class T, T val>
struct IntegralConstant
{
typedef IntegralConstant <T, val> type;
typedef T value_type;
static const T value = val;
};
typedef IntegralConstant <bool, true> TrueType;
typedef IntegralConstant <bool, false> FalseType;
}
#endif
+44
View File
@@ -0,0 +1,44 @@
//------------------------------------------------------------------------------
/*
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_TYPE_TRAITS_ISINTEGRAL_H_INCLUDED
#define BEAST_TYPE_TRAITS_ISINTEGRAL_H_INCLUDED
#include "IntegralConstant.h"
namespace beast
{
namespace detail
template <typename T> struct IsIntegral : FalseType { };
template <> struct IsIntegral <int8> : TrueType { };
template <> struct IsIntegral <int16> : TrueType { };
template <> struct IsIntegral <int32> : TrueType { };
template <> struct IsIntegral <int64> : TrueType { };
template <> struct IsIntegral <uint8> : TrueType { };
template <> struct IsIntegral <uint16> : TrueType { };
template <> struct IsIntegral <uint32> : TrueType { };
template <> struct IsIntegral <uint64> : TrueType { };
template <> struct IsIntegral <wchar_t> : TrueType { };
}
#endif
+40
View File
@@ -0,0 +1,40 @@
//------------------------------------------------------------------------------
/*
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_TYPE_TRAITS_ISSIGNED_H_INCLUDED
#define BEAST_TYPE_TRAITS_ISSIGNED_H_INCLUDED
#include "IntegralConstant.h"
namespace beast
{
template <typename T> struct IsSigned : FalseType { };
template <> struct IsSigned <int8> : TrueType { };
template <> struct IsSigned <int16> : TrueType { };
template <> struct IsSigned <int32> : TrueType { };
template <> struct IsSigned <int64> : TrueType { };
template <> struct IsSigned <float> : TrueType { };
template <> struct IsSigned <double> : TrueType { };
template <> struct IsSigned <wchar_t> : TrueType { };
}
#endif
+45
View File
@@ -0,0 +1,45 @@
//------------------------------------------------------------------------------
/*
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_TYPE_TRAITS_REMOVESIGNED_H_INCLUDED
#define BEAST_TYPE_TRAITS_REMOVESIGNED_H_INCLUDED
#include "IntegralConstant.h"
namespace beast
{
/** Returns an equally sized, unsigned type.
Requires:
IsIntegral<T>::value == true
*/
template <typename T>
struct RemoveSigned
{
typedef T type;
}
template <> struct RemoveSigned <int8> { typedef uint8 type; }
template <> struct RemoveSigned <int16> { typedef uint16 type; }
template <> struct RemoveSigned <int32> { typedef uint32 type; }
template <> struct RemoveSigned <int64> { typedef uint64 type; }
}
#endif
+48
View File
@@ -0,0 +1,48 @@
//------------------------------------------------------------------------------
/*
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_ENABLEIF_H_INCLUDED
#define BEAST_UTILITY_ENABLEIF_H_INCLUDED
#include "../type_traits/IntegralConstant.h"
namespace beast
{
template <bool Enable, class T = void>
struct EnableIfBool : FalseType { typedef T type; };
template <class T>
struct EnableIfBool <false, T> { };
template <class Cond, class T = void>
struct EnableIf : public EnableIfBool <Cond::value, T> { };
template <bool Enable, class T = void>
struct DisableIfBool : FalseType { typedef T type; };
template <class T>
struct DisableIfBool <true, T> { };
template <class Cond, class T = void>
struct DisableIf : public DisableIfBool <Cond::value, T> { };
}
#endif