1 /*
  2  * =====================================================================================
  3  *
  4  *       Filename:  lvalue.c
  5  *
  6  *    Description:  test of vector being used as an lvalue
  7  *
  8  *        Version:  1.0
  9  *        Created:  11/28/2016 12:31:54 PM
 10  *       Revision:  none
 11  *       Compiler:  gcc
 12  *
 13  *         Author:  Ruben Safir (mn), ruben@mrbrklyn.com
 14  *        Company:  NYLXS Inc
 15  *
 16  * =====================================================================================
 17  */
 18 
 19 #include <iostream>
 20 #include <vector>
 21 
 22 namespace vect{
 23 
 24    /* 
 25    ¦* =====================================================================================
 26    ¦*        Class:  Lvalue
 27    ¦*  Description:  testing vector access as an lvalue
 28    ¦* =====================================================================================
 29    ¦*/
 30 
 31    template < class T >
 32       class Lvalue
 33       {
 34          public:
 35 
 36             // ====================  LIFECYCLE     =======================================
 37             Lvalue (){};                           /* constructor      */
 38             Lvalue (std::vector<T> in): _ofthings{in}{};                           /* constructor      */
 39             Lvalue ( const Lvalue &other ); /* copy constructor */
 40             ~Lvalue (){};                          /* destructor       */
 41 
 42             /* ====================  ACCESSORS     ======================================= */
 43             std::vector<T>&  ofthings(){
 44                return _ofthings;
 45             };
 46 
 47             void ofthings(std::vector<T> const vec){
 48                _ofthings = vec;
 49             };
 50             /* ====================  MUTATORS      ======================================= */
 51 
 52             /* ====================  OPERATORS     ======================================= */
 53 
 54             const Lvalue& operator = ( const Lvalue &other ); // assignment operator
 55             T& operator [] (int index)
 56             {
 57                return (ofthings()[index]);
 58             };
 59             friend  std::ostream &operator << (std::ostream &os, const Lvalue in)
 60             {
 61                for(int i = 0; i < in._ofthings.size(); i++ ){
 62                   std::cout << i << "\t";
 63                }
 64                return os;
 65             };
 66 
 67             /* ====================  DATA MEMBERS  ======================================= */
 68          protected:
 69             std::vector<T> _ofthings;
 70 
 71          private:
 72 
 73       }; /* -----  end of template class Lvalue  ----- */
 74 
 75 };
 76 
 77 
 78 int main ( int argc, char *argv[] )
 79 {
 80 
 81    std::cout << "TEST INPUT TO VECTOR" << std::endl;
 82    std::vector<int> test(100, 0);
 83    for(int i = 0; i<10; i++ ){
 84       test[i] = i;
 85       std::cout <<  i << "::" <<test[i] << "\t";
 86    }
 87    std::cout << std::endl;
 88    std::cout << "TEST INPUT TO OBJECT" << std::endl;
 89    vect::Lvalue<int> test2 {test};
 90    for(int j = 0, i=10; j < 10; i--, j++){
 91       test[j] = i;
 92       std::cout << "What is J" << j << std::endl;
 93       std::cout << "i: " << i << "     j: " << j << "     test[j]: " <<  test[j] << std::endl;
 94       std::cout << "i: " << i << "     j: " << j << "     test2[j]: " <<  test[j] << std::endl;
 95    }
 96    std::cout << std::endl;
 97    std::cout << "TEST INPUT TO OBJECT bidirectional" << std::endl;
 98    for(int i = 0, j=10; i < 10; i++, j++){
 99       test2[i] = j;
100       std::cout << "What is i ==>" << i << std::endl;
101       std::cout << "i: " << i << "     j: " << j << "     test2[i]: " <<  test2[i] << std::endl;
102       std::cout << "i: " << i << "     j: " << j << "     test[i]: " <<  test[i] << std::endl;
103    }
104    std::cout << std::endl;
105    std::cout << std::endl;
106    for(int j = 0, i=10; j < 10; i--, j++){
107       test[j] = i;
108       std::cout << "What is J" << j << std::endl;
109       std::cout << "i: " << i << "     j: " << j << "     test[j]: " <<  test[j] << std::endl;
110       std::cout << "i: " << i << "     j: " << j << "     test2[j]: " <<  test[j] << std::endl;
111    }
112    std::cout << "__DONE__" << std::endl;
113 
114 
115 
116    return EXIT_SUCCESS;
117 }           /* ----------  end of function main  ---------- */