From f69f86b487a4f845847f2d56fb642552caaf2e1e Mon Sep 17 00:00:00 2001 From: Joshua Warner Date: Tue, 25 Feb 2014 14:26:02 -0700 Subject: [PATCH] move Slice out of object-writer, to be more general --- include/avian/tools/object-writer/tools.h | 64 +++++++++-------------- include/avian/util/slice.h | 60 +++++++++++++++++++++ 2 files changed, 85 insertions(+), 39 deletions(-) create mode 100644 include/avian/util/slice.h diff --git a/include/avian/tools/object-writer/tools.h b/include/avian/tools/object-writer/tools.h index 7763dfdf96..dde15e803e 100644 --- a/include/avian/tools/object-writer/tools.h +++ b/include/avian/tools/object-writer/tools.h @@ -14,6 +14,7 @@ #include #include +#include #include "avian/environment.h" @@ -72,51 +73,32 @@ public: unsigned add(util::String str); }; -template -class Slice { -public: - T* items; - size_t count; - - inline Slice(T* items, size_t count): - items(items), - count(count) {} - - inline Slice(const Slice& copy): - items(copy.items), - count(copy.count) {} - - inline T* begin() { - return items; - } - - inline T* end() { - return items + count; - } -}; - -template -class DynamicArray : public Slice { -public: +template +class DynamicArray : public util::Slice { + public: size_t capacity; - DynamicArray(): - Slice((T*)malloc(10 * sizeof(T)), 0), - capacity(10) {} - ~DynamicArray() { - free(Slice::items); + DynamicArray() : util::Slice((T*)malloc(10 * sizeof(T)), 0), capacity(10) + { + } + ~DynamicArray() + { + free(util::Slice::items); } - void ensure(size_t more) { - if(Slice::count + more > capacity) { - capacity = capacity * 2 + more; - Slice::items = (T*)realloc(Slice::items, capacity * sizeof(T)); + void ensure(size_t more) + { + if (util::Slice::count + more > capacity) { + capacity = capacity * 2 + more; + util::Slice::items + = (T*)realloc(util::Slice::items, capacity * sizeof(T)); + } } -} - void add(const T& item) { + void add(const T& item) + { ensure(1); - Slice::items[Slice::count++] = item; + util::Slice::items[util::Slice::count++] = item; } }; @@ -175,7 +157,11 @@ public: Executable = 1 << 1 }; - virtual bool writeObject(OutputStream* out, Slice symbols, Slice data, unsigned accessFlags, unsigned alignment) = 0; + virtual bool writeObject(OutputStream* out, + util::Slice symbols, + util::Slice data, + unsigned accessFlags, + unsigned alignment) = 0; static Platform* getPlatform(PlatformInfo info); }; diff --git a/include/avian/util/slice.h b/include/avian/util/slice.h new file mode 100644 index 0000000000..3b17f1e843 --- /dev/null +++ b/include/avian/util/slice.h @@ -0,0 +1,60 @@ +/* Copyright (c) 2008-2013, Avian Contributors + + 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. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +#ifndef AVIAN_UTIL_SLICE_H +#define AVIAN_UTIL_SLICE_H + +#include "allocator.h" +#include "math.h" + +namespace avian { +namespace util { + +template +class Slice { + public: + T* items; + size_t count; + + inline Slice(T* items, size_t count) : items(items), count(count) + { + } + + inline Slice(const Slice& copy) : items(copy.items), count(copy.count) + { + } + + inline T& operator[](size_t index) + { + return items[index]; + } + + inline T* begin() + { + return items; + } + + inline T* end() + { + return items + count; + } + + Slice clone(Allocator* a) + { + Slice ret((T*)a->allocate(count * sizeof(T)), count); + memcpy(ret.items, items, count * sizeof(T)); + return ret; + } +}; + +} // namespace util +} // namespace avian + +#endif // AVIAN_UTIL_SLICE_H