1// Copyright 2008 Google Inc.
2// Author: Lincoln Smith
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#ifndef OPEN_VCDIFF_OUTPUT_STRING_H_
17#define OPEN_VCDIFF_OUTPUT_STRING_H_
18
19#include <stddef.h>  // size_t
20
21namespace open_vcdiff {
22
23// This interface allows clients of VCDiff[Streaming]Encoder and
24// VCDiff[Streaming]Decoder to use different string types to receive the output
25// of those interfaces.
26//
27// Only the following operations can be performed on an output string, and their
28// semantics must be identical to the std::string methods of the same names:
29//     append()
30//     clear()
31//     push_back()
32//     size()
33//
34// The versions of these methods that take a std::string argument are not
35// supported by OutputStringInterface.
36//
37// There is one additional operation that can be performed on an output string:
38// ReserveAdditionalBytes().  This asks the underlying output type to reserve
39// enough capacity for the number of additional bytes requested in addition to
40// existing content.  The decoder knows the total expected output size in
41// advance, so one large ReserveAdditionalBytes() operation precedes many small
42// append() operations.  For output types that gain no advantage from knowing in
43// advance how many bytes will be appended, ReserveAdditionalBytes() can be
44// defined to do nothing.
45class OutputStringInterface {
46 public:
47  virtual ~OutputStringInterface() { }
48
49  virtual OutputStringInterface& append(const char* s, size_t n) = 0;
50
51  virtual void clear() = 0;
52
53  virtual void push_back(char c) = 0;
54
55  virtual void ReserveAdditionalBytes(size_t res_arg) = 0;
56
57  virtual size_t size() const = 0;
58};
59
60// This template can be used to wrap any class that supports the operations
61// needed by OutputStringInterface, including std::string.  A class that has
62// different names or syntax for these operations will need specialized
63// definitions of OutputString methods -- see output_string_types.h for some
64// examples of how to do this.
65template<class StringClass>
66class OutputString : public OutputStringInterface {
67 public:
68  explicit OutputString(StringClass* impl) : impl_(impl) { }
69
70  virtual ~OutputString() { }
71
72  virtual OutputString& append(const char* s, size_t n) {
73    impl_->append(s, n);
74    return *this;
75  }
76
77  virtual void clear() {
78    impl_->clear();
79  }
80
81  virtual void push_back(char c) {
82    impl_->push_back(c);
83  }
84
85  virtual void ReserveAdditionalBytes(size_t res_arg) {
86    impl_->reserve(impl_->size() + res_arg);
87  }
88
89  virtual size_t size() const {
90    return impl_->size();
91  }
92
93 protected:
94  StringClass* impl_;
95
96 private:
97  // Making these private avoids implicit copy constructor & assignment operator
98  OutputString(const OutputString&);
99  void operator=(const OutputString&);
100};
101
102// Don't allow the OutputString template to be based upon a pointer to
103// OutputStringInterface.  Enforce this restriction by defining this class to
104// lack any functions expected of an OutputString.
105template<> class OutputString<OutputStringInterface> { };
106
107}  // namespace open_vcdiff
108
109#endif  // OPEN_VCDIFF_OUTPUT_STRING_H_
110