libdballe 5.10
record.h
Go to the documentation of this file.
00001 /*
00002  * dballe/record - groups of related variables
00003  *
00004  * Copyright (C) 2005--2010  ARPA-SIM <urpsim@smr.arpa.emr.it>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00018  *
00019  * Author: Enrico Zini <enrico@enricozini.com>
00020  */
00021 
00022 #ifndef DBA_RECORD_H
00023 #define DBA_RECORD_H
00024 
00030 #include <dballe/core/var.h>
00031 #include <dballe/core/matcher.h>
00032 #include <vector>
00033 
00034 namespace dballe {
00035 
00039 enum _dba_keyword {
00040     DBA_KEY_ERROR       = -1,
00041     DBA_KEY_PRIORITY    =  0,
00042     DBA_KEY_PRIOMAX     =  1,
00043     DBA_KEY_PRIOMIN     =  2,
00044     DBA_KEY_REP_COD     =  3,
00045     DBA_KEY_REP_MEMO    =  4,
00046     DBA_KEY_ANA_ID      =  5,
00047     DBA_KEY_MOBILE      =  6,
00048     DBA_KEY_IDENT       =  7,
00049     DBA_KEY_LAT         =  8,
00050     DBA_KEY_LON         =  9,
00051     DBA_KEY_LATMAX      = 10,
00052     DBA_KEY_LATMIN      = 11,
00053     DBA_KEY_LONMAX      = 12,
00054     DBA_KEY_LONMIN      = 13,
00055     DBA_KEY_YEAR        = 14,
00056     DBA_KEY_MONTH       = 15,
00057     DBA_KEY_DAY         = 16,
00058     DBA_KEY_HOUR        = 17,
00059     DBA_KEY_MIN         = 18,
00060     DBA_KEY_SEC         = 19,
00061     DBA_KEY_YEARMAX     = 20,
00062     DBA_KEY_YEARMIN     = 21,
00063     DBA_KEY_MONTHMAX    = 22,
00064     DBA_KEY_MONTHMIN    = 23,
00065     DBA_KEY_DAYMAX      = 24,
00066     DBA_KEY_DAYMIN      = 25,
00067     DBA_KEY_HOURMAX     = 26,
00068     DBA_KEY_HOURMIN     = 27,
00069     DBA_KEY_MINUMAX     = 28,
00070     DBA_KEY_MINUMIN     = 29,
00071     DBA_KEY_SECMAX      = 30,
00072     DBA_KEY_SECMIN      = 31,
00073     DBA_KEY_LEVELTYPE1  = 32,
00074     DBA_KEY_L1          = 33,
00075     DBA_KEY_LEVELTYPE2  = 34,
00076     DBA_KEY_L2          = 35,
00077     DBA_KEY_PINDICATOR  = 36,
00078     DBA_KEY_P1          = 37,
00079     DBA_KEY_P2          = 38,
00080     DBA_KEY_VAR         = 39,
00081     DBA_KEY_VARLIST     = 40,
00082     DBA_KEY_CONTEXT_ID  = 41,
00083     DBA_KEY_QUERY       = 42,
00084     DBA_KEY_ANA_FILTER  = 43,
00085     DBA_KEY_DATA_FILTER = 44,
00086     DBA_KEY_ATTR_FILTER = 45,
00087     DBA_KEY_LIMIT       = 46,
00088     DBA_KEY_VAR_RELATED = 47,
00089     DBA_KEY_COUNT       = 48,
00090 };
00092 typedef enum _dba_keyword dba_keyword;
00093 
00100 class Record
00101 {
00102 protected:
00103     /* The storage for the core keyword data */
00104     wreport::Var* keydata[DBA_KEY_COUNT];
00105 
00106     /* The variables */
00107     std::vector<wreport::Var*> m_vars;
00108 
00110     int find_item(wreport::Varcode code) const throw ();
00111 
00113     wreport::Var& get_item(wreport::Varcode code);
00114 
00116     const wreport::Var& get_item(wreport::Varcode code) const;
00117 
00119     void remove_item(wreport::Varcode code);
00120 
00121 public:
00122     Record();
00123     Record(const Record& rec);
00124     ~Record();
00125 
00126     Record& operator=(const Record& rec);
00127 
00128     bool operator==(const Record& rec) const;
00129     bool operator!=(const Record& rec) const { return !operator==(rec); }
00130 
00132     void clear();
00133 
00135     void clear_vars();
00136 
00146     void add(const Record& source);
00147 
00152     bool contains(const Record& subset) const;
00153 
00165     void set_to_difference(const Record& source1, const Record& source2);
00166 
00174     const wreport::Var* key_peek(dba_keyword parameter) const throw ();
00175 
00183     const wreport::Var* var_peek(wreport::Varcode code) const throw ();
00184 
00191     const wreport::Var* peek(const char* name) const;
00192 
00194     const wreport::Var* peek(dba_keyword parameter) const throw () { return key_peek(parameter); }
00195 
00197     const wreport::Var* peek(wreport::Varcode code) const throw () { return var_peek(code); }
00198 
00207     const char* key_peek_value(dba_keyword parameter) const throw ();
00208 
00217     const char* var_peek_value(wreport::Varcode code) const throw ();
00218 
00225     const char* peek_value(const char* name) const;
00226 
00228     const char* peek_value(dba_keyword parameter) const throw () { return key_peek_value(parameter); }
00229 
00231     const char* peek_value(wreport::Varcode code) const throw () { return var_peek_value(code); }
00232 
00236     const wreport::Var& key(dba_keyword parameter) const;
00237 
00241     const wreport::Var& var(wreport::Varcode code) const;
00242 
00246     wreport::Var& key(dba_keyword parameter);
00247 
00251     wreport::Var& var(wreport::Varcode code);
00252 
00254     // @{
00255     const wreport::Var& get(dba_keyword parameter) const { return key(parameter); }
00256     const wreport::Var& get(wreport::Varcode code) const { return var(code); }
00257     const wreport::Var& get(const char* name) const;
00258     wreport::Var& get(dba_keyword parameter) { return key(parameter); }
00259     wreport::Var& get(wreport::Varcode code) { return var(code); }
00260     wreport::Var& get(const char* name);
00261     template<typename K, typename T>
00262     T get(K name, T default_value) const
00263     {
00264         if (const wreport::Var* v = peek(name))
00265             return v->enq(default_value);
00266         else
00267             return default_value;
00268     }
00269     const wreport::Var& operator[](dba_keyword parameter) const { return key(parameter); }
00270     const wreport::Var& operator[](wreport::Varcode code) const { return var(code); }
00271     const wreport::Var& operator[](const char* name) const { return get(name); }
00272     wreport::Var& operator[](dba_keyword parameter) { return key(parameter); }
00273     wreport::Var& operator[](wreport::Varcode code) { return var(code); }
00274     wreport::Var& operator[](const char* name) { return get(name); }
00275     template<typename P, typename V>
00276     void set(const P& field, const V& val) { get(field).set(val); }
00277     void set(const wreport::Var& var) { get(var.code()).set(var); }
00278     void unset(dba_keyword parameter) { key_unset(parameter); }
00279     void unset(wreport::Varcode code) { var_unset(code); }
00280     void unset(const char* name);
00281     // @}
00282 
00286     void set_ana_context();
00287 
00291     const std::vector<wreport::Var*>& vars() const;
00292 
00299     void key_unset(dba_keyword parameter);
00300 
00307     void var_unset(wreport::Varcode code);
00308 
00324     void parse_date_extremes(int* minvalues, int* maxvalues) const;
00325 
00336     void parse_date(int* values) const;
00337 
00351     void set_from_string(const char* str);
00352 
00359     void print(FILE* out) const;
00360 
00367     static const char* keyword_name(dba_keyword keyword);
00368 
00375     static wreport::Varinfo keyword_info(dba_keyword keyword);
00376 
00384     static dba_keyword keyword_byname(const char* tag);
00385 
00397     static dba_keyword keyword_byname_len(const char* tag, int len);
00398 };
00399 
00400 #if 0
00401 
00414 void dba_record_diff(dba_record rec1, dba_record rec2, int* diffs, FILE* out);
00415 #endif
00416 
00417 struct MatchedRecord : public Matched
00418 {
00419     const Record& r;
00420 
00421     MatchedRecord(const Record& r);
00422     ~MatchedRecord();
00423 
00424     virtual matcher::Result match_var_id(int val) const;
00425     virtual matcher::Result match_station_id(int val) const;
00426     virtual matcher::Result match_station_wmo(int block, int station=-1) const;
00427     virtual matcher::Result match_date(const int* min, const int* max) const;
00428     virtual matcher::Result match_coords(int latmin, int latmax, int lonmin, int lonmax) const;
00429     virtual matcher::Result match_rep_memo(const char* memo) const;
00430 };
00431 
00432 }
00433 
00434 /* vim:set ts=4 sw=4: */
00435 #endif