RawDAQDataConstructor.h
Go to the documentation of this file.
1 /*
2  * File: RawDAQDataConstructor.h
3  * Author: denis
4  *
5  * This is a base class for RawDAQData class constructor
6  *
7  * Created on September 20, 2012, 2:09 PM
8  */
9 
10 #ifndef RAWDAQDATACONSTRUCTOR_H
11 #define RAWDAQDATACONSTRUCTOR_H
12 #include "DAQDataFormats/RawDAQData.h"
13 #include "UnitTestTrace.h"
15 
16 #include <boost/lexical_cast.hpp>
17 
18 #define CHECK_FIELD(FIELD_NAME) \
19  bool checkField_##FIELD_NAME(daqdataformats::CURRENT_CLASS* dataformat) const { \
20  if(!setFieldToTestValue_##FIELD_NAME(dataformat)) { \
21  LOG_ERROR("UNITTEST") << std::endl<< __PRETTY_FUNCTION__ \
22  <<"\nsetFieldToTestValue_"<<#FIELD_NAME<<" failed.\n" \
23  <<"Test value = "<<TEST_VALUE_##FIELD_NAME<<"\n"; \
24  if(getVersion(dataformat) > rawdaqdataformat::DAQDATAFORMAT_UNKNOWN_VERSION)\
25  LOG_ERROR("UNITTEST")<<"version = "<<getVersion(dataformat)<<"\n"; \
26  return false; \
27  } \
28  if(getFieldValue_##FIELD_NAME(dataformat) != TEST_VALUE_##FIELD_NAME){ \
29  LOG_ERROR("UNITTEST") << std::endl<< __PRETTY_FUNCTION__ \
30  <<"\ngetFieldToTestValue_"<<#FIELD_NAME<<" failed.\n" \
31  <<"Test value = "<<TEST_VALUE_##FIELD_NAME \
32  <<" but get is " \
33  << getFieldValue_##FIELD_NAME(dataformat)<<"\n"; \
34  if(getVersion(dataformat) > rawdaqdataformat::DAQDATAFORMAT_UNKNOWN_VERSION)\
35  LOG_ERROR("UNITTEST")<<"version = "<<getVersion(dataformat)<<"\n"; \
36  return false; \
37  } \
38  \
39  std::string suceed_string = "Checking BitField in "; \
40  suceed_string.append(__PRETTY_FUNCTION__); \
41  suceed_string.append(" field "); \
42  suceed_string.append(#FIELD_NAME); \
43  suceed_string.append(" suceeded for version = "); \
44  suceed_string.append(boost::lexical_cast<std::string>(getVersion(dataformat))); \
45  suceed_string.append(" (set to "); \
46  suceed_string.append(boost::lexical_cast<std::string>(TEST_VALUE_##FIELD_NAME)); \
47  suceed_string.append(" received "); \
48  suceed_string.append(boost::lexical_cast<std::string>(getFieldValue_##FIELD_NAME(dataformat))); \
49  suceed_string.append(")."); \
50  \
51  if(!setFieldToZeroValue_##FIELD_NAME(dataformat)){ \
52  LOG_ERROR("UNITTEST")<<"\nsetFieldToZeroValue_"<<#FIELD_NAME<<" failed.\n" \
53  <<"version = "<<getVersion(dataformat)<<"\n"; \
54  return false; \
55  } \
56  if(getFieldValue_##FIELD_NAME(dataformat) != 0) { \
57  LOG_ERROR("UNITTEST")<<"\ngetFieldValue_"<<#FIELD_NAME<<" failed.\n" \
58  <<"Should be zero" \
59  <<" but get is "<<getFieldValue_##FIELD_NAME(dataformat)<<"\n" \
60  <<"version = "<<getVersion(dataformat)<<"\n"; \
61  return false; \
62  } \
63  LOG_DEBUG("UNITTEST")<< suceed_string; \
64  return true; \
65  }
66 
67 #define DEFINE_TESTVALUE_CUSTOM(FIELD_NAME, TYPE) \
68  bool setFieldToTestValue_##FIELD_NAME(daqdataformats::CURRENT_CLASS* dataformat) const {return dataformat->set##FIELD_NAME(boost::lexical_cast< TYPE >(TEST_VALUE_##FIELD_NAME));} \
69  bool setFieldToZeroValue_##FIELD_NAME(daqdataformats::CURRENT_CLASS* dataformat) const {return dataformat->set##FIELD_NAME(boost::lexical_cast< TYPE >(0));} \
70  uint64_t getFieldValue_##FIELD_NAME(const daqdataformats::CURRENT_CLASS* dataformat) const {return boost::lexical_cast<uint64_t> (dataformat->get##FIELD_NAME());} \
71  CHECK_FIELD(FIELD_NAME)
72 
73 #define DEFINE_TESTVALUE(FIELD_NAME) DEFINE_TESTVALUE_CUSTOM(FIELD_NAME, uint32_t)
74 #define DEFINE_TESTVALUE_BOOL(FIELD_NAME) DEFINE_TESTVALUE_CUSTOM(FIELD_NAME, bool)
75 #define DEFINE_TESTVALUE_64(FIELD_NAME) DEFINE_TESTVALUE_CUSTOM(FIELD_NAME, uint64_t)
76 
77 #define DEFINE_TESTVALUE_IS(FIELD_NAME) \
78  bool setFieldToTestValue_##FIELD_NAME(daqdataformats::CURRENT_CLASS* dataformat) const {return dataformat->set##FIELD_NAME(boost::lexical_cast<bool>(TEST_VALUE_##FIELD_NAME));} \
79  bool setFieldToZeroValue_##FIELD_NAME(daqdataformats::CURRENT_CLASS* dataformat) const {return dataformat->set##FIELD_NAME(boost::lexical_cast<bool>(0));} \
80  uint64_t getFieldValue_##FIELD_NAME(const daqdataformats::CURRENT_CLASS* dataformat) const {return boost::lexical_cast<uint64_t> (dataformat->is##FIELD_NAME());} \
81  CHECK_FIELD(FIELD_NAME)
82 
83 
84 /// Assert checking get/set of the the field is good
85 #define ASSERT_CHECK_FIELD(FIELD_NAME) CPPUNIT_ASSERT(_mainDFConstructor->checkField_##FIELD_NAME(df));
86 
87 #define ASSERT_PRINT_IS_NOT_UNKNOWN CPPUNIT_ASSERT(!isPrintDataFormatUnknownVersion(df));
88 
89 /// Assert that the get method for a field is not allowed
90 #define ASSERT_GET_IS_NOT_ALLOWED(FIELD_NAME) \
91  { \
92  bool succeed = true; \
93  try{ \
94  _mainDFConstructor->getFieldValue_##FIELD_NAME(df); \
95  } \
96  catch(daqdataformats::DataFormatException& e){ \
97  succeed = false; \
98  } \
99  if(succeed){ \
100  LOG_ERROR("UNITTEST")<<"Getting " \
101  <<#FIELD_NAME \
102  <<" succeded." \
103  <<"Should have failed\n"; \
104  } \
105  CPPUNIT_ASSERT(!succeed); \
106  }
107 
108 /// Assert that the set method for a field is not allowed
109 #define ASSERT_SET_IS_NOT_ALLOWED(FIELD_NAME) \
110  { \
111  bool succeed = true; \
112  try{ \
113  _mainDFConstructor->setFieldToTestValue_##FIELD_NAME(df); \
114  } \
115  catch(daqdataformats::DataFormatException& e){ \
116  succeed = false; \
117  } \
118  if(succeed){ \
119  LOG_ERROR("UNITTEST")<<"Setting " \
120  <<#FIELD_NAME \
121  <<" succeded." \
122  <<"Should have failed\n"; \
123  } \
124  CPPUNIT_ASSERT(!succeed); \
125  }
126 
127 /// Assert that both get and set methods for a field are not allowed
128 #define ASSERT_GETANDSET_IS_NOT_ALLOWED(FIELD_NAME) ASSERT_GET_IS_NOT_ALLOWED(FIELD_NAME) \
129  ASSERT_SET_IS_NOT_ALLOWED(FIELD_NAME)
130 
131 
132 
133 namespace daqdataformats {
135 public:
138  virtual ~RawDAQDataConstructor();
139 
140  /// method to create a filled dataformat with a given version
141  ///
142  virtual daqdataformats::RawDAQData* getFilledDataFormat(const version_t) const = 0;
143  /// method to create an empty dataformat with a given version
144  virtual daqdataformats::RawDAQData* getEmptyDataFormat(const version_t) const = 0;
145 
146  /// method to Fill the input dataformat
147  virtual bool fillDataFormat(daqdataformats::RawDAQData*)const = 0;
148  /// Get version of the input dataformat
149  virtual version_t getVersion(const daqdataformats::RawDAQData*) const { return -1;}
150 
151  /// Get maximum version for the dataformat
153 
154 protected:
155  /// Bool to uint abd backwards conversions
156  uint32_t boolToUint32_t (const bool number ) const { return ((number) ? 1 : 0); }
157  bool uint32_tToBool (const uint32_t number ) const { return ((number) ? true : false); }
158 
159 protected:
160 
161  /// Maximum version of the dataformat
163 };
164 }// end of namespace
165 
166 #endif /* RAWDAQDATACONSTRUCTOR_H */
167 
virtual daqdataformats::RawDAQData * getFilledDataFormat(const version_t) const =0
virtual daqdataformats::RawDAQData * getEmptyDataFormat(const version_t) const =0
method to create an empty dataformat with a given version
uint32_t boolToUint32_t(const bool number) const
Bool to uint abd backwards conversions.
virtual version_t getVersion(const daqdataformats::RawDAQData *) const
Get version of the input dataformat.
version_t getMaximumVersion() const
Get maximum version for the dataformat.
virtual bool fillDataFormat(daqdataformats::RawDAQData *) const =0
method to Fill the input dataformat
version_t _maximumVersion
Maximum version of the dataformat.
bool uint32_tToBool(const uint32_t number) const
int32_t version_t
Definition: RawDAQData.h:72
Class to hold the data from the FEBs in correct formats.