|
1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include "UM_STD.H" |
|
17 |
|
18 const TUint32 KFirstStreamIdValue=KNullStreamIdValue+1; |
|
19 const TInt KBufStoreGranularity=4; |
|
20 |
|
21 EXPORT_C CBufStore* CBufStore::NewL(TInt anExpandSize) |
|
22 /** Allocates and constructs a new in-memory store and returns a pointer to it. |
|
23 |
|
24 @param anExpandSize The granularity of the buffers used in the implementation |
|
25 of the store. Each stream is contained in a separate CBufSeg buffer. |
|
26 @return A pointer to the memory store object. */ |
|
27 { |
|
28 return new(ELeave) CBufStore(anExpandSize); |
|
29 } |
|
30 |
|
31 EXPORT_C CBufStore* CBufStore::NewLC(TInt anExpandSize) |
|
32 /** Allocates and constructs a new in-memory store and returns a pointer to it, |
|
33 putting a pointer to the object onto the cleanup stack. |
|
34 |
|
35 Putting a pointer to the object on the cleanup stack allows the object and |
|
36 allocated resources to be cleaned up if a subsequent leave occurs. |
|
37 |
|
38 @param anExpandSize The granularity of the buffers used in the implementation |
|
39 of the store. Each stream is contained in a separate CBufSeg buffer. |
|
40 @return A pointer to the memory store object. */ |
|
41 { |
|
42 CBufStore* store=NewL(anExpandSize); |
|
43 CleanupStack::PushL(store); |
|
44 return store; |
|
45 } |
|
46 |
|
47 EXPORT_C CBufStore::CBufStore(TInt anExpandSize) |
|
48 // |
|
49 // Construct a buffer store. |
|
50 // |
|
51 : iBufArray(KBufStoreGranularity),iExpandSize(anExpandSize) |
|
52 {} |
|
53 |
|
54 EXPORT_C CBufStore::~CBufStore() |
|
55 /** Frees resources owned by the object, prior to its destruction. */ |
|
56 { |
|
57 for (TInt i=0,n=iBufArray.Count(); i<n; i++) |
|
58 delete iBufArray[i]; |
|
59 } |
|
60 |
|
61 EXPORT_C TStreamId CBufStore::DoExtendL() |
|
62 // |
|
63 // Create a new buffer slot. |
|
64 // |
|
65 { |
|
66 iBufArray.AppendL((CBufSeg*)NULL); |
|
67 return TStreamId(iBufArray.Count()-1+KFirstStreamIdValue); |
|
68 } |
|
69 |
|
70 EXPORT_C void CBufStore::DoDeleteL(TStreamId anId) |
|
71 // |
|
72 // Delete the buffer at anId. |
|
73 // |
|
74 { |
|
75 TInt i=anId.Value()-KFirstStreamIdValue; |
|
76 if (i<0||i>=iBufArray.Count()) |
|
77 __LEAVE(KErrNotFound); |
|
78 // |
|
79 CBufSeg*& buf=iBufArray[i]; |
|
80 delete buf; |
|
81 buf=NULL; |
|
82 } |
|
83 |
|
84 EXPORT_C MStreamBuf* CBufStore::DoReadL(TStreamId anId) const |
|
85 // |
|
86 // Open a stream for reading from the buffer at anId. |
|
87 // |
|
88 { |
|
89 return HBufBuf::NewL(BufL(anId),0,HBufBuf::ERead); |
|
90 } |
|
91 |
|
92 EXPORT_C MStreamBuf* CBufStore::DoCreateL(TStreamId& anId) |
|
93 // |
|
94 // Create a new buffer and open a stream for writing to it. |
|
95 // |
|
96 { |
|
97 anId=DoExtendL(); |
|
98 return HBufBuf::NewL(BufL(anId),0,HBufBuf::ERead|HBufBuf::EWrite); |
|
99 } |
|
100 |
|
101 EXPORT_C MStreamBuf* CBufStore::DoWriteL(TStreamId anId) |
|
102 // |
|
103 // Open a stream for writing to the buffer at anId. |
|
104 // |
|
105 { |
|
106 return HBufBuf::NewL(BufL(anId),0,HBufBuf::ERead|HBufBuf::EWrite); |
|
107 } |
|
108 |
|
109 EXPORT_C MStreamBuf* CBufStore::DoReplaceL(TStreamId anId) |
|
110 // |
|
111 // Open a truncating stream for writing to the buffer at anId. |
|
112 // |
|
113 { |
|
114 return HBufBuf::NewL(BufL(anId),0,HBufBuf::ERead|HBufBuf::ETruncate); |
|
115 } |
|
116 |
|
117 CBufSeg& CBufStore::BufL(TStreamId anId) const |
|
118 // |
|
119 // Return this buffer store's buffer at anId. |
|
120 // |
|
121 { |
|
122 TInt i=anId.Value()-KFirstStreamIdValue; |
|
123 if (i<0||i>=iBufArray.Count()) |
|
124 __LEAVE(KErrNotFound); |
|
125 // |
|
126 CBufSeg*& buf=CONST_CAST(CArrayFixFlat<CBufSeg*>&,iBufArray)[i]; |
|
127 if (buf==NULL) |
|
128 buf=CBufSeg::NewL(iExpandSize); |
|
129 return *buf; |
|
130 } |
|
131 |