Note: Goby version 1 (shown here) is now considered obsolete. Please use version 2 for new projects, and consider upgrading old projects.

Goby Underwater Autonomy Project  Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
acomms/libdccl/message_var_enum.h
00001 // copyright 2008, 2009 t. schneider tes@mit.edu
00002 // 
00003 // this file is part of the Dynamic Compact Control Language (DCCL),
00004 // the goby-acomms codec. goby-acomms is a collection of libraries 
00005 // for acoustic underwater networking
00006 //
00007 // This program is free software: you can redistribute it and/or modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation, either version 3 of the License, or
00010 // (at your option) any later version.
00011 //
00012 // This software is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this software.  If not, see <http://www.gnu.org/licenses/>.
00019 
00020 #ifndef MESSAGE_VAR_ENUM20100317H
00021 #define MESSAGE_VAR_ENUM20100317H
00022 
00023 #include "message_var.h"
00024 namespace goby
00025 {
00026 
00027     namespace acomms
00028     {   
00029         class DCCLMessageVarEnum : public DCCLMessageVar
00030         {
00031           public:
00032             int calc_size() const
00033             { return ceil(log(enums_.size()+1)/log(2)); }        
00034         
00035             void add_enum(std::string senum) {enums_.push_back(senum);}
00036 
00037             std::vector<std::string>* enums() { return &enums_; }
00038 
00039             DCCLType type()  const { return dccl_enum; }
00040         
00041           private:
00042             void initialize_specific()
00043             { }
00044         
00045             boost::dynamic_bitset<unsigned char> encode_specific(const DCCLMessageVal& v)
00046             {
00047                 std::string s = v;
00048                 // find the iterator within the std::vector of enumerator values for *this* enumerator value
00049                 std::vector<std::string>::iterator pos;
00050                 pos = find(enums_.begin(), enums_.end(), s);
00051             
00052                 // now convert that iterator into a number (think traditional array index)
00053                 unsigned long t = (unsigned long)distance(enums_.begin(), pos);
00054             
00055                 if(pos == enums_.end())
00056                     t = 0;
00057                 else
00058                     ++t;
00059             
00060                 return boost::dynamic_bitset<unsigned char>(calc_size(), t);            
00061             }        
00062 
00063             DCCLMessageVal decode_specific(boost::dynamic_bitset<unsigned char>& b)
00064             {
00065                 unsigned long t = b.to_ulong();
00066                 if(t)
00067                 {
00068                     --t;
00069                     return DCCLMessageVal(enums_.at(t));
00070                 }
00071                 else
00072                     return DCCLMessageVal();            
00073             }
00074 
00075             void get_display_specific(std::stringstream& ss) const
00076             {
00077                 ss << "\t\tvalues:{"; 
00078                 for (std::vector<std::string>::size_type j = 0, m = enums_.size(); j < m; ++j)
00079                 {
00080                     if(j)
00081                         ss << ",";
00082                     ss << enums_[j];
00083                 }
00084             
00085                 ss << "}" << std::endl;
00086             }
00087 
00088           private:
00089             std::vector<std::string> enums_;
00090         };
00091     }
00092 }
00093 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends