1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 #ifndef COSV_STRINGDATA_HXX
25 #define COSV_STRINGDATA_HXX
26
27
28 #include <cosv/str_types.hxx>
29 #include <cstring>
30
31
32 namespace csv
33 {
34
35 /** @tpl CHAR
36 The expression CHAR(0) has to be valid.
37 */
38 template <class CHAR>
39 class StringData
40 {
41 public:
42 typedef StringData self;
43
44 typedef str::size size_type;
45 typedef str::position position_type;
46
47 // LIFECYCLE
48 StringData();
49 /** @precond i_pData != 0
50 @precond i_nValidLength <= strlen(i_pData)
51 */
52 StringData(
53 const CHAR * i_pData,
54 size_type i_nValidLength );
55 ~StringData();
56 // OPERATORS
57
58 // OPERATIONS
59
60 // INQUIRY
61 const CHAR * Data() const;
62
63 /** @returns the allocated number of CHAR.
64 This may be different from the number of bytes.
65 There is actually allocated one more CHAR,
66 which is guaranteed to be CHAR(0) in all circumstances.
67 */
68 size_type Size() const;
69
70 private:
71 /* Because this is used only within a refcounted structure,
72 these functions are forbidden - at least yet.
73 */
74 StringData(const self&);
75 self & operator=(const self&);
76
77 // DATA
78 DYN CHAR * dpData;
79 size_type nSize; /// The allocated size - 1 (for the finishing 0).
80 };
81
82
83
84 // IMPLEMENTATION
85
86 template <class CHAR>
StringData()87 StringData<CHAR>::StringData()
88 : dpData( new CHAR[1] ),
89 nSize(0)
90 {
91 *dpData = CHAR(0);
92 }
93
94 template <class CHAR>
StringData(const CHAR * i_pData,size_type i_nValidLength)95 StringData<CHAR>::StringData( const CHAR * i_pData,
96 size_type i_nValidLength )
97 : dpData( new CHAR[i_nValidLength + 1] ),
98 nSize(i_nValidLength)
99 {
100 memcpy( dpData, i_pData, i_nValidLength * sizeof(CHAR) );
101 dpData[nSize] = CHAR(0);
102 }
103
104 template <class CHAR>
~StringData()105 StringData<CHAR>::~StringData()
106 {
107 delete [] dpData;
108 }
109
110 template <class CHAR>
111 const CHAR *
Data() const112 StringData<CHAR>::Data() const
113 {
114 return dpData;
115 }
116
117 template <class CHAR>
118 typename StringData<CHAR>::size_type
Size() const119 StringData<CHAR>::Size() const
120 {
121 return nSize;
122 }
123
124
125
126 } // namespace csv
127
128
129 #endif
130
131
132