Synesis Software STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ...

fastformat/bundles/openrj_bundle.hpp

Go to the documentation of this file.
00001 /* /////////////////////////////////////////////////////////////////////////
00002  * File:        fastformat/bundles/openrj_bundle.hpp
00003  *
00004  * Purpose:     Open-RJ bundle.
00005  *
00006  * Created:     19th September 2006
00007  * Updated:     11th August 2009
00008  *
00009  * Home:        http://www.fastformat.org/
00010  *
00011  * Copyright (c) 2007-2009, Matthew Wilson and Synesis Software
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are
00016  * met:
00017  *
00018  * - Redistributions of source code must retain the above copyright notice,
00019  *   this list of conditions and the following disclaimer.
00020  * - Redistributions in binary form must reproduce the above copyright
00021  *   notice, this list of conditions and the following disclaimer in the
00022  *   documentation and/or other materials provided with the distribution.
00023  * - Neither the names of Matthew Wilson and Synesis Software nor the names
00024  *   of any contributors may be used to endorse or promote products derived
00025  *   from this software without specific prior written permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00028  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
00029  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00030  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00031  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00032  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00033  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00034  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00035  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00036  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00037  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00038  *
00039  * ////////////////////////////////////////////////////////////////////// */
00040 
00041 
00047 #ifndef FASTFORMAT_INCL_FASTFORMAT_BUNDLES_HPP_OPENRJ_BUNDLE
00048 #define FASTFORMAT_INCL_FASTFORMAT_BUNDLES_HPP_OPENRJ_BUNDLE
00049 
00050 /* /////////////////////////////////////////////////////////////////////////
00051  * Version information
00052  */
00053 
00054 #ifndef FASTFORMAT_DOCUMENTATION_SKIP_SECTION
00055 # define FASTFORMAT_VER_FASTFORMAT_BUNDLES_HPP_OPENRJ_BUNDLE_MAJOR      1
00056 # define FASTFORMAT_VER_FASTFORMAT_BUNDLES_HPP_OPENRJ_BUNDLE_MINOR      0
00057 # define FASTFORMAT_VER_FASTFORMAT_BUNDLES_HPP_OPENRJ_BUNDLE_REVISION   9
00058 # define FASTFORMAT_VER_FASTFORMAT_BUNDLES_HPP_OPENRJ_BUNDLE_EDIT       14
00059 #endif /* !FASTFORMAT_DOCUMENTATION_SKIP_SECTION */
00060 
00061 /* /////////////////////////////////////////////////////////////////////////
00062  * Language
00063  */
00064 
00065 #ifndef __cplusplus
00066 # error This file can only be included in C++ compilation units
00067 #endif /* !__cplusplus */
00068 
00069 /* /////////////////////////////////////////////////////////////////////////
00070  * Includes
00071  */
00072 
00073 /* FastFormat Header Files */
00074 #include <fastformat/fastformat.h>
00075 //#include <fastformat/bundles/common.hpp>
00076 
00077 /* Open-RJ Header Files */
00078 #include <openrj/openrj.h>          // Open-RJ C API
00079 #include <openrj/cpp/openrj.hpp>    // Open-RJ C++ API
00080 
00081 /* STLSoft Header Files */
00082 #include <stlsoft/shims/access/string.hpp>
00083 
00084 /* C++ Standard Header Files */
00085 #include <stdexcept>
00086 
00087 /* /////////////////////////////////////////////////////////////////////////
00088  * Namespace
00089  */
00090 
00091 #if !defined(FASTFORMAT_NO_NAMESPACE)
00092 namespace fastformat
00093 {
00094 #endif /* !FASTFORMAT_NO_NAMESPACE */
00095 
00096 /* /////////////////////////////////////////////////////////////////////////
00097  * Classes
00098  */
00099 
00102 class openrj_bundle
00103 {
00106 public:
00107     typedef ::openrj::ORJString     string_type;
00108     typedef openrj_bundle           class_type;
00110 
00113 public:
00114     enum
00115     {
00116             ignoreCaseOnLookup  =   0x01    
00117         ,   treatSourceAsMemory =   0x02    
00118     };
00120 
00123 public:
00124     explicit openrj_bundle(char const *source, unsigned flags = 0);
00125 
00126     template <typename S>
00127     explicit openrj_bundle(S const &source, unsigned flags = 0)
00128         : m_database(open_ns_(source, flags))
00129     {}
00130 
00131     ~openrj_bundle() throw();
00133 
00136 public:
00137     string_type operator [](char const *name) const throw(std::out_of_range);
00138     template <typename S>
00139     string_type operator [](S const& name) const throw(std::out_of_range)
00140     {
00141         FASTFORMAT_DECLARE_c_str_ptr_();
00142 
00143         return operator [](FASTFORMAT_INVOKE_c_str_ptr_(FASTFORMAT_INVOKE_c_str_ptr_(source)));
00144     }
00146 
00149 private:
00150     // We have to have this funky level of indirection due to DMC++ and GCC compilers'
00151     // weird behaviour wrt handling shims' fully qualified names in templates.
00152     template <typename S>
00153     static ::openrj::ORJDatabase const* open_ns_(S const &source, unsigned flags)
00154     {
00155         FASTFORMAT_DECLARE_c_str_ptr_();
00156 
00157         return open_(FASTFORMAT_INVOKE_c_str_ptr_(source), flags);
00158     }
00159     static ::openrj::ORJDatabase const *open_(char const *source, unsigned flags);
00161 
00164 private:
00165     ::openrj::ORJDatabase const *m_database;
00167 
00170 private:
00171     openrj_bundle(class_type const&);
00172     class_type& operator =(class_type const&);
00174 };
00175 
00176 /* /////////////////////////////////////////////////////////////////////////
00177  * Implementation
00178  */
00179 
00180 #ifndef FASTFORMAT_DOCUMENTATION_SKIP_SECTION
00181 
00182 inline /* static */ ::openrj::ORJDatabase const *openrj_bundle::open_(char const *source, unsigned flags)
00183 {
00184 #if !defined(ORJ_NO_NAMESPACE)
00185     using namespace openrj;
00186     using namespace openrj::cpp;
00187 #endif /* !ORJ_NO_NAMESPACE */
00188 
00189     unsigned    orjFlags    =   0
00190                             |   ORDER_FIELDS
00191                             |   ELIDE_BLANK_RECORDS
00192                             |   FORCE_ALL_FIELDS_INTO_1_RECORD
00193                             ;
00194 
00195     if(ignoreCaseOnLookup & flags)
00196     {
00197         orjFlags |= IGNORE_CASE_ON_LOOKUP;
00198     }
00199 
00200     ORJError            error;
00201     ORJRC               rc;
00202     ORJDatabase const   *database;
00203 
00204     if(treatSourceAsMemory & flags)
00205     {
00206         rc = ORJ_CreateDatabaseFromMemory(source, ::strlen(source), NULL, orjFlags, &database, &error);
00207     }
00208     else
00209     {
00210         rc = ORJ_ReadDatabase(source, NULL, orjFlags, &database, &error);
00211     }
00212 
00213     if(ORJ_RC_SUCCESS != rc)
00214     {
00215         throw DatabaseException(rc, error);
00216     }
00217 
00218     return database;
00219 }
00220 
00221 inline openrj_bundle::openrj_bundle(char const *source, unsigned flags)
00222     : m_database(open_ns_(source, flags))
00223 {}
00224 
00225 inline openrj_bundle::~openrj_bundle() throw()
00226 {
00227 #if !defined(ORJ_NO_NAMESPACE)
00228     using namespace openrj;
00229 #endif /* !ORJ_NO_NAMESPACE */
00230 
00231     if(NULL != m_database)
00232     {
00233         ORJ_FreeDatabase(m_database);
00234     }
00235 }
00236 
00237 inline openrj_bundle::string_type openrj_bundle::operator [](char const *name) const throw(std::out_of_range)
00238 {
00239 #if !defined(ORJ_NO_NAMESPACE)
00240     using namespace openrj;
00241 #endif /* !ORJ_NO_NAMESPACE */
00242 
00243     FASTFORMAT_CONTRACT_ENFORCE_CLASS_INVARIANT_APPL_LAYER(NULL != m_database, "underlying database cannot be null");
00244 
00245     ORJField const  *field  =   ORJ_Record_FindFieldByNameA(&m_database->records[0], name, NULL);
00246 
00247     if(NULL == field)
00248     {
00249         throw std::out_of_range("format string not found");
00250     }
00251 
00252     return field->value;
00253 }
00254 
00255 #endif /* FASTFORMAT_DOCUMENTATION_SKIP_SECTION */
00256 
00257 /* /////////////////////////////////////////////////////////////////////////
00258  * Namespace
00259  */
00260 
00261 #if !defined(FASTFORMAT_NO_NAMESPACE)
00262 } /* namespace fastformat */
00263 #endif /* !FASTFORMAT_NO_NAMESPACE */
00264 
00265 /* ////////////////////////////////////////////////////////////////////////// */
00266 
00267 #endif /* FASTFORMAT_INCL_FASTFORMAT_BUNDLES_HPP_OPENRJ_BUNDLE */
00268 
00269 /* ///////////////////////////// end of file //////////////////////////// */

FastFormat Library documentation © Matthew Wilson, 2006-2009 SourceForge.net Logo