Various C++ Examples (including IPC)  Version: 1.0.0
simpleTemplate.cc
Go to the documentation of this file.
1 
5 #include <iostream>
6 #include <string.h>
7 
32 template <class T> int Tmycpy( T a, T b, size_t size, int numElements)
33 {
34  //set the number of elements copied to the initial 1 (an unconditional
35  //copy is performed later on)
36  int count = 1;
37 
38  //print out the array using the numElements
39  std::cout << "INITIAL ARRAY CONTENTS: " << "a=" ;
40  for(int i=0; i < numElements; i++)
41  std::cout << a[i];
42 
43  std::cout << "|b=";
44 
45  for(int i=0; i < numElements; i++)
46  std::cout << b[i];
47 
48  std::cout << "|"
49  << "size=" << size
50  << "|"
51  << "numElements=" << numElements
52  << std::endl;
53 
54  //range check the arrays
55  //if( ! sizeof(b) >= sizeof(a))
56  //return -1;
57 
58  //create a couple of pointers (assuming we can -if we passed a
59  //reference we might be in trouble --> type checking and
60  //specialization might be a better option for this template...)
61  T p1 =a;
62  T p2 =b;
63 
64  //do an unconditional copy until a NULL Terminator is found
65  //(inclusive) and count the elements copied
66  while(*p2++ = *p1++)
67  count++;
68 
69  //compare the arrays
70  if(memcmp(static_cast<void *>(a), static_cast<void *>(b), size) != 0)
71  return -1;
72 
73  //return number of elements copied
74  return count;
75 }
76 
77 int main(int argc, char *argv[])
78 {
79  //string literal copy to a character array
80 
81  char ca1[] = {"123456789"};
82  char ca2[sizeof(ca1)]; // create an array that is the size of
83  // ca1.
84 
85  memset(ca2, 'x', sizeof(ca2)); // set the contents of ca2 to a bunch
86  // of char 'x' so we can "watch" what
87  // happens.
88 
89  //call our template function
90  int i = Tmycpy<char *>(ca1, ca2, sizeof(ca2), sizeof(ca2)/sizeof(char));
91 
92  //check the data
93  if(i != -1)
94  std::cout << "SUCCESS: "
95  << "ca1=" << ca1
96  << "|"
97  << "ca2=" << ca2
98  << "|"
99  << "elements copied=" << i
100  << std::endl;
101 
102  std::cout << "#############################" << std::endl;
103 
104  // int array copy (upto a found \0)
105  int ia[] = {9,8,7,6,5,4,3,2,1,0};
106  int ib[] = {0,0,0,0,0,0,0,0,0,4}; // initialize to all \0 and a 4 at
107  // the end -for range checking
108  // demonstration purposes in the
109  // template
110 
111  //call our template function to make the copy
112  i = Tmycpy<int *>(ia, ib, sizeof(ib), sizeof(ib)/sizeof(*ib));
113 
114 
115  //check status and display contents
116  if(i != -1)
117  std::cout << "SUCCESS: int array works too"
118  << "|"
119  << "elements copied=" << i
120  << std::endl;
121 
122  std::cout << "SECOND ARRAY CONTENTS: " << "ia=" ;
123  for(int j=0; j < i; j++)
124  std::cout << ia[j];
125 
126  std::cout << "|ib=";
127 
128  for(int j=0; j < i; j++)
129  std::cout << ib[j];
130 
131  std::cout << std::endl;
132 
133  std::cout << "#############################" << std::endl;
134 
135  //return success
136  return 0;
137 }
int Tmycpy(T a, T b, size_t size, int numElements)
Template function to copy a string literal.
int main(int argc, char *argv[])