BinaryValue.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2007 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00024 #include <iterator>
00025 
00026 #include "BinaryValue.h"
00027 
00028 using namespace std;
00029 
00030 // predefined BinaryValue
00031 const BinaryValue BinaryValue::UNSPECIFIED_VALUE;
00032 
00033 BinaryValue::BinaryValue(const char* s): vector<char>(strlen(s))
00034 {
00035     copy(s, s+strlen(s), begin());  // Inherits vector<char>::begin()
00036 }
00037 
00038 BinaryValue::BinaryValue(size_t n): vector<char>(n)
00039 {
00040 }
00041 
00042 BinaryValue::BinaryValue(const std::string& str)
00043 {
00044     *this = BinaryValue(str.c_str());
00045 }
00046 
00047 BinaryValue::BinaryValue(const std::vector<char>& v) : vector<char>(v)
00048 {
00049 };
00050 
00051 BinaryValue::BinaryValue(const char* b, const char* e) : vector<char>(b, e)
00052 {
00053 };
00054 
00055 BinaryValue& BinaryValue::operator+=(const BinaryValue& rhs)
00056 {
00057     insert(end(), rhs.begin(), rhs.end());
00058     return *this;
00059 }
00060 
00061 bool BinaryValue::isUnspecified() const
00062 {
00063     return empty();
00064 }
00065 
00066 // Allow output of vector<char> using normal notation
00067 std::ostream& operator << (std::ostream& os, const BinaryValue& v) {
00068     copy(v.begin(), v.end(), ostream_iterator<char>(os, ""));
00069     return os;        // To allow (cout << a) << b;
00070 }
00071 
00072 bool BinaryValue::operator<(const BinaryValue& rhs)
00073 {
00074     size_type minSize = min(this->size(), rhs.size());
00075     for (size_type i=0; i<minSize; i++) {
00076         if ((*this)[i] < rhs[i]) {
00077             return true;
00078         } else if ((*this)[i] > rhs[i]) {
00079             return false;
00080         }
00081     }
00082 
00083     return (this->size() < rhs.size()) ? true : false;
00084 }
00085 
00086 void BinaryValue::netPack(cCommBuffer *b)
00087 {
00088     doPacking(b,(uint16_t)size());
00089     doPacking(b, (const char*)(&(*begin())), size());
00090 }
00091 
00092 void BinaryValue::netUnpack(cCommBuffer *b)
00093 {
00094     uint16_t size;
00095     doUnpacking(b, size);
00096     resize(size);
00097     doUnpacking(b, (char*)(&(*begin())), size);
00098 }
Generated on Wed May 26 16:21:13 2010 for OverSim by  doxygen 1.6.3