Skip to content

Commit

Permalink
ENH: Introduce itk::Boolean for use with std::vector
Browse files Browse the repository at this point in the history
  • Loading branch information
Leengit authored and hjmjohnson committed Feb 23, 2022
1 parent b5e0514 commit a837e7b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
58 changes: 58 additions & 0 deletions Modules/Core/Common/include/itkBoolean.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkBoolean_h
#define itkBoolean_h

namespace itk
{

/**
* \class Boolean
*
* \brief An alternative to bool for additional thread safety
*
* The class `Boolean` provides an alternative to `bool`. `std::vector<bool>` is not thread safe due to the possibility
* of multiple bits being packed together in the same memory location. std::vector<Boolean> does not have such a space
* optimization. std::vector<Boolean> is semantically like `std::vector<bool>`, but unlike `std::vector<bool>`, it does
* "avoid data races when the contents of the contained object in different elements in the same container [...] are
* modified concurrently", according to the C++ Standard, section [container.requirements.dataraces], "Container data
* races".
*
* \ingroup ITKCommon
*/
class Boolean
{
public:
/** Supports default construction */
constexpr Boolean() = default;

/** Supports implicit conversion from `bool`. */
constexpr Boolean(bool v)
: m_Value(v)
{}

/** Supports implicit conversion to `bool`. */
constexpr operator bool() const { return m_Value; }

private:
bool m_Value = false;
};

} // namespace itk

#endif
7 changes: 2 additions & 5 deletions Modules/Core/Common/include/itkBooleanStdVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#ifndef itkBooleanStdVector_h
#define itkBooleanStdVector_h

#include "itkIntTypes.h"
#include "itkBoolean.h"
#include <vector>

namespace itk
Expand All @@ -30,11 +30,8 @@ namespace itk
* `std::vector<bool>`, but unlike `std::vector<bool>`, it does "avoid data races when the contents of the contained
* object in different elements in the same container [...] are modified concurrently", according to the C++ Standard,
* section [container.requirements.dataraces], "Container data races".
*
* \note BooleanStdVectorType is only intended to be used for the storage of Boolean values (true and false), not for
* arbitrary integer values.
*/
using BooleanStdVectorType = std::vector<uint8_t>;
using BooleanStdVectorType = std::vector<Boolean>;

} // namespace itk

Expand Down
1 change: 1 addition & 0 deletions Modules/Core/Common/wrapping/ITKCommonBase.wrap
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ itk_wrap_simple_class("itk::OutputDataObjectIterator")
itk_wrap_simple_class("itk::DataObjectConstIterator")
itk_wrap_simple_class("itk::InputDataObjectConstIterator")
itk_wrap_simple_class("itk::OutputDataObjectConstIterator")
itk_wrap_simple_class("itk::Boolean")
itk_wrap_simple_class("itk::Statistics::RandomVariateGeneratorBase" POINTER)
itk_wrap_simple_class("itk::Statistics::MersenneTwisterRandomVariateGenerator" POINTER)

Expand Down

0 comments on commit a837e7b

Please sign in to comment.