1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* @file DataHolder.h
*
* @author <a href="mailto:mellmann@informatik.hu-berlin.de">Heinrich Mellmann</a>
* @author <a href="mailto:krause@informatik.hu-berlin.de">Thomas Krause</a>
* 
*/

#ifndef _DataHolder_h_
#define _DataHolder_h_

#include "Representation.h"
#include "Tools/DataStructures/Printable.h"
#include "Tools/DataStructures/Serializer.h"

#include <type_traits>

namespace naoth
{
template<>
class Serializer<Representation>
{
public:
  static void serialize(const Representation& representation, std::ostream& stream) {
    representation.serialize(stream);
  }
  static void deserialize(std::istream& stream, Representation& representation) {
    representation.deserialize(stream);
  }
};
}

/**
 * Connects an arbitrary class with Representation
 */
template<class T>
class DataHolder: public Representation, public T
{
private:
  // creates an object of the data
  // (this requires the class T to have a default constructor)
  //T  data;

public:
  DataHolder(const std::string& name): Representation(name){}
  DataHolder(): Representation(typeid(T).name()){}
  virtual ~DataHolder(){}

  T& operator*() { return *this; }
  const T& operator*() const { return *this; }

  /** 
   * wrap the print, fromDataStream and toDataStream of the data member 
   */
  virtual void print(std::ostream& stream) const {
    std::conditional<std::is_base_of<Printable,T>::value, T, Representation>::type::print(stream);
  }

  virtual bool serializable() const {
    return !std::is_base_of<naoth::EmptySerializer,naoth::Serializer<T> >::value;
  }

  void serialize(MsgOut<Representation>::type& msg) const {
    naoth::Serializer<T>::serialize(*this, msg);
  }

  void deserialize(MsgIn<Representation>::type& msg) {
    naoth::Serializer<T>::deserialize(msg, *this);
  }
};


#endif  /* _DataHolder_h_ */