inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/buffer
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_buffer
00005 #define inlib_buffer
00006 
00007 #include "platform"
00008 
00009 #include <cstring>
00010 #include <cstddef>
00011 
00012 namespace inlib {
00013 
00014 template <class T>
00015 inline unsigned int write_buffer(T a_value,char* a_buffer) {
00016   size_t n = sizeof(T);
00017   //NOTE : a_buffer must be allocated to n.
00018   if(is_little_endian()) {
00019     ::memcpy(a_buffer,&a_value,n);
00020   } else {
00021     char* pos = (char*)&a_value;
00022     for(unsigned int i=0;i<n;i++,pos++) a_buffer[n-1-i] = *pos;
00023   }
00024   return n;
00025 }
00026 
00027 template <class T>
00028 inline unsigned int read_buffer(const char* a_buffer,T& a_value) {
00029   size_t n = sizeof(T);
00030   //NOTE : a_buffer must be allocated to n.
00031   if(is_little_endian()) {
00032     ::memcpy(&a_value,a_buffer,n);
00033   } else {
00034     char* pos = (char*)&a_value;
00035     for(unsigned int i=0;i<n;i++,pos++) *pos = a_buffer[n-1-i];
00036   }
00037   return n;
00038 }
00039 
00040 }
00041 
00042 #endif
00043 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines