00001
00002
00003
00004
00005
00006
00007 #ifndef DSM_LAYER_HH
00008 #define DSM_LAYER_HH
00009
00010 #include <byteswap.h>
00011 #include <vector>
00012 #include <functional>
00013 #include <algorithm>
00014
00015 using namespace std;
00016
00017 #include "DSM.hh"
00018
00019 inline long long swapLL(long long x)
00020 {
00021 return ((x & 0xffff000000000000ll) >> 48 |
00022 (x & 0x0000ffff00000000ll) >> 16 |
00023 (x & 0x00000000ffff0000ll) << 16 |
00024 (x & 0x000000000000ffffll) << 48);
00025 }
00026
00027 inline void copy_and_swap16(void* dest, const void* src)
00028 {
00029 long long* x = (long long*)src;
00030 long long* y = (long long*)dest;
00031
00032
00033
00034
00035 *y = bswap_64(*x); ++x; ++y;
00036 *y = bswap_64(*x); ++x; ++y;
00037 }
00038
00039 inline void copy_and_swap8(void* dest, const void* src)
00040 {
00041 long long* x = (long long*)src;
00042 long long* y = (long long*)dest;
00043
00044 *y++ = swapLL(*x++);
00045 *y++ = swapLL(*x++);
00046 }
00047
00048 template<class T> struct DSMLayer : public vector<DSM> {
00049 DSMLayer(int n) : vector<DSM>(n) {}
00050 virtual ~DSMLayer() {}
00051 virtual void setRegister(int i, int value);
00052 virtual int getRegister(int i) const;
00053 virtual bool read(const T& event) = 0;
00054 virtual void write(DSMLayer& layer) = 0;
00055 virtual void run() = 0;
00056 virtual void save(int nchannels, short* buffer);
00057 virtual void dump() { for_each(begin(), end(), mem_fun_ref(&DSM::dump)); }
00058 };
00059
00060 template<class T> inline void DSMLayer<T>::setRegister(int i, int value)
00061 {
00062 for (vector<DSM>::iterator dsm = begin(); dsm != end(); ++dsm)
00063 dsm->registers[i] = value;
00064 }
00065
00066 template<class T> inline int DSMLayer<T>::getRegister(int i) const
00067 {
00068 return front().registers[i];
00069 }
00070
00071 template<class T> inline void DSMLayer<T>::save(int nchannels, short* buffer)
00072 {
00073 for (size_t dsm = 0; dsm < size(); ++dsm) {
00074 short* channels = (*this)[dsm].channels;
00075 copy(&channels[0], &channels[nchannels], &buffer[dsm*nchannels]);
00076 }
00077 }
00078
00079 #endif // DSM_LAYER_HH