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) 2005 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 base_type_factory_h
27 #define base_type_factory_h
28 
29 #include <string>
30 
31 #include "Type.h"
32 #include "InternalErr.h"
33 
34 // Class declarations; Make sure to include the corresponding headers in the
35 // implementation file.
36 
37 namespace libdap
38 {
39 
40 class Byte;
41 class Int16;
42 class UInt16;
43 class Int32;
44 class UInt32;
45 class Float32;
46 class Float64;
47 class Str;
48 class Url;
49 class Array;
50 class Structure;
51 class Sequence;
52 class Grid;
53 class BaseType;
54 
55 /** A factory to create instances of the leaf nodes of BaseType (Byte, ...
56     Grid). Clients of libdap++ which require special behavior for the types
57     should subclass this factory and provide an implementation which creates
58     instances of those specializations. Make sure to pass a reference to the
59     new factory to DDS's constructor since by default it uses this factory.
60 
61     To define and use your own factory, first make sure that you are not
62     using the compile time constant 'DEFAULT_BASETYPE_FACTORY.' Then pass a
63     pointer to an instance of your factory to the DDS/DataDDS constructors.
64     When the parser is used to build a DDS from a DAP response, the factory
65     will be used to instantiate the different variable-type classes.
66 
67     @note The easiest way to subclass this is to follow the pattern of using
68     a separate class declaration and implementation. It's possible to use one
69     file to hold
70     both, but that is complicated somewhat because DDS.h, which includes this
71     class, also includes many of the type classes (Array.h, ..., Grid.h) and
72     the order of their inclusion can create compilation problems where the
73     Vector and/or Constructor base classes are not defined. It's easiest to
74     split the declaration and implementation and include forward declarations
75     of the type classes in the declaration (\c .h) file and then include the
76     type class' headers in the implementation (\c .cc) file.
77 
78     @author James Gallagher
79     @see DDS */
80 class BaseTypeFactory
81 {
82 public:
BaseTypeFactory()83     BaseTypeFactory()
84     {}
~BaseTypeFactory()85     virtual ~BaseTypeFactory()
86     {}
87 
88     /**
89      * Build a new variable and return it using a BaseType pointer. The
90      * type of the variable is given using  Type enumeration.
91      *
92      * @note Added for DAP4
93      *
94      * @param t The type of the variable to create
95      * @parma name The (optional) name of the variable.
96      */
97     virtual BaseType *NewVariable(Type t, const string &name = "") const;
98 
99     /**
100      * Clone this object and return a pointer to the clone.
101      *
102      * @note added for DAP4
103      */
ptr_duplicate()104     virtual BaseTypeFactory *ptr_duplicate() const {
105         throw InternalErr(__FILE__, __LINE__, "Not Implemented.");
106     }
107 
108     virtual Byte *NewByte(const string &n = "") const;
109     virtual Int16 *NewInt16(const string &n = "") const;
110     virtual UInt16 *NewUInt16(const string &n = "") const;
111     virtual Int32 *NewInt32(const string &n = "") const;
112     virtual UInt32 *NewUInt32(const string &n = "") const;
113     virtual Float32 *NewFloat32(const string &n = "") const;
114     virtual Float64 *NewFloat64(const string &n = "") const;
115 
116     virtual Str *NewStr(const string &n = "") const;
117     virtual Url *NewUrl(const string &n = "") const;
118 
119     virtual Array *NewArray(const string &n = "", BaseType *v = 0) const;
120     virtual Structure *NewStructure(const string &n = "") const;
121     virtual Sequence *NewSequence(const string &n = "") const;
122     virtual Grid *NewGrid(const string &n = "") const;
123 };
124 
125 } // namespace libdap
126 
127 #endif // base_type_factory_h
128