1 // Copyright 2008, Google Inc. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 //  1. Redistributions of source code must retain the above copyright notice,
7 //     this list of conditions and the following disclaimer.
8 //  2. Redistributions in binary form must reproduce the above copyright notice,
9 //     this list of conditions and the following disclaimer in the documentation
10 //     and/or other materials provided with the distribution.
11 //  3. Neither the name of Google Inc. nor the names of its contributors may be
12 //     used to endorse or promote products derived from this software without
13 //     specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #ifndef KML_DOM_KML_CAST_H__
27 #define KML_DOM_KML_CAST_H__
28 
29 #include <exception>  // Needed by boost::instrusive_ptr.
30 
31 #include "kml/base/xml_element.h"
32 #include "kml/dom/kmldom.h"
33 #include "kml/dom/kml_ptr.h"
34 
35 namespace kmldom {
36 
37 // This function template operates akin to dynamic_cast.  If the given
38 // Element-derived type is of the template type then a pointer is returned,
39 // else NULL.  It is safe to pass a NULL to this function.
40 template<class T>
ElementCast(const ElementPtr & element)41 inline const boost::intrusive_ptr<T> ElementCast(
42     const ElementPtr& element) {
43   if (element && element->IsA(T::ElementType())) {
44     return boost::static_pointer_cast<T>(element);
45   }
46   return NULL;
47 }
48 
AsElement(const kmlbase::XmlElementPtr & xml_element)49 inline const ElementPtr AsElement(const kmlbase::XmlElementPtr& xml_element) {
50   return boost::static_pointer_cast<Element>(xml_element);
51 }
52 
53 // Abstract element groups.
54 const AbstractLatLonBoxPtr AsAbstractLatLonBox(const ElementPtr element);
55 const AbstractViewPtr AsAbstractView(const ElementPtr element);
56 const ColorStylePtr AsColorStyle(const ElementPtr element);
57 const ContainerPtr AsContainer(const ElementPtr element);
58 const FeaturePtr AsFeature(const ElementPtr element);
59 const GeometryPtr AsGeometry(const ElementPtr element);
60 const ObjectPtr AsObject(const ElementPtr element);
61 const OverlayPtr AsOverlay(const ElementPtr element);
62 const StyleSelectorPtr AsStyleSelector(const ElementPtr element);
63 const SubStylePtr AsSubStyle(const ElementPtr element);
64 const TimePrimitivePtr AsTimePrimitive(const ElementPtr element);
65 
66 // Concrete elements.
67 const AliasPtr AsAlias(const ElementPtr element);
68 const BalloonStylePtr AsBalloonStyle(const ElementPtr element);
69 const CameraPtr AsCamera(const ElementPtr element);
70 const ChangePtr AsChange(const ElementPtr element);
AsCoordinates(const ElementPtr & element)71 inline const CoordinatesPtr AsCoordinates(const ElementPtr& element) {
72   return ElementCast<Coordinates>(element);
73 }
74 const CreatePtr AsCreate(const ElementPtr element);
75 const DataPtr AsData(const ElementPtr element);
76 const DeletePtr AsDelete(const ElementPtr element);
77 const DocumentPtr AsDocument(const ElementPtr element);
AsExtendedData(const ElementPtr & element)78 inline const ExtendedDataPtr AsExtendedData(const ElementPtr& element) {
79   return ElementCast<ExtendedData>(element);
80 }
81 const FolderPtr AsFolder(const ElementPtr element);
82 const GroundOverlayPtr AsGroundOverlay(const ElementPtr element);
83 const HotSpotPtr AsHotSpot(const ElementPtr element);
84 const IconPtr AsIcon(const ElementPtr element);
85 const IconStylePtr AsIconStyle(const ElementPtr element);
86 const IconStyleIconPtr AsIconStyleIcon(const ElementPtr element);
87 const ImagePyramidPtr AsImagePyramid(const ElementPtr element);
88 const InnerBoundaryIsPtr AsInnerBoundaryIs(const ElementPtr element);
89 const ItemIconPtr AsItemIcon(const ElementPtr element);
AsKml(const ElementPtr & element)90 inline const KmlPtr AsKml(const ElementPtr& element) {
91   return ElementCast<Kml>(element);
92 }
93 const LabelStylePtr AsLabelStyle(const ElementPtr element);
94 const LatLonAltBoxPtr AsLatLonAltBox(const ElementPtr element);
95 const LatLonBoxPtr AsLatLonBox(const ElementPtr element);
96 const LineStringPtr AsLineString(const ElementPtr element);
97 const LineStylePtr AsLineStyle(const ElementPtr element);
98 const LinearRingPtr AsLinearRing(const ElementPtr element);
99 const LinkPtr AsLink(const ElementPtr element);
100 const LinkSnippetPtr AsLinkSnippet(const ElementPtr element);
101 const ListStylePtr AsListStyle(const ElementPtr element);
102 const LocationPtr AsLocation(const ElementPtr element);
103 const LodPtr AsLod(const ElementPtr element);
104 const LookAtPtr AsLookAt(const ElementPtr element);
AsMetadata(const ElementPtr & element)105 inline const MetadataPtr AsMetadata(const ElementPtr& element) {
106   return ElementCast<Metadata>(element);
107 }
108 const ModelPtr AsModel(const ElementPtr element);
109 const MultiGeometryPtr AsMultiGeometry(const ElementPtr element);
110 const NetworkLinkPtr AsNetworkLink(const ElementPtr element);
AsNetworkLinkControl(const ElementPtr & element)111 inline const NetworkLinkControlPtr AsNetworkLinkControl(
112     const ElementPtr& element) {
113   return ElementCast<NetworkLinkControl>(element);
114 }
115 const OrientationPtr AsOrientation(const ElementPtr element);
116 const OuterBoundaryIsPtr AsOuterBoundaryIs(const ElementPtr element);
117 const OverlayXYPtr AsOverlayXY(const ElementPtr element);
118 const PairPtr AsPair(const ElementPtr element);
119 const PhotoOverlayPtr AsPhotoOverlay(const ElementPtr element);
120 const PlacemarkPtr AsPlacemark(const ElementPtr element);
121 const PointPtr AsPoint(const ElementPtr element);
122 const PolyStylePtr AsPolyStyle(const ElementPtr element);
123 const PolygonPtr AsPolygon(const ElementPtr element);
124 const RegionPtr AsRegion(const ElementPtr element);
125 const ResourceMapPtr AsResourceMap(const ElementPtr element);
126 const RotationXYPtr AsRotationXY(const ElementPtr element);
127 const ScalePtr AsScale(const ElementPtr element);
128 const SchemaPtr AsSchema(const ElementPtr element);
129 const SchemaDataPtr AsSchemaData(const ElementPtr element);
130 const ScreenOverlayPtr AsScreenOverlay(const ElementPtr element);
131 const ScreenXYPtr AsScreenXY(const ElementPtr element);
AsSimpleData(const ElementPtr & element)132 inline const SimpleDataPtr AsSimpleData(const ElementPtr& element) {
133   return ElementCast<SimpleData>(element);
134 }
AsSimpleField(const ElementPtr & element)135 inline const SimpleFieldPtr AsSimpleField(const ElementPtr& element) {
136   return ElementCast<SimpleField>(element);
137 }
138 const SizePtr AsSize(const ElementPtr element);
139 const SnippetPtr AsSnippet(const ElementPtr element);
140 const StylePtr AsStyle(const ElementPtr element);
141 const StyleMapPtr AsStyleMap(const ElementPtr element);
142 const TimeSpanPtr AsTimeSpan(const ElementPtr element);
143 const TimeStampPtr AsTimeStamp(const ElementPtr element);
AsUpdate(const ElementPtr & element)144 inline const UpdatePtr AsUpdate(const ElementPtr& element) {
145   return ElementCast<Update>(element);
146 }
147 const ViewVolumePtr AsViewVolume(const ElementPtr element);
148 
149 // Atom
AsAtomAuthor(const ElementPtr & element)150 inline const AtomAuthorPtr AsAtomAuthor(const ElementPtr& element) {
151   return ElementCast<AtomAuthor>(element);
152 }
AsAtomCategory(const ElementPtr & element)153 inline const AtomCategoryPtr AsAtomCategory(const ElementPtr& element) {
154   return ElementCast<AtomCategory>(element);
155 }
AsAtomContent(const ElementPtr & element)156 inline const AtomContentPtr AsAtomContent(const ElementPtr& element) {
157   return ElementCast<AtomContent>(element);
158 }
AsAtomEntry(const ElementPtr & element)159 inline const AtomEntryPtr AsAtomEntry(const ElementPtr& element) {
160   return ElementCast<AtomEntry>(element);
161 }
AsAtomFeed(const ElementPtr & element)162 inline const AtomFeedPtr AsAtomFeed(const ElementPtr& element) {
163   return ElementCast<AtomFeed>(element);
164 }
AsAtomLink(const ElementPtr & element)165 inline const AtomLinkPtr AsAtomLink(const ElementPtr& element) {
166   return ElementCast<AtomLink>(element);
167 }
168 
169 // xAL
AsXalAddressDetails(const ElementPtr & element)170 inline const XalAddressDetailsPtr AsXalAddressDetails(
171     const ElementPtr& element) {
172   return ElementCast<XalAddressDetails>(element);
173 }
AsXalAdministrativeArea(const ElementPtr & element)174 inline const XalAdministrativeAreaPtr AsXalAdministrativeArea(
175     const ElementPtr& element) {
176   return ElementCast<XalAdministrativeArea>(element);
177 }
178 
AsXalCountry(const ElementPtr & element)179 inline const XalCountryPtr AsXalCountry(const ElementPtr& element) {
180   return ElementCast<XalCountry>(element);
181 }
182 
AsXalLocality(const ElementPtr & element)183 inline const XalLocalityPtr AsXalLocality(const ElementPtr& element) {
184   return ElementCast<XalLocality>(element);
185 }
186 
AsXalPostalCode(const ElementPtr & element)187 inline const XalPostalCodePtr AsXalPostalCode(const ElementPtr& element) {
188   return ElementCast<XalPostalCode>(element);
189 }
190 
AsXalSubAdministrativeArea(const ElementPtr & element)191 inline const XalSubAdministrativeAreaPtr AsXalSubAdministrativeArea(
192     const ElementPtr& element) {
193   return ElementCast<XalSubAdministrativeArea>(element);
194 }
195 
AsXalThoroughfare(const ElementPtr & element)196 inline const XalThoroughfarePtr AsXalThoroughfare(const ElementPtr& element) {
197   return ElementCast<XalThoroughfare>(element);
198 }
199 
200 // gx
201 
AsGxAnimatedUpdate(const ElementPtr element)202 inline const GxAnimatedUpdatePtr AsGxAnimatedUpdate(const ElementPtr element) {
203   return ElementCast<GxAnimatedUpdate>(element);
204 }
205 
AsGxFlyTo(const ElementPtr element)206 inline const GxFlyToPtr AsGxFlyTo(const ElementPtr element) {
207   return ElementCast<GxFlyTo>(element);
208 }
209 
AsGxLatLonQuad(const ElementPtr element)210 inline const GxLatLonQuadPtr AsGxLatLonQuad(const ElementPtr element) {
211   return ElementCast<GxLatLonQuad>(element);
212 }
213 
AsGxMultiTrack(const ElementPtr element)214 inline const GxMultiTrackPtr AsGxMultiTrack(const ElementPtr element) {
215   return ElementCast<GxMultiTrack>(element);
216 }
217 
AsGxPlaylist(const ElementPtr element)218 inline const GxPlaylistPtr AsGxPlaylist(const ElementPtr element) {
219   return ElementCast<GxPlaylist>(element);
220 }
221 
AsGxSimpleArrayField(const ElementPtr element)222 inline const GxSimpleArrayFieldPtr AsGxSimpleArrayField(
223     const ElementPtr element) {
224   return ElementCast<GxSimpleArrayField>(element);
225 }
226 
AsGxSimpleArrayData(const ElementPtr element)227 inline const GxSimpleArrayDataPtr AsGxSimpleArrayData(
228     const ElementPtr element) {
229   return ElementCast<GxSimpleArrayData>(element);
230 }
231 
AsGxSoundCue(const ElementPtr element)232 inline const GxSoundCuePtr AsGxSoundCue(const ElementPtr element) {
233   return ElementCast<GxSoundCue>(element);
234 }
235 
AsGxTimeSpan(const ElementPtr element)236 inline const GxTimeSpanPtr AsGxTimeSpan(const ElementPtr element) {
237   return ElementCast<GxTimeSpan>(element);
238 }
239 
AsGxTimeStamp(const ElementPtr element)240 inline const GxTimeStampPtr AsGxTimeStamp(const ElementPtr element) {
241   return ElementCast<GxTimeStamp>(element);
242 }
243 
AsGxTour(const ElementPtr element)244 inline const GxTourPtr AsGxTour(const ElementPtr element) {
245   return ElementCast<GxTour>(element);
246 }
247 
AsGxTourControl(const ElementPtr element)248 inline const GxTourControlPtr AsGxTourControl(const ElementPtr element) {
249   return ElementCast<GxTourControl>(element);
250 }
251 
AsGxTourPrimitive(const ElementPtr element)252 inline const GxTourPrimitivePtr AsGxTourPrimitive(const ElementPtr element) {
253   return ElementCast<GxTourPrimitive>(element);
254 }
255 
AsGxTrack(const ElementPtr element)256 inline const GxTrackPtr AsGxTrack(const ElementPtr element) {
257   return ElementCast<GxTrack>(element);
258 }
259 
AsGxWait(const ElementPtr element)260 inline const GxWaitPtr AsGxWait(const ElementPtr element) {
261   return ElementCast<GxWait>(element);
262 }
263 
264 }  // end namespace kmldom
265 
266 #endif  // KML_DOM_KML_CAST_H__
267