BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlat_arrayutil

Detailed Description

Outline

Purpose

Provide utilities for operating on bdlat "array" types.

Classes

See also
bdlat_arrayfunctions, bdlat_typecategory

Description

This component provides a utility struct, bdlat::ArrayUtil, which serves as a namespace for a collection of function templates providing derived operations for "array" types. See bdlat_arrayfunctions for the set of requirements of "array" types in the bdlat framework. See bdlat_typecategory for more general information about this framework.

Primitive and Derived Functions of Arrays

In order to be "plugged in" to the bdlat framework as an "array", a type must meet a set of requirements including providing certain function overloads (customization points) and specifying certain type traits, as specified by the bdlat_arrayfunctions component. We call the required function overloads the "primitive" operations of "array" types. This component provides "derived" operations, which are operations that are exclusively defined in terms of primitive operations, and as such can be used with any "array" type.

Usage

In this section we show intended usage of this component.

Example 1: Accessing an Array Element And Its Category

Suppose we would like to define a function that detects whether an element of an array is itself an array, in order to more generally detect nested arrays.

First, we need to define an accessor functor per {bdlat_typecategory |ACCESSOR Functors} that will be used to detect whether an array element is itself an array:

class MyArrayDetector {
// DATA
bool d_didVisitArray;
public:
// CREATORS
MyArrayDetector()
: d_didVisitArray(false)
{
}
// MANIPULATORS
template <class TYPE>
int operator()(const TYPE& object, bdlat_TypeCategory::Array)
{
d_didVisitArray = true;
return 0;
}
template <class TYPE, class OTHER_CATEGORY>
int operator()(const TYPE&, OTHER_CATEGORY)
{
d_didVisitArray = false;
return 0;
}
// ACCESSORS
bool didVisitArray()
{
return d_didVisitArray;
}
};
Definition bdlat_typecategory.h:1035

Then, we can define a utility struct, MyArrayUtil, that provides a function for detecting whether or not an array has an element that is itself an array:

struct MyArrayUtil {
// CLASS METHODS
template <class TYPE>
static int isElementAnArray(bool *isArray,
const TYPE& array,
int index)
// Load the value 'true' to the specified 'isArray' if the element
// at the specified 'index' of the specified 'array' has the
// "array" type category, and load the value 'false' otherwise.
// Return 0 on success, and a non-zero value otherwise. If a
// non-zero value is returned, the value loaded to 'isArray' is
// unspecified. The behavior is undefined unless the specified
// 'array' has the "array" type category, '0 <= index', and
// 'index < bdlat_ArrayFunctions::size(array)'.
{
BSLS_ASSERT(0 <= index);
BSLS_ASSERT(static_cast<bsl::size_t>(index) <
MyArrayDetector detector;
detector,
index);
if (0 != rc) {
return -1; // RETURN
}
*isArray = detector.didVisitArray();
return 0;
}
};
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
bsl::size_t size(const TYPE &array)
Return the number of elements in the specified array.
bdlat_TypeCategory::Value select(const TYPE &object)
static int accessElementByCategory(const TYPE &array, ACCESSOR &accessor, int index)
Definition bdlat_arrayutil.h:335
@ e_ARRAY_CATEGORY
Definition bdlat_typecategory.h:1046

Finally, we can use this utility to detect whether elements of array types are themselves arrays:

void example()
{
vectorA.push_back(42);
bool isArray = false;
int rc = MyArrayUtil::isElementAnArray(&isArray, vectorA, 0);
assert(0 == rc);
assert(! isArray);
rc = MyArrayUtil::isElementAnArray(&isArray, vectorB, 0);
assert(0 == rc);
assert(isArray);
}
Definition bslstl_vector.h:1025
void push_back(const VALUE_TYPE &value)
Definition bslstl_vector.h:3760