From adad62aed85b9f8bd092c919bde68d9df6b152df Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sat, 27 Jul 2013 09:52:13 -0700 Subject: [PATCH] Generalize swapWithArray Conflicts: Subtrees/beast/TODO.txt --- Subtrees/beast/CodingStyle.md | 11 +++++++++++ Subtrees/beast/TODO.txt | 6 +++++- .../modules/beast_core/containers/beast_Array.h | 5 +++-- .../beast_core/containers/beast_OwnedArray.h | 13 +++++++++---- .../beast_core/containers/beast_SharedObjectArray.h | 9 +++++---- 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/Subtrees/beast/CodingStyle.md b/Subtrees/beast/CodingStyle.md index a060e14af3..92b35da4ef 100644 --- a/Subtrees/beast/CodingStyle.md +++ b/Subtrees/beast/CodingStyle.md @@ -1,3 +1,11 @@ +Write code in a clear, self-documenting style but use comments where necessary. +Use Test Driven Development. It encourages designing interfaces first, before implementation. +Don't Repeat Yourself, “D.R.Y.”. Put redundant code in a class so it can be re-used and unit tested. +Expose as little of a class as possible. Prefer private over protected. Prefer protected over public. The smaller the interface footprint, the easier it is to write unit tests and comprehend the operation of the class. This is the Interface Segregation Principle. +Use language constants (enum or static const) with descriptive names instead of hard-coded “magic numbers.” +Make classes depend on as few external classes or routines as possible. Ideally, no dependencies. +Don't limit flexibility of parameters by forcing the caller to use specific types where general types would work. + -------------------------------------------------------------------------------- # Coding Standards @@ -175,6 +183,9 @@ overlooked. Blank lines are used to separate code into "paragraphs." ## Miscellaneous +* Constrain the scope of identifiers to the smallest area that needs it. Consider + using nested classes or classes inside functions, if doing so will expose less + interface and implementation overall. * `goto` statements should not be used at all, even if the alternative is more verbose code. The only exception is when implementing an algorithm in a function as a state machine. diff --git a/Subtrees/beast/TODO.txt b/Subtrees/beast/TODO.txt index 89513ae9a8..4c553c80e4 100644 --- a/Subtrees/beast/TODO.txt +++ b/Subtrees/beast/TODO.txt @@ -2,7 +2,11 @@ BEAST TODO -------------------------------------------------------------------------------- -- Implement HardenedHashFunctions +- HashMap work: + - Add unit test + - Return size_t from hash function, take out upperLimit, move mod % to caller + - Make hash function a functor using operator() + - Implement HardenedHashFunctions - Set sqlite thread safety model to '2' in beast_sqlite diff --git a/Subtrees/beast/modules/beast_core/containers/beast_Array.h b/Subtrees/beast/modules/beast_core/containers/beast_Array.h index 92c66f3917..07ab7bdfb9 100644 --- a/Subtrees/beast/modules/beast_core/containers/beast_Array.h +++ b/Subtrees/beast/modules/beast_core/containers/beast_Array.h @@ -571,10 +571,11 @@ public: If you need to exchange two arrays, this is vastly quicker than using copy-by-value because it just swaps their internal pointers. */ - void swapWithArray (Array& otherArray) noexcept + template + void swapWithArray (OtherArrayType& otherArray) noexcept { const ScopedLockType lock1 (getLock()); - const ScopedLockType lock2 (otherArray.getLock()); + const typename OtherArrayType::ScopedLockType lock2 (otherArray.getLock()); data.swapWith (otherArray.data); std::swap (numUsed, otherArray.numUsed); diff --git a/Subtrees/beast/modules/beast_core/containers/beast_OwnedArray.h b/Subtrees/beast/modules/beast_core/containers/beast_OwnedArray.h index f4f964cef8..022188ec16 100644 --- a/Subtrees/beast/modules/beast_core/containers/beast_OwnedArray.h +++ b/Subtrees/beast/modules/beast_core/containers/beast_OwnedArray.h @@ -90,7 +90,7 @@ public: //============================================================================== /** Clears the array, optionally deleting the objects inside it first. */ - void clear (const bool deleteObjects = true) + void clear (bool deleteObjects = true) { const ScopedLockType lock (getLock()); @@ -101,14 +101,19 @@ public: numUsed = 0; } - /** Removes all elements from the array without freeing the array's allocated storage. + /** Clears the array, optionally deleting the objects inside it first. + + The array's allocated storage is preserved. @see clear */ - void clearQuick() + void clearQuick(bool deleteObjects = true) { const ScopedLockType lock (getLock()); - deleteAllElements(); + + if (deleteObjects) + deleteAllObjects(); + numUsed = 0; } diff --git a/Subtrees/beast/modules/beast_core/containers/beast_SharedObjectArray.h b/Subtrees/beast/modules/beast_core/containers/beast_SharedObjectArray.h index e140b47679..0b775ed77b 100644 --- a/Subtrees/beast/modules/beast_core/containers/beast_SharedObjectArray.h +++ b/Subtrees/beast/modules/beast_core/containers/beast_SharedObjectArray.h @@ -21,8 +21,8 @@ */ //============================================================================== -#ifndef BEAST_REFERENCECOUNTEDARRAY_BEASTHEADER -#define BEAST_REFERENCECOUNTEDARRAY_BEASTHEADER +#ifndef BEAST_SHAREDOBJECTARRAY_H_INCLUDED +#define BEAST_SHAREDOBJECTARRAY_H_INCLUDED #include "../memory/beast_SharedObject.h" #include "beast_ArrayAllocationBase.h" @@ -740,10 +740,11 @@ public: If you need to exchange two arrays, this is vastly quicker than using copy-by-value because it just swaps their internal pointers. */ - void swapWithArray (SharedObjectArray& otherArray) noexcept + template + void swapWithArray (OtherArrayType& otherArray) noexcept { const ScopedLockType lock1 (getLock()); - const ScopedLockType lock2 (otherArray.getLock()); + const typename OtherArrayType::ScopedLockType lock2 (otherArray.getLock()); data.swapWith (otherArray.data); std::swap (numUsed, otherArray.numUsed);