summaryrefslogtreecommitdiff
path: root/utils/swap_pod.h
blob: bb9a830d3a8f437ca22c705a15fceb29ae7f1508 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef SWAP_POD_H
#define SWAP_POD_H

//for swapping objects of the same concrete type where just swapping their bytes will work.  will at least work on plain old data.

#include <algorithm> // not used, but people who use this will want to bring std::swap in anyway
#include <cstring>

template <class T>
inline void swap_pod(T &a,T &b) {
  using namespace std;
  const unsigned s=sizeof(T);
  char tmp[s];
  void *pt=(void*)tmp;
  void *pa=(void*)&a;
  void *pb=(void*)&b;
  memcpy(pt,pa,s);
  memcpy(pa,pb,s);
  memcpy(pb,pt,s);
}


#endif