(file) Return to StrLit.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 mike  1.1 //%2005////////////////////////////////////////////////////////////////////////
  2           //
  3           // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
  4           // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
  5           // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
  6           // IBM Corp.; EMC Corporation, The Open Group.
  7           // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
  8           // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
  9           // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 10           // EMC Corporation; VERITAS Software Corporation; The Open Group.
 11           //
 12           // Permission is hereby granted, free of charge, to any person obtaining a copy
 13           // of this software and associated documentation files (the "Software"), to
 14           // deal in the Software without restriction, including without limitation the
 15           // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 16           // sell copies of the Software, and to permit persons to whom the Software is
 17           // furnished to do so, subject to the following conditions:
 18           //
 19           // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 20           // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 21           // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 22 mike  1.1 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 23           // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 24           // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 25           // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 26           // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27           //
 28           //==============================================================================
 29           //
 30           // Author: Mike Brasher, Inova Europe (mike-brasher@austin.rr.com)
 31           //
 32           //%/////////////////////////////////////////////////////////////////////////////
 33           
 34           #ifndef Pegasus_StrLit_h
 35           #define Pegasus_StrLit_h
 36           
 37           #include <Pegasus/Common/Config.h>
 38           #include <Pegasus/Common/Buffer.h>
 39           
 40           #define STRLIT_ARGS(STR) STR, (sizeof(STR)-1)
 41           #define STRLIT(STR) StrLit(STRLIT_ARGS(STR))
 42           
 43 mike  1.1 PEGASUS_NAMESPACE_BEGIN
 44           
 45           /*  The StrLit class and associated macros provide a mechanism for retaining the
 46               length C string literals at compile time. This is preferrable to repeated
 47               recalculation of the length, usually with strlen(). For example, this:
 48              
 49           	String s("hello world");
 50           
 51               Is less efficient than this:
 52           
 53           	String s("hello world", 11);
 54           
 55               The first form, forces the String constructor to call strlen(), an O(N)
 56               operation. This is unfortunate for C string literals since the length can 
 57               be obtained at compile time with the sizeof operator. For example:
 58           
 59           	String s("hello world", sizeof("hello world") - 1);
 60           
 61               But repeating the literal twice is error prone, so instead we use the
 62               STRLIT_ARGS() macro.
 63           
 64 mike  1.1 	String s(STRLIT_ARGS("hello world"));
 65           
 66               This macro can also be used to define StrLit objects at compile time.
 67               For example:
 68           
 69           	const StrLit DEFAULT_HOSTNAME(STRLIT_ARGS("localhost"));
 70           
 71               You can implement functions that take StrLit objects. For example, we 
 72               define this function:
 73           
 74           	operator<<(const Buffer&, const StrLit&);
 75           
 76               This function can be used in two ways. You can pass predefined StrLit
 77               object to it like this:
 78           
 79           	Buffer out;
 80           	out << DEFAULT_HOSTNAME;
 81           
 82               Or you can use the STRLIT() macro to construct on on the fly:
 83           
 84           	Buffer out;
 85 mike  1.1 	out << STRLIT("localhost");
 86           
 87               Note that the latter form would be faster than this, since somebody
 88               is going to have to call strlen() eventually.
 89           
 90           	Buffer out;
 91           	out << "localhost";
 92           
 93               At first glance, this may seem like a small optimization, but this 
 94               technique alone was used to decrease the Pegasus CIM server latency 
 95               by ten percent (with only moderate application to the XML marshalling 
 96               routines).
 97           */
 98           struct StrLit
 99           {
100               StrLit(const char* s, size_t n) : str(s), size(n) { }
101               const char* str;
102               const size_t size;
103           };
104           
105           inline Buffer& operator<<(Buffer& out, const StrLit& x)
106 mike  1.1 {
107 mike  1.2     out.append(x.str, x.size);
108 mike  1.1     return out;
109           }
110           
111           PEGASUS_NAMESPACE_END
112           
113           #endif /* Pegasus_StrLit_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2