IBR-DTNSuite  0.10
BundleBuilder.h
Go to the documentation of this file.
1 /*
2  * BundleBuilder.h
3  *
4  * Created on: 10.01.2013
5  * Author: morgenro
6  */
7 
8 #ifndef BUNDLEBUILDER_H_
9 #define BUNDLEBUILDER_H_
10 
11 #include <ibrdtn/data/Number.h>
12 #include <ibrdtn/data/Bundle.h>
13 #include <ibrdtn/data/Block.h>
14 #include <iterator>
15 #include <stdlib.h>
16 
17 namespace dtn
18 {
19  namespace data
20  {
21  class BundleBuilder {
22  public:
24  {
25  public:
26  DiscardBlockException(string what = "Block has been discarded.") throw() : dtn::SerializationFailedException(what)
27  {
28  };
29  };
30 
31  enum POSITION
32  {
36  };
37 
38  BundleBuilder(Bundle &target, POSITION alignment = END, int pos = 0);
39  virtual ~BundleBuilder();
40 
44  void clear();
45 
46  template <class T>
47  T& insert(const Bitset<Block::ProcFlags> &procflags);
48 
49  POSITION getAlignment() const;
50 
52 
57 
58  private:
59  Bundle *_target;
60 
61  POSITION _alignment;
62  int _pos;
63  };
64 
65  template <class T>
67  {
68  switch (_alignment)
69  {
70  case FRONT:
71  {
72  T &block = _target->push_front<T>();
73  bool last_block = block.get(dtn::data::Block::LAST_BLOCK);
74  block._procflags = procflags;
75  block.set(dtn::data::Block::LAST_BLOCK, last_block);
76  return block;
77  }
78 
79  case END:
80  {
81  T &block = _target->push_back<T>();
82  bool last_block = block.get(dtn::data::Block::LAST_BLOCK);
83  block._procflags = procflags;
84  block.set(dtn::data::Block::LAST_BLOCK, last_block);
85  return block;
86  }
87 
88  default:
89  if(_pos <= 0) {
90  T &block = _target->push_front<T>();
91  bool last_block = block.get(dtn::data::Block::LAST_BLOCK);
92  block._procflags = procflags;
93  block.set(dtn::data::Block::LAST_BLOCK, last_block);
94  return block;
95  }
96 
97  dtn::data::Bundle::iterator it = _target->begin();
98  std::advance(it, _pos-1);
99 
100  T &block = (it == _target->end()) ? _target->push_back<T>() : _target->insert<T>(it);
101 
102  bool last_block = block.get(dtn::data::Block::LAST_BLOCK);
103  block._procflags = procflags;
104  block.set(dtn::data::Block::LAST_BLOCK, last_block);
105  return block;
106  }
107  }
108  } /* namespace data */
109 } /* namespace dtn */
110 #endif /* BUNDLEBUILDER_H_ */