vgfx/request.h

Go to the documentation of this file.
00001 /*
00002  * request.h
00003  *
00004  * Copyright (C) 2007  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  * Utility methods for constructing vgfx requests.
00032  */
00033 
00034 #ifndef WAVEPACKET_VGFX_REQUEST_H__
00035 #define WAVEPACKET_VGFX_REQUEST_H__
00036 
00037 // includes --------------------------------------------------------------------
00038 #include "vgfx.h"
00039 
00040 #include <sstream>
00041 
00042 
00043 namespace vgfx {
00044 
00045 /// \ingroup vgfx
00046 /*@{*/
00047 
00048 
00049 /// used to easily construct vgfx Requests
00050 ///
00051 /// To use, create one of these, add a bunch of stuff, and then call
00052 /// get().  That closes the request and returns a string containing
00053 /// the full request.
00054 ///
00055 /// Note that once a request has been closed, you cannot add any more
00056 /// objects to it!  Subsequent addXXX() calls will fail (program crash,
00057 /// since to do so is a programming mistake).
00058 class Request {
00059 public:
00060         Request(void) : m_closed(false) {
00061                         m_stream << "request {\n";
00062                 }
00063 
00064         void close(void) {
00065                         if (!m_closed) {
00066                                 m_stream << "}";
00067                                 m_closed = true;
00068                         }
00069                 }
00070 
00071         std::string get(void) {
00072                         this->close();
00073                         return m_stream.str();
00074                 }
00075 
00076         void addBezier(IN const char * id,
00077                         IN float x0, IN float ax, IN float bx, IN float cx,
00078                         IN float y0, IN float ay, IN float by, IN float cy) {
00079                         ASSERT(!m_closed, "request is closed");
00080                         m_stream << "add bezier id " << id;
00081                         m_stream << " x0 " << x0 << " ax " << ax;
00082                         m_stream << " bx " << bx << " cx " << cx;
00083                         m_stream << " y0 " << y0 << " ay " << ay;
00084                         m_stream << " by " << by << " cy " << cy;
00085                         m_stream << "\n";
00086                 }
00087 
00088         void addQuadBezier(IN const char * id, IN const point_t& p0,
00089                                 IN const point_t& p1, IN const point_t& p2) {
00090                         ASSERT(!m_closed, "request is closed");
00091 
00092                         m_stream << "add quad id " << id;
00093                         m_stream << " x0 " << p0.x << " y0 " << p0.y;
00094                         m_stream << " x1 " << p1.x << " y1 " << p1.y;
00095                         m_stream << " x2 " << p2.x << " y2 " << p2.y;
00096                         m_stream << "\n";
00097                 }
00098 
00099         void addRect(IN const char * id,
00100                                 IN float left, IN float top,
00101                                 IN float right, IN float bottom,
00102                                 IN bool fill) {
00103                         ASSERT(!m_closed, "request is closed");
00104                         m_stream << "add rect id " << id << " left " << left;
00105                         m_stream << " top " << top;
00106                         m_stream << " right " << right << " bottom " << bottom;
00107                         if (fill) {
00108                                 m_stream << " fill true";
00109                         }
00110                         m_stream << "\n";
00111                 }
00112 
00113         void addSimpleRect(IN const char * id,
00114                                 IN float width, IN float height, IN bool fill) {
00115                         this->addRect(id, 0, 0, width, height, fill);
00116                 }
00117 
00118         void addText(IN const char * id, IN const char * text) {
00119                         ASSERT(!m_closed, "request is closed");
00120                         m_stream << "add text id " << id;
00121                         m_stream << " text \"" << text << "\"\n";
00122                 }
00123 
00124         void addSimpleGroup(IN const char * id) {
00125                         ASSERT(!m_closed, "request is closed");
00126                         m_stream << "add group {\n";
00127                         m_stream << "\tid\t" << id << "\n";
00128                         m_stream << "\t}\n";
00129                 }
00130 
00131         void addMeta(IN const char * group_id,
00132                                 IN const char * name,
00133                                 IN const char * value) {
00134                         ASSERT(!m_closed, "request is closed");
00135                         m_stream << "set " << group_id << ".meta." << name;
00136                         m_stream << "\t" << name << " \"" << value << "\"\n";
00137                 }
00138 
00139         void addObject(IN const char * group_id, IN const char * tag,
00140                                 IN const char * ref,
00141                                 IN float x, IN float y,
00142                                 IN float z, IN float phi = 0.0) {
00143                         ASSERT(!m_closed, "request is closed");
00144                         m_stream << "set " << group_id << ".object." << tag;
00145                         m_stream << "\ttag " << tag << "\tid " << ref;
00146                         m_stream << "\tx " << x << "\ty " << y;
00147                         m_stream << "\tz " << z;
00148                         m_stream << "\tphi " << phi << "\n";
00149                 }
00150 
00151         void setDictionary(IN const char * path, IN const dictionary_t& data) {
00152                         ASSERT(!m_closed, "request is closed");
00153                         m_stream << "set " << path;
00154                         for (dictionary_t::const_iterator i = data.begin();
00155                              i != data.end(); ++i) {
00156                                 m_stream << " " << i->first << " \"";
00157                                 m_stream << i->second << "\"";
00158                         }
00159                         m_stream << "\n";
00160                 }
00161 
00162         void removeObject(IN const char * path) {
00163                         ASSERT(!m_closed, "request is closed");
00164                         m_stream << "remove " << path << "\n";
00165                 }
00166 
00167         std::ostream& getStream(void) {
00168                         ASSERT(!m_closed, "request is closed");
00169                         return m_stream; 
00170                 }
00171         
00172 private:
00173         std::ostringstream      m_stream;
00174         bool                    m_closed;
00175 };
00176 
00177 
00178 };      // namespace vgfx
00179 
00180 
00181 #endif  // WAVEPACKET_VGFX_REQUEST_H__
00182