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