single_dict_content.h revision 90b7c1729f6f9fd6bf67e2ca55478c5a4be4100d
1/*
2 * Copyright (C) 2013, The Android Open Source Project
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
17#ifndef LATINIME_SINGLE_DICT_CONTENT_H
18#define LATINIME_SINGLE_DICT_CONTENT_H
19
20#include <cstdint>
21#include <cstdio>
22
23#include "defines.h"
24#include "suggest/policyimpl/dictionary/structure/v4/ver4_dict_constants.h"
25#include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h"
26#include "suggest/policyimpl/dictionary/utils/dict_file_writing_utils.h"
27
28namespace latinime {
29
30class SingleDictContent {
31 public:
32    SingleDictContent(uint8_t *const buffer, const int bufferSize)
33            : mExpandableContentBuffer(buffer, bufferSize,
34                      BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE) {}
35
36    SingleDictContent()
37            : mExpandableContentBuffer(Ver4DictConstants::MAX_DICTIONARY_SIZE) {}
38
39    virtual ~SingleDictContent() {}
40
41    bool isNearSizeLimit() const {
42        return mExpandableContentBuffer.isNearSizeLimit();
43    }
44
45 protected:
46    BufferWithExtendableBuffer *getWritableBuffer() {
47        return &mExpandableContentBuffer;
48    }
49
50    const BufferWithExtendableBuffer *getBuffer() const {
51        return &mExpandableContentBuffer;
52    }
53
54    bool flush(FILE *const file) const {
55        return DictFileWritingUtils::writeBufferToFileTail(file, &mExpandableContentBuffer);
56    }
57
58 private:
59    DISALLOW_COPY_AND_ASSIGN(SingleDictContent);
60
61    BufferWithExtendableBuffer mExpandableContentBuffer;
62};
63} // namespace latinime
64#endif /* LATINIME_SINGLE_DICT_CONTENT_H */
65