• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

soa_types.h

Go to the documentation of this file.
00001 /* Copyright (c) 2008-2009, AbiSource Corporation B.V.
00002  * All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *     * Redistributions of source code must retain the above copyright
00007  *       notice, this list of conditions and the following disclaimer.
00008  *     * Redistributions in binary form must reproduce the above copyright
00009  *       notice, this list of conditions and the following disclaimer in the
00010  *       documentation and/or other materials provided with the distribution.
00011  *     * Neither the name of AbiSource Corporation B.V. nor the
00012  *       names of other contributors may be used to endorse or promote products
00013  *       derived from this software without specific prior written permission.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY ABISOURCE CORPORATION B.V. AND OTHER
00016  * CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
00017  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00018  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ABISOURCE
00019  * CORPORATION B.V OR OTHER CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00020  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00021  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00022  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00023  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00024  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00025  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00028 
00029 #ifndef __SOA_TYPES__
00030 #define __SOA_TYPES__
00031 
00032 #include <vector>
00033 #ifdef _MSC_VER
00034 #include "msc_stdint.h"
00035 #else
00036 #include <stdint.h>
00037 #endif
00038 #include <string>
00039 #include <boost/shared_ptr.hpp>
00040 #include <boost/enable_shared_from_this.hpp>
00041 
00042 namespace soa {
00043 
00044 enum Type {
00045     ARRAY_TYPE = 0,
00046     COLLECTION_TYPE,
00047     STRING_TYPE,
00048     INT_TYPE,
00049     BOOL_TYPE,
00050     BASE64BIN_TYPE,
00051     QNAME_TYPE
00052 };
00053 
00054 class Generic : public boost::enable_shared_from_this<Generic> {
00055 public:
00056     Generic(const std::string& n, Type t)
00057         : name_(n),
00058         type_(t)
00059     {}
00060 
00061     virtual ~Generic() {
00062     }
00063 
00064     virtual bool complex() {
00065         return false;
00066     }
00067 
00068     const std::string& name() const {
00069         return name_;
00070     }
00071 
00072     Type type() const {
00073         return type_;
00074     }
00075 
00076     template <class T>
00077     boost::shared_ptr<T> as() {
00078         return boost::dynamic_pointer_cast<T>(shared_from_this());
00079     }
00080 
00081     template <class T>
00082     boost::shared_ptr<T> as(const std::string& n) {
00083         if (name_ != n)
00084             return boost::shared_ptr<T>();
00085         return boost::dynamic_pointer_cast<T>(shared_from_this());
00086     }
00087 
00088 private:
00089     std::string name_;
00090     Type type_;
00091 };
00092 typedef boost::shared_ptr<Generic> GenericPtr;
00093 
00094 template <class T, Type Y>
00095 class Primitive : public Generic {
00096 public:
00097     Primitive(const std::string& n, T t)
00098         : Generic(n, Y),
00099         value_(t)
00100     {}
00101 
00102     const T& value() const {
00103         return value_;
00104     }
00105 
00106 private:
00107     T value_;
00108 };
00109 
00110 typedef Primitive<std::string, STRING_TYPE> String;
00111 typedef boost::shared_ptr<String> StringPtr;
00112 
00113 typedef Primitive<int64_t, INT_TYPE> Int;
00114 typedef boost::shared_ptr<Int> IntPtr;
00115 
00116 typedef Primitive<bool, BOOL_TYPE> Bool;
00117 typedef boost::shared_ptr<Bool> BoolPtr;
00118 
00119 class Base64Bin : public Generic {
00120 public:
00121     Base64Bin(const std::string& n, boost::shared_ptr<std::string> data)
00122         : Generic(n, BASE64BIN_TYPE),
00123         m_data(data)
00124     {}
00125 
00126     const std::string& value() const {
00127         return *m_data;
00128     }
00129 
00130 private:
00131     boost::shared_ptr<std::string> m_data;
00132 };
00133 typedef boost::shared_ptr<Base64Bin> Base64BinPtr;
00134 
00135 typedef Primitive<std::string, QNAME_TYPE> QName; // FIXME: QName's are not simple strings, but they have a Namespace URI, local part and prefix
00136 typedef boost::shared_ptr<QName> QNamePtr;
00137 
00138 class Complex : public Generic {
00139 public:
00140     Complex(const std::string& n, Type t)
00141         : Generic(n, t)
00142     {}
00143 
00144     virtual bool complex() {
00145         return true;
00146     }
00147 };
00148 
00149 template <class T>
00150 class Array : public Complex {
00151 public:
00152     Array(const std::string& n)
00153         : Complex(n, ARRAY_TYPE)
00154     {}
00155 
00156     size_t size() const {
00157         return values_.size();
00158     }
00159 
00160     template <class Y>
00161     boost::shared_ptr< Array< boost::shared_ptr<Y> > > construct() const {
00162         boost::shared_ptr< Array< boost::shared_ptr<Y> > > conv(new Array< boost::shared_ptr<Y> >(name()));
00163         for (typename std::vector< T >::const_iterator it = values_.begin(); it != values_.end(); it++) {
00164             conv->add(Y::construct(*it));
00165         }
00166         return conv;
00167     }
00168 
00169     T get(size_t i) {
00170         return values_[i];
00171     }
00172 
00173     T operator[](size_t i) {
00174         return values_[i];
00175     }
00176 
00177     virtual void add(T element) {
00178         values_.push_back(element);
00179     }
00180 
00181 private:
00182     std::vector< T > values_;
00183 };
00184 typedef boost::shared_ptr< Array<GenericPtr> > ArrayPtr;
00185 
00186 class Collection : public Complex {
00187 public:
00188     Collection(const std::string& n)
00189         : Complex(n, COLLECTION_TYPE)
00190     {}
00191 
00192     // TODO: back this by a multimap
00193 
00194     size_t size() const {
00195         return values_.size();
00196     }
00197 
00198     template <class T>
00199     boost::shared_ptr<T> get(const std::string& n) {
00200         for (std::vector<GenericPtr>::iterator it = values_.begin(); it != values_.end(); it++) {
00201             if ((*it)->name() == n) {
00202                 return (*it)->as<T>();
00203             }
00204         }
00205         return boost::shared_ptr<T>();
00206     }
00207 
00208     template <class T>
00209     boost::shared_ptr<T> operator[](const std::string& n) {
00210         return get<T>(n);
00211     }
00212 
00213     virtual void add(GenericPtr element) {
00214         values_.push_back(element);
00215     }
00216 
00217     std::vector<GenericPtr>& values() {
00218         return values_;
00219     }
00220 
00221 private:
00222     std::vector<GenericPtr> values_;
00223 };
00224 typedef boost::shared_ptr<Collection> CollectionPtr;
00225 
00226 class SoapFault {
00227 public:
00228     SoapFault()
00229         : code_(), string_(), detail_() {
00230     }
00231 
00232     SoapFault(QNamePtr c, StringPtr s, StringPtr det)
00233         : code_(c), string_(s), detail_(det) {
00234     }
00235 
00236     QNamePtr code() const {
00237         return code_;
00238     }
00239 
00240     StringPtr string() const {
00241         return string_;
00242     }
00243 
00244     StringPtr detail() const {
00245         return detail_;
00246     }
00247 
00248 private:
00249     QNamePtr code_;
00250     StringPtr string_;
00251     // TODO: add faultactor actor_;
00252     StringPtr detail_;
00253 };
00254 
00255 }
00256 
00257 #endif /* __SOA_TYPES__ */

Generated on Sun Feb 14 2021 for AbiWord by  doxygen 1.7.1