/* sequence_example.cxx modification history ------------ ------- */ #include #include #include "foo.h" #include "ndds/ndds_cpp.h" int sequence_example() { FooSeq *myOwnedSequence1 = new FooSeq(); printf("myOwnedSequence1->length() = %d\n", myOwnedSequence1->length()); printf("myOwnedSequence1->maximum() = %d\n", myOwnedSequence1->maximum()); printf("myOwnedSequence1->has_ownership() = %d\n", myOwnedSequence1->has_ownership()); delete myOwnedSequence1; FooSeq myOwnedSequence2; printf("myOwnedSequence2.length() = %d\n", myOwnedSequence2.length()); printf("myOwnedSequence2.maximum() = %d\n", myOwnedSequence2.maximum()); printf("\nNote: This will generate an error because we are trying to get an element\n" " at an index (one) that exceeds the length (currently zero)\n"); Foo *myElement = &myOwnedSequence2[1]; FooSeq myOwnedSequence3; printf("\n\nmyOwnedSequence3.length() = %d\n", myOwnedSequence3.length()); printf("myOwnedSequence3.maximum() = %d\n", myOwnedSequence3.maximum()); printf("\nCalling: myOwnedSequence3.length(10)\n"); printf("\nNote: This will generate an error because we are trying to set the\n" " length to a value (10) that exceeds the capacity (currently zero)\n"); myOwnedSequence3.length(10); printf("\nNote: Length remains unchanged\n"); printf("myOwnedSequence3.length() = %d\n", myOwnedSequence3.length()); printf("\nCalling: myOwnedSequence3.maximum(5)\n"); myOwnedSequence3.maximum(5); printf("myOwnedSequence3.maximum() = %d\n", myOwnedSequence3.maximum()); printf("\nCalling: myOwnedSequence3.length(10)\n"); printf("Note: This will generate an error again because we are trying to set the\n" " length to a value (10) that still exceeds the capacity (currently 5)\n"); myOwnedSequence3.length(10); printf("\nNote: Length remains unchanged\n"); printf("myOwnedSequence3.length() = %d\n", myOwnedSequence3.length()); printf("\nCalling: myOwnedSequence3.maximum(10)\n"); myOwnedSequence3.maximum(10); printf("myOwnedSequence3.maximum() = %d\n", myOwnedSequence3.maximum()); printf("\nCalling: myOwnedSequence3.length(10)\n"); myOwnedSequence3.length(10); printf("\nNote: Length has now changed to the requested value\n"); printf("myOwnedSequence3.length() = %d\n", myOwnedSequence3.length()); FooSeq myOwnedSequence4; printf("\n\nmyOwnedSequence4.length() = %d\n", myOwnedSequence4.length()); printf( "myOwnedSequence4.maximum() = %d\n", myOwnedSequence4.maximum()); printf("\nCalling: myOwnedSequence4.ensure_length(10, 20)\n"); myOwnedSequence4.ensure_length(10, 20); printf("\nNote: Both the capacity and length are set to the desired value\n"); printf( "myOwnedSequence4.length() = %d\n", myOwnedSequence4.length()); printf( "myOwnedSequence4.maximum() = %d\n", myOwnedSequence4.maximum()); printf("\nCalling: myOwnedSequence4.ensure_length(15, 30)\n"); myOwnedSequence4.ensure_length(15, 30); printf("\nNote: Since the capacity was sufficient for the new length of 15\n" " the length is increased but the capacity remains the same\n"); printf( "myOwnedSequence4.length() = %d\n", myOwnedSequence4.length()); printf( "myOwnedSequence4.maximum() = %d\n", myOwnedSequence4.maximum()); printf("\nUsing loaned sequences\n"); int FOO_ARRAY_LENGTH = 40; Foo *foo_array = new Foo[FOO_ARRAY_LENGTH]; FooSeq myLoanedSequence1; printf("myLoanedSequence1.length() = %d\n", myLoanedSequence1.length()); printf("myLoanedSequence1.maximum() = %d\n", myLoanedSequence1.maximum()); printf("myLoanedSequence1.has_ownership() = %d\n", myLoanedSequence1.has_ownership()); printf("\nCalling: myLoanedSequence1.loan_contiguous(foo_array, 40, 40)\n"); myLoanedSequence1.loan_contiguous(foo_array, FOO_ARRAY_LENGTH, FOO_ARRAY_LENGTH); printf("\nNote: myLoanedSequence1 has been resized and has_ownership() now equals 0\n"); printf("myLoanedSequence1.length() = %d\n", myLoanedSequence1.length()); printf("myLoanedSequence1.maximum() = %d\n", myLoanedSequence1.maximum()); printf("myLoanedSequence1.has_ownership() = %d\n", myLoanedSequence1.has_ownership()); printf("\nCalling myLoanedSequence1.unloan()\n"); myLoanedSequence1.unloan(); printf("myLoanedSequence1.length() = %d\n", myLoanedSequence1.length()); printf("myLoanedSequence1.maximum() = %d\n", myLoanedSequence1.maximum()); printf("myLoanedSequence1.has_ownership() = %d\n", myLoanedSequence1.has_ownership()); for (int i=0; i< FOO_ARRAY_LENGTH; ++i) { foo_array[i].x = i; foo_array[i].y = -i; } printf("\n\nCopying elements one-by-one into a sequence that owns its memory\n"); // Example using owned sequences FooSeq myOwnedSequence5; // This allocates memory myOwnedSequence5.ensure_length(FOO_ARRAY_LENGTH, FOO_ARRAY_LENGTH); for (int i=0; i< FOO_ARRAY_LENGTH; ++i) { myOwnedSequence5[i] = foo_array[i]; // this copies each element } printf("myOwnedSequence5[10] = { x = %d, y = %d } \n", myOwnedSequence5[10].x, myOwnedSequence5[10].y); printf("\n\nUsing from_array() to copy into a sequence that owns its memory\n"); // Alternatively we could use the convenience function from_array FooSeq myOwnedSequence6; // Note that from_array() calls internally ensure_length() so it is unnecessary to // set the capacity or length explicitly ahead of calling from_array() myOwnedSequence6.from_array(foo_array, FOO_ARRAY_LENGTH); // Copies each element printf("\n\nUsing sequence that loans the memory to set elements without\n" "allocating memory or doing extra copies.\n"); FooSeq myLoanedSequence6; // This does not allocate memory. It makes the sequence point to the existing foo_array // This also does not do any copies. Yet the array will end up with the same values myLoanedSequence6.loan_contiguous(foo_array, FOO_ARRAY_LENGTH, FOO_ARRAY_LENGTH); printf("myLoanedSequence6[10] = { x = %d, y = %d } \n", myLoanedSequence6[10].x, myLoanedSequence6[10].y); myLoanedSequence6.unloan(); printf("\n\nUsing a loaned sequence with a discontiguous element buffer.\n"); int FOO_PTR_ARRAY_LENGTH = 40; Foo **foo_ptr_array = (Foo **)malloc(FOO_PTR_ARRAY_LENGTH); // Set the elements in foo_array_ptr to point to Foo objects wherever they are in memory for (int i=0; i< FOO_ARRAY_LENGTH; ++i) { foo_ptr_array[i] = new Foo(); foo_ptr_array[i]->x = i; foo_ptr_array[i]->y = -i; } FooSeq myLoanedDiscontiguousSequence6; // This does not allocate memory. It makes the sequence point to the existing foo_array_ptr // This also does not do any copies. Yet the array will end up with the same values myLoanedDiscontiguousSequence6.loan_discontiguous(foo_ptr_array, FOO_ARRAY_LENGTH, FOO_ARRAY_LENGTH); printf("myLoanedDiscontiguousSequence6[10] = { x = %d, y = %d } \n", myLoanedDiscontiguousSequence6[10].x, myLoanedDiscontiguousSequence6[10].y); // Note really us the same foo_array_ptr we set in the first place. // So no memory allocation occurred on the loan_discontiguous() call printf("\nReturn from get_discontiguous_buffer() = %p, original foo_array_ptr = %p\n", myLoanedDiscontiguousSequence6.get_discontiguous_buffer(), foo_ptr_array); myLoanedDiscontiguousSequence6.unloan(); printf("\nSwapping the elements at index 0 and 5\n"); FooSeq myLoanedDiscontiguousSequence7; myLoanedDiscontiguousSequence7.loan_discontiguous(foo_ptr_array, FOO_ARRAY_LENGTH, FOO_ARRAY_LENGTH); printf("Value before: myLoanedDiscontiguousSequence7[0] = { x = %d, y = %d } \n", myLoanedDiscontiguousSequence7[0].x, myLoanedDiscontiguousSequence7[0].y); printf("Value before: myLoanedDiscontiguousSequence7[5] = { x = %d, y = %d } \n", myLoanedDiscontiguousSequence7[5].x, myLoanedDiscontiguousSequence7[5].y); Foo **sequence_foo_ptr_array = myLoanedDiscontiguousSequence7.get_discontiguous_buffer(); Foo *tmp = sequence_foo_ptr_array[0]; sequence_foo_ptr_array[0] = sequence_foo_ptr_array[5]; sequence_foo_ptr_array[5] = tmp; // No need to call loan_discontiguous() or do anything else as we are directly accessing the array the // myLoanedDiscontiguousSequence7 uses internally. printf("Value after: myLoanedDiscontiguousSequence7[0] = { x = %d, y = %d } \n", myLoanedDiscontiguousSequence7[0].x, myLoanedDiscontiguousSequence7[0].y); printf("Value after: myLoanedDiscontiguousSequence7[5] = { x = %d, y = %d } \n", myLoanedDiscontiguousSequence7[5].x, myLoanedDiscontiguousSequence7[5].y); myLoanedDiscontiguousSequence7.unloan(); return 0; } int main(int argc, char *argv[]) { return sequence_example(); }