TOBI Interface A  0.1
/home/breidi/Dropbox/libtia/src/tia/datapacket/raw_mem.cpp
Go to the documentation of this file.
00001 /*
00002     This file is part of the TOBI Interface A (TiA) library.
00003 
00004     Commercial Usage
00005     Licensees holding valid Graz University of Technology Commercial
00006     licenses may use this file in accordance with the Graz University
00007     of Technology Commercial License Agreement provided with the
00008     Software or, alternatively, in accordance with the terms contained in
00009     a written agreement between you and Graz University of Technology.
00010 
00011     --------------------------------------------------
00012 
00013     GNU Lesser General Public License Usage
00014     Alternatively, this file may be used under the terms of the GNU Lesser
00015     General Public License version 3.0 as published by the Free Software
00016     Foundation and appearing in the file lgpl.txt included in the
00017     packaging of this file.  Please review the following information to
00018     ensure the GNU General Public License version 3.0 requirements will be
00019     met: http://www.gnu.org/copyleft/lgpl.html.
00020 
00021     In case of GNU Lesser General Public License Usage ,the TiA library
00022     is distributed in the hope that it will be useful,
00023     but WITHOUT ANY WARRANTY; without even the implied warranty of
00024     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025     GNU General Public License for more details.
00026 
00027     You should have received a copy of the GNU Lesser General Public License
00028     along with the TiA library. If not, see <http://www.gnu.org/licenses/>.
00029 
00030     Copyright 2010 Graz University of Technology
00031     Contact: TiA@tobi-project.org
00032 */
00033 
00038 #include <iostream>
00039 #include <vector>
00040 
00041 #include <boost/numeric/conversion/cast.hpp>
00042 #include "tia-private/datapacket/raw_mem.h"
00043 
00044 namespace tia
00045 {
00046 
00047 using boost::numeric_cast;
00048 using boost::numeric::bad_numeric_cast;
00049 using boost::numeric::positive_overflow;
00050 using boost::numeric::negative_overflow;
00051 
00052 using boost::uint16_t;
00053 using boost::uint32_t;
00054 using boost::uint64_t;
00055 
00056 using std::vector;
00057 using std::cerr;
00058 
00059 //-----------------------------------------------------------------------------
00060 
00061 RawMem::RawMem(uint32_t flags, uint64_t sample_nr, uint64_t packet_nr, \
00062                 boost::posix_time::ptime timestamp, \
00063                 vector<uint16_t>& nr_values, vector<uint16_t>& nr_blocks,
00064                 vector<double>& data)  : size_(0)
00065 {
00066   size_ = sizeof(flags) + sizeof(sample_nr) + sizeof(packet_nr) + sizeof(timestamp) \
00067   + nr_blocks.size() * sizeof(nr_blocks[0]) \
00068   + nr_values.size() * sizeof(nr_values[0]) \
00069   + data.size() * sizeof(float);  // FIXXXXXME  ...  hardcoded sizeof() !!!!
00070 
00071   mem_ = malloc(size_);
00072 
00073   uint32_t* ui32_ptr = reinterpret_cast<uint32_t*>(mem_);
00074   *ui32_ptr++ = flags;
00075 
00076   uint64_t* ui64_ptr = reinterpret_cast<uint64_t*>(ui32_ptr);
00077   *ui64_ptr++ = sample_nr;
00078   *ui64_ptr++ = packet_nr;
00079 
00080   boost::posix_time::ptime* ptime_ptr
00081   = reinterpret_cast<boost::posix_time::ptime*>(ui64_ptr);
00082   *ptime_ptr++ = timestamp;
00083 
00084   uint16_t* ui16_ptr = reinterpret_cast<uint16_t*>(ptime_ptr);
00085 
00086   for(unsigned int n = 0; n < nr_values.size(); n++)
00087     *ui16_ptr++ = nr_values[n];
00088   for(unsigned int n = 0; n < nr_blocks.size(); n++)
00089     *ui16_ptr++ = nr_blocks[n];
00090 
00091   try
00092   {
00093     float* flt_ptr = reinterpret_cast<float*>(ui16_ptr);
00094     for(unsigned int n = 0; n < data.size(); n++)
00095       *flt_ptr++ = numeric_cast<float>(data[n]);
00096   }
00097   catch(negative_overflow& e)
00098   {
00099     cerr << "RawMem -- Constructor: " << e.what();
00100   }
00101   catch(positive_overflow& e)
00102   {
00103     cerr << "RawMem -- Constructor: " <<  e.what();
00104   }
00105   catch(bad_numeric_cast& e)
00106   {
00107     cerr << "RawMem -- Constructor: " <<  e.what();
00108   }
00109 }
00110 
00111 } // Namespace tobiss
00112 
00113 //-----------------------------------------------------------------------------
 All Data Structures Files Functions Variables Typedefs Enumerations