From 42692abd1ce3f33ac9997e0f9977d22598718d60 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sat, 14 Dec 2013 20:18:01 -0800 Subject: [PATCH] Add stl module for c++11 compatibility classes --- Builds/VisualStudio2012/beast.vcxproj | 6 + Builds/VisualStudio2012/beast.vcxproj.filters | 21 ++ beast/STL.h | 28 ++ beast/stl/STL.cpp | 25 ++ beast/stl/function.h | 31 +++ beast/stl/shared_ptr.h | 33 +++ beast/stl/thread.h | 35 +++ beast/stl/unique_ptr.h | 255 ++++++++++++++++++ modules/beast_core/beast_core.h | 1 + 9 files changed, 435 insertions(+) create mode 100644 beast/STL.h create mode 100644 beast/stl/STL.cpp create mode 100644 beast/stl/function.h create mode 100644 beast/stl/shared_ptr.h create mode 100644 beast/stl/thread.h create mode 100644 beast/stl/unique_ptr.h diff --git a/Builds/VisualStudio2012/beast.vcxproj b/Builds/VisualStudio2012/beast.vcxproj index 29ce253cfd..6a3412a1e4 100644 --- a/Builds/VisualStudio2012/beast.vcxproj +++ b/Builds/VisualStudio2012/beast.vcxproj @@ -149,6 +149,11 @@ + + + + + @@ -519,6 +524,7 @@ true + true true diff --git a/Builds/VisualStudio2012/beast.vcxproj.filters b/Builds/VisualStudio2012/beast.vcxproj.filters index be1df3c138..765753011f 100644 --- a/Builds/VisualStudio2012/beast.vcxproj.filters +++ b/Builds/VisualStudio2012/beast.vcxproj.filters @@ -315,6 +315,9 @@ {df4f2935-13a1-4afe-90cc-d86472ec2466} + + {793e2d61-14f6-4fa1-a17f-af6bbec13ba1} + @@ -1278,6 +1281,21 @@ beast\smart_ptr + + beast\stl + + + beast\stl + + + beast\stl + + + beast\stl + + + beast + @@ -1835,6 +1853,9 @@ beast\smart_ptr\impl + + beast\stl + diff --git a/beast/STL.h b/beast/STL.h new file mode 100644 index 0000000000..3a84b3486e --- /dev/null +++ b/beast/STL.h @@ -0,0 +1,28 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + 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_STL_H_INCLUDED +#define BEAST_STL_H_INCLUDED + +#include "stl/function.h" +#include "stl/shared_ptr.h" +#include "stl/thread.h" +#include "stl/unique_ptr.h" + +#endif diff --git a/beast/stl/STL.cpp b/beast/stl/STL.cpp new file mode 100644 index 0000000000..2d46a5f9a9 --- /dev/null +++ b/beast/stl/STL.cpp @@ -0,0 +1,25 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + 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 "function.h" +#include "shared_ptr.h" +#include "thread.h" +#include "unique_ptr.h" diff --git a/beast/stl/function.h b/beast/stl/function.h new file mode 100644 index 0000000000..2c769b76f1 --- /dev/null +++ b/beast/stl/function.h @@ -0,0 +1,31 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + 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_STL_FUNCTION_H_INCLUDED +#define BEAST_STL_FUNCTION_H_INCLUDED + +#include + +namespace beast { + +using boost::function; + +} + +#endif diff --git a/beast/stl/shared_ptr.h b/beast/stl/shared_ptr.h new file mode 100644 index 0000000000..3d2875b4db --- /dev/null +++ b/beast/stl/shared_ptr.h @@ -0,0 +1,33 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + 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_STL_SHARED_PTR_H_INCLUDED +#define BEAST_STL_SHARED_PTR_H_INCLUDED + +#include + +namespace beast { + +using boost::shared_ptr; +using boost::make_shared; +using boost::enable_shared_from_this; + +} + +#endif diff --git a/beast/stl/thread.h b/beast/stl/thread.h new file mode 100644 index 0000000000..987f461cff --- /dev/null +++ b/beast/stl/thread.h @@ -0,0 +1,35 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + 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_STL_THREAD_H_INCLUDED +#define BEAST_STL_THREAD_H_INCLUDED + +#include + +namespace beast { + +using boost::thread; + +namespace this_thread { +using namespace boost::this_thread; +} + +} + +#endif diff --git a/beast/stl/unique_ptr.h b/beast/stl/unique_ptr.h new file mode 100644 index 0000000000..24984cc4d4 --- /dev/null +++ b/beast/stl/unique_ptr.h @@ -0,0 +1,255 @@ +//------------------------------------------------------------------------------ +/* + This file is part of Beast: https://github.com/vinniefalco/Beast + Copyright 2013, Vinnie Falco + + 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_STL_UNIQUE_PTR_H_INCLUDED +#define BEAST_STL_UNIQUE_PTR_H_INCLUDED + +#include "../Config.h" +#include "../Uncopyable.h" + +#ifndef BEAST_UNIQUE_PTR_USES_STL +# if BEAST_USE_CPLUSPLUS11 +# define BEAST_UNIQUE_PTR_USES_STL 1 +# else +# define BEAST_UNIQUE_PTR_USES_STL 0 +# endif +#endif + +#if BEAST_UNIQUE_PTR_USES_STL +# include +namespace beast { +using std::unique_ptr; +} + +#else + +namespace beast { + +template +class unique_ptr : public Uncopyable +{ +public: + //============================================================================== + /** Creates a unique_ptr containing a null pointer. */ + inline unique_ptr() + : object (nullptr) + { + } + + /** Creates a unique_ptr that owns the specified object. */ + inline unique_ptr (ObjectType* const objectToTakePossessionOf) + : object (objectToTakePossessionOf) + { + } + + /** Creates a unique_ptr that takes its pointer from another unique_ptr. + + Because a pointer can only belong to one unique_ptr, this transfers + the pointer from the other object to this one, and the other object is reset to + be a null pointer. + */ + unique_ptr (unique_ptr& objectToTransferFrom) + : object (objectToTransferFrom.object) + { + objectToTransferFrom.object = nullptr; + } + + /** Destructor. + This will delete the object that this unique_ptr currently refers to. + */ + inline ~unique_ptr() + { + ContainerDeletePolicy ::destroy (object); + } + + /** Changes this unique_ptr to point to a new object. + + Because a pointer can only belong to one unique_ptr, this transfers + the pointer from the other object to this one, and the other object is reset to + be a null pointer. + + If this unique_ptr already points to an object, that object + will first be deleted. + */ + unique_ptr& operator= (unique_ptr& objectToTransferFrom) + { + if (this != objectToTransferFrom.getAddress()) + { + // Two ScopedPointers should never be able to refer to the same object - if + // this happens, you must have done something dodgy! + bassert (object == nullptr || object != objectToTransferFrom.object); + + ObjectType* const oldObject = object; + object = objectToTransferFrom.object; + objectToTransferFrom.object = nullptr; + ContainerDeletePolicy ::destroy (oldObject); + } + + return *this; + } + + /** Changes this unique_ptr to point to a new object. + + If this unique_ptr already points to an object, that object + will first be deleted. + + The pointer that you pass in may be a nullptr. + */ + unique_ptr& operator= (ObjectType* const newObjectToTakePossessionOf) + { + if (object != newObjectToTakePossessionOf) + { + ObjectType* const oldObject = object; + object = newObjectToTakePossessionOf; + ContainerDeletePolicy ::destroy (oldObject); + } + + return *this; + } + + #if BEAST_COMPILER_SUPPORTS_MOVE_SEMANTICS + unique_ptr (unique_ptr&& other) + : object (other.object) + { + other.object = nullptr; + } + + unique_ptr& operator= (unique_ptr&& other) + { + object = other.object; + other.object = nullptr; + return *this; + } + #endif + + //============================================================================== + /** Returns the object that this unique_ptr refers to. */ + inline operator ObjectType*() const { return object; } + + /** Returns the object that this unique_ptr refers to. */ + inline ObjectType* get() const { return object; } + + /** Returns the object that this unique_ptr refers to. */ + inline ObjectType& operator*() const { return *object; } + + /** Lets you access methods and properties of the object that this unique_ptr refers to. */ + inline ObjectType* operator->() const { return object; } + + //============================================================================== + /** Removes the current object from this unique_ptr without deleting it. + This will return the current object, and set the unique_ptr to a null pointer. + */ + ObjectType* release() { ObjectType* const o = object; object = nullptr; return o; } + + //============================================================================== + /** Swaps this object with that of another unique_ptr. + The two objects simply exchange their pointers. + */ + void swapWith (unique_ptr & other) + { + // Two ScopedPointers should never be able to refer to the same object - if + // this happens, you must have done something dodgy! + bassert (object != other.object || this == other.getAddress()); + std::swap (object, other.object); + } + + inline ObjectType* createCopy() const { return createCopyIfNotNull (object); } + +private: + ObjectType* object; + const unique_ptr* getAddress() const { return this; } +}; + +template +bool operator== (const unique_ptr& pointer1, ObjectType* const pointer2) +{ + return static_cast (pointer1) == pointer2; +} + +template +bool operator!= (const unique_ptr& pointer1, ObjectType* const pointer2) +{ + return static_cast (pointer1) != pointer2; +} + +//------------------------------------------------------------------------------ + +} + +#endif + +namespace beast { + +template +unique_ptr make_unique () +{ + return unique_ptr (new T ()); +} + +template +unique_ptr make_unique (P1 p1) +{ + return unique_ptr (new T (p1)); +} + +template +unique_ptr make_unique (P1 p1, P2 p2) +{ + return unique_ptr (new T (p1, p2)); +} + +template +unique_ptr make_unique (P1 p1, P2 p2, P3 p3) +{ + return unique_ptr (new T (p1, p2, p3)); +} + +template +unique_ptr make_unique (P1 p1, P2 p2, P3 p3, P4 p4) +{ + return unique_ptr (new T (p1, p2, p3, p4)); +} + +template +unique_ptr make_unique (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) +{ + return unique_ptr (new T (p1, p2, p3, p4, p5)); +} + +template +unique_ptr make_unique (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) +{ + return unique_ptr (new T (p1, p2, p3, p4, p5, p6)); +} + +template +unique_ptr make_unique (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7) +{ + return unique_ptr (new T (p1, p2, p3, p4, p5, p6, p7)); +} + +template +unique_ptr make_unique (P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8) +{ + return unique_ptr (new T (p1, p2, p3, p4, p5, p6, p7, p8)); +} + +} + +#endif diff --git a/modules/beast_core/beast_core.h b/modules/beast_core/beast_core.h index 74aeb1601a..911825d976 100644 --- a/modules/beast_core/beast_core.h +++ b/modules/beast_core/beast_core.h @@ -61,6 +61,7 @@ #include "../../beast/Threads.h" #include "../../beast/Utility.h" #include "../../beast/Chrono.h" +#include "../../beast/STL.h" #include "system/StandardIncludes.h"