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.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_VAR20091211H
00021 #define MESSAGE_VAR20091211H
00022 
00023 #include <bitset>
00024 #include <stdexcept>
00025 #include <vector>
00026 #include <string>
00027 #include <map>
00028 #include <ostream>
00029 #include <cmath>
00030 #include <limits>
00031 
00032 #include <boost/dynamic_bitset.hpp>
00033 #include <boost/lexical_cast.hpp>
00034 
00035 #include "dccl_constants.h"
00036 #include "dccl_exception.h"
00037 #include "message_val.h"
00038 namespace goby
00039 {
00040     namespace acomms
00041     {
00042         class DCCLMessageVal;
00043         class DCCLMessage;
00044         class DCCLAlgorithmPerformer;
00045     
00046 // defines a piece of a DCCLMessage (an <int>, a <bool>, etc)
00047         class DCCLMessageVar
00048         {
00049           public:
00050             DCCLMessageVar();
00051         
00052             // set
00053             void set_name(std::string name) {name_ = name;}
00054             void set_source_var(std::string source_var) {source_var_ = source_var; source_set_=true;}
00055             void set_source_key(std::string source_key) {source_key_ = source_key;}
00056             void set_source_set(bool source_set) {source_set_ = source_set;}
00057             void set_algorithms(const std::vector<std::string>& algorithm) {algorithms_ = algorithm;}
00058 
00059 
00060 
00061             // should be overloaded by derived types if supported
00062             virtual void set_max(const std::string& s)
00063             { bad_overload("set_max()"); }
00064             virtual void set_min(const std::string& s)
00065             { bad_overload("set_min()"); }
00066             virtual void set_precision(const std::string& s)
00067             { bad_overload("set_precision()"); }
00068             virtual void set_max_length(const std::string& s)
00069             { bad_overload("set_max_length()"); }
00070             virtual void set_num_bytes(const std::string& s)
00071             { bad_overload("set_num_bytes()"); }
00072             virtual void set_static_val(const std::string& static_val)
00073             { bad_overload("set_static_val()"); }
00074             virtual void add_enum(std::string senum)
00075             { bad_overload("add_enum()"); }
00076             virtual void set_max_delta(const std::string& s)
00077             { bad_overload("set_max_delta()"); }
00078 
00079             void set_array_length(unsigned u)
00080             { array_length_ = u; }
00081             void set_array_length(const std::string& s)
00082             { set_array_length(boost::lexical_cast<unsigned>(s)); }
00083 
00084         
00085             virtual double max() const
00086             { bad_overload("max()"); return 0; }
00087             virtual double min() const
00088             { bad_overload("min()"); return 0; }
00089             virtual int precision() const
00090             { bad_overload("precision()"); return 0;}
00091             virtual unsigned max_length() const
00092             { bad_overload("max_length()"); return 0; }
00093             virtual unsigned num_bytes() const
00094             { bad_overload("num_bytes()"); return 0; }
00095             virtual std::string static_val() const
00096             { bad_overload("static_val()"); return ""; }
00097             virtual std::vector<std::string>* enums()
00098             { bad_overload("enums()"); return 0; }  
00099         
00100             unsigned array_length() const { return array_length_; }
00101         
00102         
00103             // get
00104             virtual DCCLType type() const = 0;
00105 
00106             std::string name() const {return name_;}
00107             std::string source_var() const { return source_var_; }
00108 
00109             // other
00110             void initialize(const DCCLMessage& msg);
00111             std::string get_display() const;
00112 
00113             void read_pubsub_vars(std::map<std::string,std::vector<DCCLMessageVal> >& vals,
00114                                   const std::map<std::string,std::vector<DCCLMessageVal> >& in);
00115         
00116             std::string parse_string_val(const std::string& sval);
00117         
00118             void var_encode(std::map<std::string,std::vector<DCCLMessageVal> >& vals,
00119                             boost::dynamic_bitset<unsigned char>& bits);
00120             void var_decode(std::map<std::string,std::vector<DCCLMessageVal> >& vals,
00121                             boost::dynamic_bitset<unsigned char>& bits);
00122 
00123             void set_defaults(std::map<std::string,std::vector<DCCLMessageVal> >& vals, unsigned modem_id, unsigned id);  
00124         
00125             virtual int calc_size() const = 0;
00126             virtual int calc_total_size() const
00127             { return calc_size() * array_length_; }        
00128         
00129 
00130         
00131           protected:
00132             virtual void initialize_specific() = 0;
00133 
00134             virtual void pre_encode(DCCLMessageVal& val) { }
00135              
00136             virtual boost::dynamic_bitset<unsigned char> encode_specific(const DCCLMessageVal& v) = 0;
00137             virtual DCCLMessageVal decode_specific(boost::dynamic_bitset<unsigned char>& bits) = 0;
00138             virtual void get_display_specific(std::stringstream& ss) const = 0;
00139         
00140             virtual void set_defaults_specific(DCCLMessageVal& v, unsigned modem_id, unsigned id) { bad_overload("set_defaults_specific()"); }
00141 
00142         
00143           private:
00144             void bad_overload(const std::string& s) const
00145             {
00146                 throw(DCCLException(std::string(s + " not supported by this DCCLMessageVar: " + name() + " (" + type_to_string(type()) + ")")));
00147             }        
00148 
00149             // helper to avoid copy-paste code
00150             void encode_value(const DCCLMessageVal& val, boost::dynamic_bitset<unsigned char>& bits);
00151         
00152         
00153           protected:        
00154             unsigned array_length_;
00155             DCCLMessageVal key_val_;
00156             bool is_key_frame_;
00157             std::string source_var_;
00158             std::string name_;
00159         
00160           private:
00161             bool source_set_;
00162             DCCLAlgorithmPerformer * ap_;
00163             std::string source_key_;
00164             std::vector<std::string> algorithms_;
00165         };
00166 
00167 
00168         std::ostream& operator<< (std::ostream& out, const DCCLMessageVar& m);
00169     }
00170 }
00171 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends