~zorba-coders/zorba/ordpathmsgs

« back to all changes in this revision

Viewing changes to src/api/transcode_streambuf.cpp

Augmented API for transcoding streambuf. Approved: Chris Hillery, Matthias Brantner

Show diffs side-by-side

added added

removed removed

Lines of Context:
99
99
///////////////////////////////////////////////////////////////////////////////
100
100
 
101
101
} // namespace transcode
 
102
 
 
103
///////////////////////////////////////////////////////////////////////////////
 
104
 
 
105
namespace internal {
 
106
 
 
107
// Both new & delete are done here inside Zorba rather than in the header to
 
108
// guarantee that they're cross-DLL-boundary safe on Windows.
 
109
 
 
110
zorba::transcode::streambuf*
 
111
alloc_streambuf( char const *charset, std::streambuf *orig ) {
 
112
  return new zorba::transcode::streambuf( charset, orig );
 
113
}
 
114
 
 
115
void dealloc_streambuf( zorba::transcode::streambuf *buf ) {
 
116
  delete buf;
 
117
}
 
118
 
 
119
int get_streambuf_index() {
 
120
  //
 
121
  // This function is out-of-line because it has a static constant within it.
 
122
  // It has a static constant within it to guarantee (1) initialization before
 
123
  // use and (2) initialization happens exactly once.
 
124
  //
 
125
  // See: "Standard C++ IOStreams and Locales: Advanced Programmer's Guide and
 
126
  // Reference," Angelika Langer and Klaus Kreft, Addison-Wesley, 2000, section
 
127
  // 3.3.1.1: "Initializing and Maintaining the iword/pword Index."
 
128
  //
 
129
  // See: "The C++ Programming Language," Bjarne Stroustrup, Addison-Wesley,
 
130
  // 2000, section 10.4.8: "Local Static Store."
 
131
  //
 
132
  static int const index = ios_base::xalloc();
 
133
  return index;
 
134
}
 
135
 
 
136
void stream_callback( ios_base::event e, ios_base &ios, int index ) {
 
137
  //
 
138
  // See: "Standard C++ IOStreams and Locales: Advanced Programmer's Guide and
 
139
  // Reference," Angelika Langer and Klaus Kreft, Addison-Wesley, 2000, section
 
140
  // 3.3.1.4: "Using Stream Callbacks for Memory Management."
 
141
  //
 
142
  if ( e == ios_base::erase_event )
 
143
    delete static_cast<streambuf*>( ios.pword( index ) );
 
144
}
 
145
 
 
146
} // namespace internal
 
147
 
 
148
///////////////////////////////////////////////////////////////////////////////
 
149
 
102
150
} // namespace zorba
103
151
/* vim:set et sw=2 ts=2: */