1 
2 // -*- mode: c++; c-basic-offset:4 -*-
3 
4 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
5 // Access Protocol.
6 
7 // Copyright (c) 2014 OPeNDAP, Inc.
8 // Author: James Gallagher <jgallagher@opendap.org>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 //
24 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25 
26 #ifndef _D4RValue_h
27 #define _D4RValue_h
28 
29 #include <vector>
30 #include <string>
31 
32 #include <dods-datatypes.h>
33 #include <D4Function.h>
34 
35 namespace libdap
36 {
37 
38 class BaseType;
39 class D4RValue;
40 
41 // Factory class to build RValue objects. User by the parser/ce-evaluator
42 D4RValue *D4RValueFactory(std::string cpps);
43 
44 class D4RValueList
45 {
46 private:
47 	std::vector<D4RValue *> d_rvalues;
48 
49 	void m_duplicate(const D4RValueList &src);
50 
51 public:
52 	typedef std::vector<D4RValue *>::iterator iter;
53 
D4RValueList()54 	D4RValueList() { }
D4RValueList(const D4RValueList & src)55 	D4RValueList(const D4RValueList &src) { m_duplicate(src); }
D4RValueList(D4RValue * rv)56 	D4RValueList(D4RValue *rv) { add_rvalue(rv); }
57 
58 	virtual ~D4RValueList();
59 
60     D4RValueList &operator=(const D4RValueList &rhs) {
61         if (this == &rhs) return *this;
62         m_duplicate(rhs);
63         return *this;
64     }
65 
add_rvalue(D4RValue * rv)66     void add_rvalue(D4RValue *rv) {
67 		d_rvalues.push_back(rv);
68 	}
69 
get_rvalue(unsigned int i)70 	D4RValue *get_rvalue(unsigned int i) {
71 		return d_rvalues.at(i);
72 	}
73 
begin()74 	iter begin() { return d_rvalues.begin(); }
end()75 	iter end() { return d_rvalues.end(); }
76 
size()77 	unsigned int size() const { return d_rvalues.size(); }
78 
79 };
80 
81 /**
82  * Holds the RValues for the D4 function parser and for the filter
83  * expression evaluator.
84  */
85 class D4RValue
86 {
87 public:
88     enum value_kind {
89         unknown,
90         basetype,
91         function,
92         constant
93     };
94 
95 private:
96     BaseType *d_variable;	// This is a weak pointer; do not delete
97 
98     D4Function d_func;  	// (weak) pointer to a function returning BaseType *
99     D4RValueList *d_args;  	// pointer to arguments to the function; delete
100 
101     BaseType *d_constant;	// pointer; delete.
102 
103     value_kind d_value_kind;
104 
105     /** @brief Clone 'src' to 'this'. */
106     void m_duplicate(const D4RValue &src);
107 
108     friend class D4RValueList;
109 
110 public:
D4RValue()111     D4RValue() : d_variable(0), d_func(0), d_args(0), d_constant(0), d_value_kind(unknown) { }
D4RValue(const D4RValue & src)112     D4RValue(const D4RValue &src) { m_duplicate(src); }
D4RValue(BaseType * btp)113     D4RValue(BaseType *btp)  : d_variable(btp), d_func(0), d_args(0), d_constant(0), d_value_kind(basetype) { }
D4RValue(D4Function f,D4RValueList * args)114     D4RValue(D4Function f, D4RValueList *args)  : d_variable(0), d_func(f), d_args(args), d_constant(0), d_value_kind(function) { }
115 
116     D4RValue(unsigned long long ui);
117     D4RValue(long long i);
118     D4RValue(double r);
119     D4RValue(std::string s);
120     D4RValue(std::vector<dods_byte> &byte_args);
121     D4RValue(std::vector<dods_int8> &byte_int8);
122     D4RValue(std::vector<dods_uint16> &byte_uint16);
123     D4RValue(std::vector<dods_int16> &byte_int16);
124     D4RValue(std::vector<dods_uint32> &byte_uint32);
125     D4RValue(std::vector<dods_int32> &byte_int32);
126     D4RValue(std::vector<dods_uint64> &byte_uint64);
127     D4RValue(std::vector<dods_int64> &byte_int64);
128     D4RValue(std::vector<dods_float32> &byte_float32);
129     D4RValue(std::vector<dods_float64> &byte_float64);
130 
131     virtual ~D4RValue();
132 
133     D4RValue &operator=(D4RValue &rhs) {
134         if (this == &rhs)
135             return *this;
136 
137         m_duplicate(rhs);
138 
139         return *this;
140     }
141 
142     /**
143      * @brief What kind of thing holds the value
144      * Values in DAP4 constraints are either constants, dataset variables
145      * or function results. It might be nice to know the source of a
146      * given value in order to optimize the evaluation of certain kinds of
147      * expressions.
148      * @return The 'value_kind' of this value.
149      */
get_kind()150     value_kind get_kind() const { return d_value_kind; }
151 
152     // This is the call that will be used to return the value of a function.
153     // jhrg 3/10/14
154     virtual BaseType *value(DMR &dmr);
155     // And this optimizes value() for filters, where functions are not supported.
156     virtual BaseType *value();
157 
158 };
159 
160 } // namespace libdap
161 #endif // _RValue_h
162