1 karl 1.3 //%2006////////////////////////////////////////////////////////////////////////
|
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.
|
11 karl 1.3 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec 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:
|
20 karl 1.3 //
|
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 //%/////////////////////////////////////////////////////////////////////////////
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 mike 1.1
43 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 kumpf 1.4
49 String s("hello world");
|
50 mike 1.1
51 Is less efficient than this:
52
|
53 kumpf 1.4 String s("hello world", 11);
|
54 mike 1.1
55 The first form, forces the String constructor to call strlen(), an O(N)
|
56 kumpf 1.4 operation. This is unfortunate for C string literals since the length can
|
57 mike 1.1 be obtained at compile time with the sizeof operator. For example:
58
|
59 kumpf 1.4 String s("hello world", sizeof("hello world") - 1);
|
60 mike 1.1
61 But repeating the literal twice is error prone, so instead we use the
62 STRLIT_ARGS() macro.
63
|
64 kumpf 1.4 String s(STRLIT_ARGS("hello world"));
|
65 mike 1.1
66 This macro can also be used to define StrLit objects at compile time.
67 For example:
68
|
69 kumpf 1.4 const StrLit DEFAULT_HOSTNAME(STRLIT_ARGS("localhost"));
|
70 mike 1.1
|
71 kumpf 1.4 You can implement functions that take StrLit objects. For example, we
|
72 mike 1.1 define this function:
73
|
74 kumpf 1.4 operator<<(const Buffer&, const StrLit&);
|
75 mike 1.1
76 This function can be used in two ways. You can pass predefined StrLit
77 object to it like this:
78
|
79 kumpf 1.4 Buffer out;
80 out << DEFAULT_HOSTNAME;
|
81 mike 1.1
82 Or you can use the STRLIT() macro to construct on on the fly:
83
|
84 kumpf 1.4 Buffer out;
85 out << STRLIT("localhost");
|
86 mike 1.1
87 Note that the latter form would be faster than this, since somebody
88 is going to have to call strlen() eventually.
89
|
90 kumpf 1.4 Buffer out;
91 out << "localhost";
|
92 mike 1.1
|
93 kumpf 1.4 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 mike 1.1 routines).
97 */
98 struct StrLit
99 {
|
100 kumpf 1.5 StrLit(const char* s, size_t n) : str(s), size((Uint32)n) { }
|
101 mike 1.1 const char* str;
|
102 kumpf 1.5 const Uint32 size;
|
103 mike 1.1 };
104
105 inline Buffer& operator<<(Buffer& out, const StrLit& x)
106 {
|
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 */
|