ESA JPIP server  0.1
cache_model.h
Go to the documentation of this file.
1 #ifndef _JPIP_CACHE_MODEL_H_
2 #define _JPIP_CACHE_MODEL_H_
3 
4 
5 #include <vector>
6 #include <iostream>
7 #include <limits.h>
8 #include "base.h"
9 #include "jpip/jpip.h"
10 #include "data/serialize.h"
11 
12 
13 namespace jpip
14 {
15 
16  using namespace std;
17  using namespace data;
18 
19 
24  template<int BIN_CLASS> struct DataBinSelector
25  {
26  };
27 
28 
38  class CacheModel
39  {
40  public:
45  class Codestream
46  {
47  private:
48  int header;
50  vector<int> precincts;
51 
61 
62  public:
67  {
68  header = 0;
69  tile_header = 0;
70  min_precinct = 0;
71  }
72 
76  Codestream(const Codestream& model)
77  {
78  *this = model;
79  }
80 
85  {
86  header = model.header;
87  tile_header = model.tile_header;
88  min_precinct = model.min_precinct;
89  base::copy(precincts, model.precincts);
90 
91  return *this;
92  }
93 
98  {
99  AddToMainHeader(model.header);
100  AddToTileHeader(model.tile_header);
101 
102  for(int i = 0; i < (int)model.precincts.size(); i++)
103  AddToPrecinct(model.min_precinct + i, model.precincts[i]);
104 
105  return *this;
106  }
107 
108  template<typename T> T& SerializeWith(T& stream)
109  {
110  return (stream & header & tile_header & min_precinct & precincts);
111  }
112 
116  int GetMainHeader() const
117  {
118  return header;
119  }
120 
124  int GetTileHeader() const
125  {
126  return tile_header;
127  }
128 
136  int AddToMainHeader(int amount, bool complete = false)
137  {
138  if (header != INT_MAX)
139  {
140  if (complete || (amount == INT_MAX)) header = INT_MAX;
141  else header += amount;
142  }
143 
144  return header;
145  }
146 
154  int AddToTileHeader(int amount, bool complete = false)
155  {
156  if (tile_header != INT_MAX)
157  {
158  if (complete || (amount == INT_MAX)) tile_header = INT_MAX;
159  else tile_header += amount;
160  }
161 
162  return tile_header;
163  }
164 
169  int GetPrecinct(int num_precinct)
170  {
171  if (num_precinct < min_precinct) return INT_MAX;
172  else
173  {
174  int n = num_precinct - min_precinct;
175 
176  if (n >= (int) precincts.size()) precincts.resize(n + 1, 0);
177 
178  return precincts[n];
179  }
180  }
181 
190  int AddToPrecinct(int num_precinct, int amount, bool complete = false)
191  {
192  if (num_precinct < min_precinct) return INT_MAX;
193  else
194  {
195  int n = num_precinct - min_precinct;
196 
197  if (n >= (int) precincts.size()) precincts.resize(n + 1, 0);
198 
199  int& p = precincts[n];
200 
201  if (p != INT_MAX)
202  {
203  if (complete || (amount == INT_MAX)) p = INT_MAX;
204  else p += amount;
205  }
206 
207  return p;
208  }
209  }
210 
219  void Pack(int min_sum = 1)
220  {
221  int sum = 0;
222 
223  for (int i = 0; i < (int) precincts.size(); i++)
224  {
225  if (precincts[i] == INT_MAX) sum++;
226  else break;
227  }
228 
229  if (sum >= min_sum)
230  {
231  precincts.erase(precincts.begin(), precincts.begin() + sum);
232  min_precinct += sum;
233  }
234  }
235  };
236 
237  private:
241  bool full_meta;
242 
246  vector<int> meta_data;
247 
251  vector<Codestream> codestreams;
252 
253  public:
258  {
259  full_meta = false;
260  }
261 
265  CacheModel(const CacheModel& model)
266  {
267  *this = model;
268  }
269 
274  {
275  full_meta = model.full_meta;
276  base::copy(meta_data, model.meta_data);
277  base::copy(codestreams, model.codestreams);
278 
279  return *this;
280  }
281 
286  {
287  if(!full_meta) {
288  for(int i = 0; i < (int)model.meta_data.size(); i++)
289  AddToMetadata(i, model.meta_data[i]);
290  }
291 
292  for(int i = 0; i < (int)model.codestreams.size(); i++)
293  GetCodestream(i) += model.codestreams[i];
294 
295  return *this;
296  }
297 
298  template<typename T> T& SerializeWith(T& stream)
299  {
300  return (stream & full_meta & meta_data & codestreams);
301  }
302 
307  Codestream& GetCodestream(int num_codestream)
308  {
309  if (num_codestream >= (int) codestreams.size()) codestreams.resize(num_codestream + 1);
310 
311  return codestreams[num_codestream];
312  }
313 
318  int GetMetadata(int id)
319  {
320  if(full_meta) return INT_MAX;
321  else {
322  if(id >= (int)meta_data.size()) meta_data.resize(id + 1, 0);
323  return meta_data[id];
324  }
325  }
326 
335  int AddToMetadata(int id, int amount, bool complete = false)
336  {
337  if(full_meta) return INT_MAX;
338  else {
339  if (GetMetadata(id) != INT_MAX)
340  {
341  if (complete || (amount == INT_MAX)) meta_data[id] = INT_MAX;
342  else meta_data[id] += amount;
343  }
344 
345  return meta_data[id];
346  }
347  }
348 
355  template<int BIN_CLASS> int GetDataBin(int num_codestream, int id)
356  {
357  return DataBinSelector<BIN_CLASS>::Get(*this, num_codestream, id);
358  }
359 
370  template<int BIN_CLASS> int AddToDataBin(int num_codestream, int id, int amount, bool complete = false)
371  {
372  return DataBinSelector<BIN_CLASS>::AddTo(*this, num_codestream, id, amount, complete);
373  }
374 
378  bool IsFullMetadata() const
379  {
380  return full_meta;
381  }
382 
387  {
388  full_meta = true;
389  meta_data.clear();
390  }
391 
395  void Pack(int min_sum = 1)
396  {
397  for (vector<Codestream>::iterator i = codestreams.begin(); i != codestreams.end(); i++)
398  i->Pack(min_sum);
399  }
400 
404  void Clear()
405  {
406  full_meta = false;
407  meta_data.clear();
408  codestreams.clear();
409  }
410 
411  virtual ~CacheModel()
412  {
413  }
414  };
415 
416  template<> struct DataBinSelector<DataBinClass::META_DATA>
417  {
418  static int Get(CacheModel& model, int num_codestream, int id)
419  {
420  return model.GetMetadata(id);
421  }
422 
423  static int AddTo(CacheModel& model, int num_codestream, int id, int amount, bool complete)
424  {
425  return model.AddToMetadata(id, amount, complete);
426  }
427  };
428 
429  template<> struct DataBinSelector<DataBinClass::MAIN_HEADER>
430  {
431  static int Get(CacheModel& model, int num_codestream, int id)
432  {
433  return model.GetCodestream(num_codestream).GetMainHeader();
434  }
435 
436  static int AddTo(CacheModel& model, int num_codestream, int id, int amount, bool complete)
437  {
438  return model.GetCodestream(num_codestream).AddToMainHeader(amount, complete);
439  }
440  };
441 
442  template<> struct DataBinSelector<DataBinClass::TILE_HEADER>
443  {
444  static int Get(CacheModel& model, int num_codestream, int id)
445  {
446  return model.GetCodestream(num_codestream).GetTileHeader();
447  }
448 
449  static int AddTo(CacheModel& model, int num_codestream, int id, int amount, bool complete)
450  {
451  return model.GetCodestream(num_codestream).AddToTileHeader(amount, complete);
452  }
453  };
454 
455  template<> struct DataBinSelector<DataBinClass::PRECINCT>
456  {
457  static int Get(CacheModel& model, int num_codestream, int id)
458  {
459  return model.GetCodestream(num_codestream).GetPrecinct(id);
460  }
461 
462  static int AddTo(CacheModel& model, int num_codestream, int id, int amount, bool complete)
463  {
464  return model.GetCodestream(num_codestream).AddToPrecinct(id, amount, complete);
465  }
466  };
467 
468 }
469 
470 #endif /* _JPIP_CACHE_MODEL_H_ */
static void copy(std::vector< T > &dest, const std::vector< T > &src)
Copies a vector.
Definition: base.h:30
static int AddTo(CacheModel &model, int num_codestream, int id, int amount, bool complete)
Definition: cache_model.h:462
vector< Codestream > codestreams
Amounts for the codestreams.
Definition: cache_model.h:251
static int Get(CacheModel &model, int num_codestream, int id)
Definition: cache_model.h:431
The cache model of a JPIP client is handled using this class.
Definition: cache_model.h:38
vector< int > meta_data
Amounts for the meta-datas.
Definition: cache_model.h:246
static int Get(CacheModel &model, int num_codestream, int id)
Definition: cache_model.h:457
Codestream & operator+=(const Codestream &model)
Add the content of the given codestream cache model.
Definition: cache_model.h:97
static int Get(CacheModel &model, int num_codestream, int id)
Definition: cache_model.h:444
static int Get(CacheModel &model, int num_codestream, int id)
Definition: cache_model.h:418
Codestream(const Codestream &model)
Copy constructor.
Definition: cache_model.h:76
Codestream()
Initializes all the members to zero.
Definition: cache_model.h:66
bool full_meta
Says if the meta-data has been totally sent.
Definition: cache_model.h:241
CacheModel(const CacheModel &model)
Copy constructor.
Definition: cache_model.h:265
T & SerializeWith(T &stream)
Definition: cache_model.h:298
int GetTileHeader() const
Returns the amount of the tile header.
Definition: cache_model.h:124
static int AddTo(CacheModel &model, int num_codestream, int id, int amount, bool complete)
Definition: cache_model.h:449
int header
Amount for the header.
Definition: cache_model.h:48
void Clear()
Clear all the amounts.
Definition: cache_model.h:404
static int AddTo(CacheModel &model, int num_codestream, int id, int amount, bool complete)
Definition: cache_model.h:436
int AddToMainHeader(int amount, bool complete=false)
Increases the amount of the main header.
Definition: cache_model.h:136
int AddToPrecinct(int num_precinct, int amount, bool complete=false)
Increases the amount of a precinct.
Definition: cache_model.h:190
CacheModel & operator+=(const CacheModel &model)
Add the content of the given cache model.
Definition: cache_model.h:285
int GetDataBin(int num_codestream, int id)
Returns the amount of a data-bin item using the class DataBinSelector.
Definition: cache_model.h:355
Codestream & operator=(const Codestream &model)
Copy assignment.
Definition: cache_model.h:84
virtual ~CacheModel()
Definition: cache_model.h:411
void Pack(int min_sum=1)
Packs the information stored regarding the precincts, removing those initial elements that are consec...
Definition: cache_model.h:219
bool IsFullMetadata() const
Returns the full flag of the meta-datas.
Definition: cache_model.h:378
Template class that is specialized for allowing basic operations (add and get) with cache models depe...
Definition: cache_model.h:24
static int AddTo(CacheModel &model, int num_codestream, int id, int amount, bool complete)
Definition: cache_model.h:423
void Pack(int min_sum=1)
Calls the Pack method of all the codestreams.
Definition: cache_model.h:395
int tile_header
Amount for the tile-header.
Definition: cache_model.h:49
T & SerializeWith(T &stream)
Definition: cache_model.h:108
void SetFullMetadata()
Sets the full flag for the meta-datas to true.
Definition: cache_model.h:386
vector< int > precincts
Amount for the precincts.
Definition: cache_model.h:50
CacheModel()
Empty constructor.
Definition: cache_model.h:257
int GetMetadata(int id)
Returns the amount of a meta-data.
Definition: cache_model.h:318
Class that contains the definitions of all the data-bin classes defined for the JPIP protocol...
Definition: jpip.h:18
int min_precinct
Minimum identifier of the non-consecutive precinct completely sent.
Definition: cache_model.h:60
int AddToDataBin(int num_codestream, int id, int amount, bool complete=false)
Increases the amount of a data-bin item using the class DataBinSelector.
Definition: cache_model.h:370
int AddToMetadata(int id, int amount, bool complete=false)
Increases the amount of a meta-data.
Definition: cache_model.h:335
int GetMainHeader() const
Returns the amount of the main header.
Definition: cache_model.h:116
Codestream & GetCodestream(int num_codestream)
Returns the reference of a codestream.
Definition: cache_model.h:307
Sub-class of the cache model class used to identify a codestream.
Definition: cache_model.h:45
CacheModel & operator=(const CacheModel &model)
Copy assignment.
Definition: cache_model.h:273
int GetPrecinct(int num_precinct)
Returns the amount of a precinct.
Definition: cache_model.h:169
int AddToTileHeader(int amount, bool complete=false)
Increases the amount of the tile header.
Definition: cache_model.h:154