commit
598936be46
50
public/amtl/am-float.h
Normal file
50
public/amtl/am-float.h
Normal file
@ -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 <math.h>
|
||||
#include <float.h>
|
||||
|
||||
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_
|
@ -55,6 +55,13 @@ class AlreadyRefed
|
||||
: thing_(t)
|
||||
{
|
||||
}
|
||||
AlreadyRefed(const AlreadyRefed<T> &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();
|
||||
|
@ -37,6 +37,7 @@
|
||||
#if defined(_MSC_VER)
|
||||
# include <intrin.h>
|
||||
#endif
|
||||
#include <am-moveable.h>
|
||||
|
||||
#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 <typename T>
|
||||
@ -78,6 +91,11 @@ class AutoPtr
|
||||
: t_(t)
|
||||
{
|
||||
}
|
||||
AutoPtr(Moveable<AutoPtr<T> > 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<AutoPtr<T> > 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 <typename T>
|
||||
class StackLinked
|
||||
{
|
||||
public:
|
||||
StackLinked<T>(T **prevp)
|
||||
: prevp_(prevp),
|
||||
prev_(*prevp)
|
||||
{
|
||||
*prevp_ = static_cast<T *>(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
|
||||
|
@ -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<T> 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<T>(data_[i + 1]));
|
||||
data_[i] = Moveable<T>(data_[i + 1]);
|
||||
pop();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user