OverSim
BinaryValue.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2007 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
24 #include <iterator>
25 
26 #include <cnetcommbuffer.h>
27 #include "BinaryValue.h"
28 
29 using namespace std;
30 
31 // predefined BinaryValue
33 
35 {
36 };
37 
38 BinaryValue::BinaryValue(size_t n): vector<char>(n)
39 {
40 };
41 
42 BinaryValue::BinaryValue(const std::string& str) : vector<char>(str.begin(), str.end())
43 {
44 };
45 
46 BinaryValue::BinaryValue(const std::vector<char>& v) : vector<char>(v)
47 {
48 };
49 
50 BinaryValue::BinaryValue(const char* cStr)
51 {
52  *this = BinaryValue(cStr, strlen(cStr));
53 };
54 
55 BinaryValue::BinaryValue(const char* b, const size_t l) : vector<char>(b, b+l)
56 {
57 };
58 
60 {
61  packObject(obj);
62 };
63 
65 {
66  insert(end(), rhs.begin(), rhs.end());
67  return *this;
68 }
69 
71 {
72  return empty();
73 }
74 
75 // Allow output of vector<char> using normal notation
76 std::ostream& operator << (std::ostream& os, const BinaryValue& v) {
77  copy(v.begin(), v.end(), ostream_iterator<char>(os, ""));
78  return os; // To allow (cout << a) << b;
79 }
80 
82 {
83  size_type minSize = min(this->size(), rhs.size());
84  for (size_type i=0; i<minSize; i++) {
85  if ((*this)[i] < rhs[i]) {
86  return true;
87  } else if ((*this)[i] > rhs[i]) {
88  return false;
89  }
90  }
91 
92  return (this->size() < rhs.size()) ? true : false;
93 }
94 
95 void BinaryValue::netPack(cCommBuffer *b)
96 {
97  doPacking(b,(uint16_t)size());
98  doPacking(b, data(), size());
99 }
100 
101 void BinaryValue::netUnpack(cCommBuffer *b)
102 {
103  uint16_t size;
104  doUnpacking(b, size);
105  resize(size);
106  doUnpacking(b, data(), size);
107 }
108 
109 void BinaryValue::packObject(cObject* obj)
110 {
111  cNetCommBuffer* b = new cNetCommBuffer();
112  b->reset();
113  b->packObject(obj);
114  resize(b->getMessageSize());
115  memcpy(data(), b->getBuffer(), b->getMessageSize());
116  delete b;
117 }
118 
120 {
121  cNetCommBuffer* b = new cNetCommBuffer();
122  cObject* obj;
123 
124  b->reset();
125  b->allocateAtLeast(size());
126  memcpy(b->getBuffer(), data(), size());
127  b->setMessageSize(size());
128 
129  obj = b->unpackObject();
130 
131  delete b;
132 
133  return obj;
134 }