BDE 4.14.0 Production release
|
Provide an enumeration of copy/move state, including unsupported.
This component provides a struct
, bsltf::CopyMoveState
, which serves as a namespace for enumerating the copy and move state of an object, including an unknown value indicating that the test type does not support tracking this information. A test type can, using a single instance of this enum
track whether the object was most recently copied into, moved into, or moved from.
In instances where more than one operatation needs to be tracked (i.e., the object was copied into, then moved from), the logical-OR of multiple enumerators can be stored in a single variable, but doing so does not preserve the order of the operations nor whether any operation occured more than once; if such information is needed, multiple instances of this enum
can be used.
The CopyMoveState
namespace also provides a toAscii
method that converts an enumerator into its name represented as a null-terminated ASCII string.
All of the enumerators have unique values. The e_ORIGINAL
enumerator has value 0 (no bits set). The values for e_COPIED_CONST_INTO
and e_COPIED_NONCONST_INTO
each have the e_COPIED_INTO
bit set as well as one other bit. The remaining enumerators have one bit set each, sharing no bits with any of the other enumerators.
When converted to bool
, e_ORIGINAL
yields false
whereas the rest yield true
. When e_UNKNOWN
is not used, this conversion makes it easy create a binary test of whether an object was copied or moved.
This section illustrates intended use of this component.
In this example, we show how CopyMoveState
can be used to track the most recent copy or move operation applied to an object.
First, we define a TrackedValue
type that contains an integer value and a CopyMoveState
Next, we define a value constructor that indicates that the object was neither copied into, moved into, nor moved from:
Next, we define a copy constructor that records the fact that the object was copied into. As in the case of most copy constructors, the original is accessed through a const
reference, and the copy/moved state reflects that fact.
Next, we define a move constructor that records both that the object being constructed was moved into, but also that the original object was moved from.
Next, we declare the destructor and assignment operators, but, for the purpose of this example, we don't need to see their implementations:
Then, we define accessors for the value and copy/move state.
Now, outside of the class definition or (better yet) as a hidden friend function, we define an ADL customization point used to retrieve the copy/move state of the tracked value. This free function will be called in generic code that doesn't know whether the type it's working with has a copyMoveState
accessor; a default is provided for types that don't:
Finally, we test our TrackedValue
class by creating an object, copying it, and moving it. After each step, we test that each object's copy/move state is as expected. Note that e_COPIED_INTO
names a bit that is set in both of the other e_COPIED_*
enumerators. At the end, we verify that the copy/move state is correctly converted to a string by the toAscii
method and that the copy/move state of a plain int
is always "e_UNKNOWN".