diff --git a/public/amtl/am-float.h b/public/amtl/am-float.h new file mode 100644 index 00000000..e53f25a0 --- /dev/null +++ b/public/amtl/am-float.h @@ -0,0 +1,50 @@ +// vim: set sts=8 ts=2 sw=2 tw=99 et: +// +// Copyright (C) 2013, David Anderson and AlliedModders LLC +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of AlliedModders LLC nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +#ifndef _include_amtl_float_h_ +#define _include_amtl_float_h_ + +#include +#include + +namespace ke { + +static inline bool +IsNaN(double v) +{ +#ifdef _MSC_VER + return !!_isnan(v); +#else + return isnan(v); +#endif +} + +} // namespace ke + +#endif // _include_amtl_float_h_ diff --git a/public/amtl/am-refcounting.h b/public/amtl/am-refcounting.h index 028d6df5..ea869464 100644 --- a/public/amtl/am-refcounting.h +++ b/public/amtl/am-refcounting.h @@ -55,6 +55,13 @@ class AlreadyRefed : thing_(t) { } + AlreadyRefed(const AlreadyRefed &other) + : thing_(other.thing_) + { + // If copy elision for some reason doesn't happen (for example, when + // returning from AdoptRef), just null out the source ref. + other.thing_ = NULL; + } ~AlreadyRefed() { if (thing_) thing_->Release(); diff --git a/public/amtl/am-utility.h b/public/amtl/am-utility.h index cbe48167..1e31e067 100644 --- a/public/amtl/am-utility.h +++ b/public/amtl/am-utility.h @@ -37,6 +37,7 @@ #if defined(_MSC_VER) # include #endif +#include #define KE_32BIT @@ -62,6 +63,18 @@ ReturnAndVoid(T &t) return saved; } +#if __cplusplus >= 201103L +# define KE_CXX11 +#endif + +#if defined(KE_CXX11) +# define KE_DELETE = delete +# define KE_OVERRIDE override +#else +# define KE_DELETE +# define KE_OVERRIDE +#endif + // Wrapper that automatically deletes its contents. The pointer can be taken // to avoid destruction. template @@ -78,6 +91,11 @@ class AutoPtr : t_(t) { } + AutoPtr(Moveable > other) + { + t_ = other->t_; + other->t_ = NULL; + } ~AutoPtr() { delete t_; } @@ -93,13 +111,24 @@ class AutoPtr operator T *() const { return t_; } - void operator =(T *t) { + T *operator =(T *t) { delete t_; t_ = t; + return t_; + } + T *operator =(Moveable > other) { + delete t_; + t_ = other->t_; + other->t_ = NULL; + return t_; } bool operator !() const { return !t_; } + + private: + AutoPtr(const AutoPtr &other) KE_DELETE; + AutoPtr &operator =(const AutoPtr &other) KE_DELETE; }; // Wrapper that automatically deletes its contents. The pointer can be taken @@ -317,6 +346,26 @@ class SaveAndSet T old_; }; +template +class StackLinked +{ + public: + StackLinked(T **prevp) + : prevp_(prevp), + prev_(*prevp) + { + *prevp_ = static_cast(this); + } + virtual ~StackLinked() { + assert(*prevp_ == this); + *prevp_ = prev_; + } + + private: + T **prevp_; + T *prev_; +}; + #if __cplusplus >= 201103L # define KE_CXX11 #endif @@ -331,8 +380,12 @@ class SaveAndSet #if defined(_MSC_VER) # define KE_SIZET_FMT "%Iu" +# define KE_I64_FMT "%I64d" +# define KE_U64_FMT "%I64u" #elif defined(__GNUC__) # define KE_SIZET_FMT "%zu" +# define KE_I64_FMT "%lld" +# define KE_U64_FMT "%llu" #else # error "Implement format specifier string" #endif diff --git a/public/amtl/am-vector.h b/public/amtl/am-vector.h index 700ec188..4f01bb97 100644 --- a/public/amtl/am-vector.h +++ b/public/amtl/am-vector.h @@ -86,14 +86,22 @@ class Vector : public AllocPolicy } // Shift all elements including |at| up by one, and insert |item| at the - // given position. This is a linear-time operation. + // given position. If |at| is one greater than the last usable index, + // i.e. |at == length()|, then this is the same as append(). No other + // invalid indexes are allowed. + // + // This is a linear-time operation. bool insert(size_t at, const T &item) { + if (at == length()) + return append(item); if (!moveUp(at)) return false; new (&data_[at]) T(item); return true; } bool insert(size_t at, Moveable item) { + if (at == length()) + return append(item); if (!moveUp(at)) return false; new (&data_[at]) T(item); @@ -104,7 +112,7 @@ class Vector : public AllocPolicy // element. This is a linear-time operation. void remove(size_t at) { for (size_t i = at; i < length() - 1; i++) - data_[i] = T(Moveable(data_[i + 1])); + data_[i] = Moveable(data_[i + 1]); pop(); }