|
| 1 | +// Copyright CERN and copyright holders of ALICE O2. This software is |
| 2 | +// distributed under the terms of the GNU General Public License v3 (GPL |
| 3 | +// Version 3), copied verbatim in the file "COPYING". |
| 4 | +// |
| 5 | +// See http://alice-o2.web.cern.ch/license for full licensing information. |
| 6 | +// |
| 7 | +// In applying this license CERN does not waive the privileges and immunities |
| 8 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 9 | +// or submit itself to any jurisdiction. |
| 10 | + |
| 11 | +/// @brief O2 data header classes, header example |
| 12 | +/// |
| 13 | +/// origin: CWG4 |
| 14 | +/// @author Mikolaj Krzewicki, mkrzewic@cern.ch |
| 15 | +/// @author Matthias Richter, Matthias.Richter@cern.ch |
| 16 | +/// @author David Rohr, drohr@cern.ch |
| 17 | + |
| 18 | +#ifndef NAMEHEADER_H |
| 19 | +#define NAMEHEADER_H |
| 20 | + |
| 21 | +#include <string> |
| 22 | +#include "Headers/DataHeader.h" |
| 23 | + |
| 24 | +namespace o2 { |
| 25 | +namespace Header { |
| 26 | + |
| 27 | +/// @struct NameHeader |
| 28 | +/// @brief an example data header containing a name of an object as a null terminated char arr. |
| 29 | +/// this is a template! at instantiation the template parameter determines the |
| 30 | +/// size of the held string array. |
| 31 | +/// a caveat with decoding is you have to use Header::get<NameHeader<0>>(buffer) |
| 32 | +/// to get it out of a buffer. May improve in the future if enough people complain. |
| 33 | +/// @ingroup aliceo2_dataformats_dataheader |
| 34 | +template <size_t N> |
| 35 | +struct NameHeader : public BaseHeader { |
| 36 | + static const uint32_t sVersion; |
| 37 | + static const o2::Header::HeaderType sHeaderType; |
| 38 | + static const o2::Header::SerializationMethod sSerializationMethod; |
| 39 | + NameHeader() |
| 40 | + : BaseHeader(sizeof(NameHeader), sHeaderType, sSerializationMethod, sVersion) |
| 41 | + , name() |
| 42 | + { |
| 43 | + memset(&name[0],'\0',N); |
| 44 | + } |
| 45 | + |
| 46 | + NameHeader(std::string in) |
| 47 | + : BaseHeader(sizeof(NameHeader), sHeaderType, sSerializationMethod, sVersion) |
| 48 | + , name() |
| 49 | + { |
| 50 | + // here we actually want a null terminated string |
| 51 | + strncpy(name, in.c_str(), N); |
| 52 | + name[N-1] = '\0'; |
| 53 | + } |
| 54 | + |
| 55 | + NameHeader& operator=(const std::string string) { |
| 56 | + std::copy(string.begin(), string.begin()+N, name); |
| 57 | + return *this; |
| 58 | + } |
| 59 | +private: |
| 60 | + char name[N]; |
| 61 | +}; |
| 62 | + |
| 63 | +template <size_t N> |
| 64 | +const o2::Header::HeaderType NameHeader<N>::sHeaderType = "NameHead"; |
| 65 | + |
| 66 | +// dirty trick to always have access to the headertypeID of a templated header type |
| 67 | +// TODO: find out if this can be done in a nicer way + is this realy necessary? |
| 68 | +template <> |
| 69 | +const o2::Header::HeaderType NameHeader<0>::sHeaderType; |
| 70 | + |
| 71 | +template <size_t N> |
| 72 | +const SerializationMethod NameHeader<N>::sSerializationMethod = gSerializationMethodNone; |
| 73 | + |
| 74 | +template <size_t N> |
| 75 | +const uint32_t NameHeader<N>::sVersion = 1; |
| 76 | + |
| 77 | +} |
| 78 | +} |
| 79 | + |
| 80 | +#endif //NAMEHEADER_H |
0 commit comments