dialog/request.h

Go to the documentation of this file.
00001 /*
00002  * request.h
00003  *
00004  * Copyright (C) 2008  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  */
00031 
00032 #ifndef WAVEPACKET_DIALOG_REQUEST_H__
00033 #define WAVEPACKET_DIALOG_REQUEST_H__
00034 
00035 // includes --------------------------------------------------------------------
00036 #include "common/common.h"
00037 
00038 #include <sstream>
00039 
00040 
00041 // forward declarations
00042 class Datahash;
00043 
00044 
00045 namespace dialog {
00046 
00047 /// \ingroup dialog
00048 /*@{*/
00049 
00050 
00051 /// object to help construct dialog request streams
00052 ///
00053 /// It is recommended that callers use this object instead of directly creating
00054 /// text streams!  This way your code is relatively insulated if elements add
00055 /// new attributes, etc.  The code is also easier to write and maintain.
00056 ///
00057 /// Typical usage is to create a ContainerRequest, add a bunch of stuff, and
00058 /// then call get() to retrieve the string that represents the dialog stream.
00059 ///
00060 /// Once you call get(), the ContainerRequest is considered closed.  Any
00061 /// additional attempts to add data to the container is a programming failure
00062 /// and the program will hard stop.
00063 ///
00064 /// If you want to recursively add subcontainers (nested elements within the
00065 /// dialog), create separate ContainerRequest objects for each child container
00066 /// and add them to the parent container with addContainer().  Note that adding
00067 /// a ContainerRequest to a parent has the side-effect of closing the child
00068 /// container.
00069 class ContainerRequest {
00070 public:
00071         // public typedefs -----------------------------------------------------
00072         enum eAxis {
00073                         eAxis_Horizontal        = 1,
00074                         eAxis_Vertical          = 2
00075                 };
00076 
00077         // constructor ---------------------------------------------------------
00078         ContainerRequest(eAxis axis = eAxis_Vertical) : m_closed(false) {
00079                         m_stream << "type container\n";
00080                         m_stream << "axis ";
00081                         m_stream << ((eAxis_Horizontal == axis) ? "h" : "v");
00082                         m_stream << "\n";
00083                 }
00084 
00085         // accessors -----------------------------------------------------------
00086         std::string get(void) {
00087                         this->close();
00088                         return m_stream.str();
00089                 }
00090 
00091         smart_ptr<Datahash> getDatahash(void); 
00092 
00093         void addLabel(IN const char * label) {
00094                         ASSERT(!m_closed, "request is closed");
00095                         ASSERT(label, "null");
00096                         m_stream << "element {\n";
00097                         m_stream << "type label\n";
00098                         m_stream << "value " << label << "\n";
00099                         m_stream << "}\n";
00100                 }
00101 
00102         void addWidthString(IN const char * widthString) {
00103                         ASSERT(!m_closed, "request is closed");
00104                         ASSERT(widthString, "null");
00105                         m_stream << "widthString " << widthString << "\n";
00106                 }
00107 
00108         void addButton(IN const char * submit, IN const char * label) {
00109                         ASSERT(!m_closed, "request is closed");
00110                         ASSERT(submit, "null");
00111                         ASSERT(label, "null");
00112                         m_stream << "element {\n";
00113                         m_stream << "type button\n";
00114                         m_stream << "submit " << submit << "\n";
00115                         m_stream << "label " << label << "\n";
00116                         m_stream << "}\n";
00117                 }
00118 
00119         void addTextbox(IN const char * name, IN const char * value,
00120                                 IN int max, IN bool encrypted = false) {
00121                         ASSERT(!m_closed, "request is closed");
00122                         ASSERT(name, "null");
00123                         ASSERT(value, "null");
00124                         ASSERT(max > 0, "Bad textbox max: %d", max);
00125                         m_stream << "element {\n";
00126                         m_stream << "type textbox\n";
00127                         m_stream << "name " << name << "\n";
00128                         m_stream << "value " << value << "\n";
00129                         m_stream << "max " << max << "\n";
00130                         if (encrypted) {
00131                                 m_stream << "encrypt true\n";
00132                         }
00133                         m_stream << "}\n";
00134                 }
00135 
00136         void addContainer(IN ContainerRequest& cr) {
00137                         ASSERT(!m_closed, "request is closed");
00138                         m_stream << "element {\n";
00139                         m_stream << cr.get();
00140                         m_stream << "}\n";
00141                 }
00142 
00143         /// raw access to stream!
00144         void addCustom(IN const char * string) {
00145                         ASSERT(!m_closed, "request is closed");
00146                         m_stream << string << "\n";
00147                 }
00148 private:
00149         // private helper methods ----------------------------------------------
00150         void close(void) {
00151                         if (!m_closed) {
00152                                 m_closed = true;
00153                         }
00154                 }
00155 
00156         // private member data -------------------------------------------------
00157         bool                    m_closed;
00158         std::ostringstream      m_stream;
00159 };
00160 
00161 
00162 
00163 };      // dialog namespace
00164 
00165 #endif  // WAVEPACKET_DIALOG_REQUEST_H__
00166