Open-RJ C/C++ User's Journal Synesis Software STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ...

openrj/cpp/functions.hpp

Go to the documentation of this file.
00001 /* /////////////////////////////////////////////////////////////////////////////
00002  * File:    openrj/cpp/functions.hpp
00003  *
00004  * Purpose: Helper functions for the Open-RJ C++ mapping
00005  *
00006  * Created: 12th April 2005
00007  * Updated: 14th July 2006
00008  *
00009  * Home:    http://openrj.org/
00010  *
00011  * Copyright (c) 2004-2006, 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 met:
00016  *
00017  * - Redistributions of source code must retain the above copyright notice, this
00018  *   list of conditions and the following disclaimer.
00019  * - Redistributions in binary form must reproduce the above copyright notice,
00020  *   this list of conditions and the following disclaimer in the documentation
00021  *   and/or other materials provided with the distribution.
00022  * - Neither the names of Matthew Wilson and Synesis Software nor the names of
00023  *   any contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036  * POSSIBILITY OF SUCH DAMAGE.
00037  *
00038  * ////////////////////////////////////////////////////////////////////////// */
00039 
00040 
00046 #ifndef OPENRJ_INCL_OPENRJ_CPP_HPP_FUNCTIONS
00047 #define OPENRJ_INCL_OPENRJ_CPP_HPP_FUNCTIONS
00048 
00049 /* /////////////////////////////////////////////////////////////////////////////
00050  * Version information
00051  */
00052 
00053 #ifndef OPENRJ_DOCUMENTATION_SKIP_SECTION
00054 # define OPENRJ_VER_OPENRJ_CPP_HPP_FUNCTIONS_MAJOR      1
00055 # define OPENRJ_VER_OPENRJ_CPP_HPP_FUNCTIONS_MINOR      1
00056 # define OPENRJ_VER_OPENRJ_CPP_HPP_FUNCTIONS_REVISION   3
00057 # define OPENRJ_VER_OPENRJ_CPP_HPP_FUNCTIONS_EDIT       8
00058 #endif /* !OPENRJ_DOCUMENTATION_SKIP_SECTION */
00059 
00060 /* /////////////////////////////////////////////////////////////////////////////
00061  * Includes
00062  */
00063 
00064 #include <openrj/cpp/openrj.hpp>
00065 #include <openrj/cpp/field.hpp>
00066 #include <openrj/cpp/record.hpp>
00067 #include <openrj/cpp/databasebase.hpp>
00068 
00069 #include <stlsoft/memory/auto_buffer.hpp>
00070 #include <stlsoft/shims/access/string.hpp>
00071 #include <stdio.h>
00072 
00073 /* /////////////////////////////////////////////////////////////////////////////
00074  * Namespace
00075  */
00076 
00077 namespace openrj
00078 {
00079 namespace cpp
00080 {
00081 
00082 /* /////////////////////////////////////////////////////////////////////////////
00083  * Functions
00084  */
00085 
00086 #ifndef OPENRJ_DOCUMENTATION_SKIP_SECTION
00087 namespace helper
00088 {
00089     inline Record throw_for_missing_Record(char const *reason, char const *recordName)
00090     {
00091         try
00092         {
00093             const size_t                cchReason       =   ::strlen(reason);
00094             const size_t                cchRecordName   =   ::strlen(recordName);
00095             stlsoft::auto_buffer<char>  message(cchReason + 2 + cchRecordName + 1);
00096 
00097             if(message.empty())
00098             {
00099                 goto plain_throw;
00100             }
00101             else
00102             {
00103                 ::sprintf(&message[0], "%*s: %*s", (int)cchReason, reason, (int)cchRecordName, recordName);
00104 
00105                 throw ::std::out_of_range(message.data());
00106             }
00107         }
00108         catch(std::out_of_range &)
00109         {
00110             throw;
00111         }
00112         catch(...)
00113         {
00114 plain_throw:
00115             throw ::std::out_of_range(reason);
00116         }
00117 
00118         return Record();
00119     }
00120 
00121 } // namespace helper
00122 #endif /* !OPENRJ_DOCUMENTATION_SKIP_SECTION */
00123 
00124 /* /////////////////////////////////////////////////////////////////////////////
00125  * Functions
00126  */
00127 
00135 inline String Lookup(char const *fieldName, Record const &r0, char const *defaultValue)
00136 {
00137     return r0.HasField(fieldName) ? r0[fieldName] : defaultValue;
00138 }
00139 
00149 inline String Lookup(char const *fieldName, Record const &r0, Record const &r1)
00150 {
00151     return r0.HasField(fieldName) ? r0[fieldName] : r1[fieldName];
00152 }
00153 
00162 inline String Lookup(char const *fieldName, Record const &r0, Record const &r1, char const *defaultValue)
00163 {
00164     return r0.HasField(fieldName) ? r0[fieldName] : r1.HasField(fieldName) ? r1[fieldName] : defaultValue;
00165 }
00166 
00167 
00168 inline Record FindFirstRecordWithFieldName(DatabaseBase const &db, char const *name)
00169 {
00170     { for(size_t i = 0; i < db.GetNumRecords(); ++i)
00171     {
00172         if(db[i].HasField(name))
00173         {
00174             return db[i];
00175         }
00176     }}
00177 
00178     return helper::throw_for_missing_Record("Matching record not found", name);
00179 }
00180 
00181 template <typename S>
00182 inline Record FindFirstRecordWithFieldName(DatabaseBase const &db, S const &name)
00183 {
00184     return FindFirstRecordWithFieldName(db, ::stlsoft::c_str_ptr(name));
00185 }
00186 
00187 #if 0
00188 inline 
00189 
00190 inline Record find_first_record_with_field(DatabaseBase const &db, Field const &f)
00191 {
00192     { for(size_t i = 0; i < db.GetNumRecords(); ++i)
00193     {
00194         if(db[i].HasFieldWithValue(name, value))
00195         {
00196             return db[i];
00197         }
00198     }
00199 
00200     throw ::std::out_of_range("Matching record not found");
00201 
00202     return Record();
00203 }
00204 
00205 
00206 inline Record find_first_record_match(DatabaseBase const &db, Field const &f)
00207 {
00208 }
00209 
00210 template <typename S>
00211 inline Record find_first_record_match(DatabaseBase const &db, S const &name)
00212 {
00213 }
00214 
00215 inline Record find_first_record_match(DatabaseBase const &db, Field const &f)
00216 {
00217 }
00218 
00219 #endif /* 0 */
00220 
00221 /* /////////////////////////////////////////////////////////////////////////////
00222  * Namespace
00223  */
00224 
00225 } // namespace cpp
00226 } // namespace openrj
00227 
00228 /* ////////////////////////////////////////////////////////////////////////// */
00229 
00230 #endif /* !OPENRJ_INCL_OPENRJ_CPP_HPP_FUNCTIONS */
00231 
00232 /* ////////////////////////////////////////////////////////////////////////// */

Open-RJ Library documentation © Synesis Software Pty Ltd, 2004-2005