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 addButton(IN const char * submit, IN const char * label) {
00103                         ASSERT(!m_closed, "request is closed");
00104                         ASSERT(submit, "null");
00105                         ASSERT(label, "null");
00106                         m_stream << "element {\n";
00107                         m_stream << "type button\n";
00108                         m_stream << "submit " << submit << "\n";
00109                         m_stream << "label " << label << "\n";
00110                         m_stream << "}\n";
00111                 }
00112 
00113         void addTextbox(IN const char * name, IN const char * value,
00114                                 IN int max, IN bool encrypted = false) {
00115                         ASSERT(!m_closed, "request is closed");
00116                         ASSERT(name, "null");
00117                         ASSERT(value, "null");
00118                         ASSERT(max > 0, "Bad textbox max: %d", max);
00119                         m_stream << "element {\n";
00120                         m_stream << "type textbox\n";
00121                         m_stream << "name " << name << "\n";
00122                         m_stream << "value " << value << "\n";
00123                         m_stream << "max " << max << "\n";
00124                         if (encrypted) {
00125                                 m_stream << "encrypt true\n";
00126                         }
00127                         m_stream << "}\n";
00128                 }
00129 
00130         void addContainer(IN ContainerRequest& cr) {
00131                         ASSERT(!m_closed, "request is closed");
00132                         m_stream << "element {\n";
00133                         m_stream << cr.get();
00134                         m_stream << "}\n";
00135                 }
00136 
00137         /// raw access to stream!
00138         void addCustom(IN const char * string) {
00139                         ASSERT(!m_closed, "request is closed");
00140                         m_stream << string << "\n";
00141                 }
00142 private:
00143         // private helper methods ----------------------------------------------
00144         void close(void) {
00145                         if (!m_closed) {
00146                                 m_closed = true;
00147                         }
00148                 }
00149 
00150         // private member data -------------------------------------------------
00151         bool                    m_closed;
00152         std::ostringstream      m_stream;
00153 };
00154 
00155 
00156 
00157 };      // dialog namespace
00158 
00159 #endif  // WAVEPACKET_DIALOG_REQUEST_H__
00160