single_dict_content.h revision c0c674cdc0721a374e140ad5ee1409c0498b3262
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#include "utils/byte_array_view.h"
28
29namespace latinime {
30
31class SingleDictContent {
32 public:
33    SingleDictContent(uint8_t *const buffer, const int bufferSize)
34            : mExpandableContentBuffer(ReadWriteByteArrayView(buffer, bufferSize),
35                      BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE) {}
36
37    SingleDictContent()
38            : mExpandableContentBuffer(Ver4DictConstants::MAX_DICTIONARY_SIZE) {}
39
40    virtual ~SingleDictContent() {}
41
42    bool isNearSizeLimit() const {
43        return mExpandableContentBuffer.isNearSizeLimit();
44    }
45
46 protected:
47    BufferWithExtendableBuffer *getWritableBuffer() {
48        return &mExpandableContentBuffer;
49    }
50
51    const BufferWithExtendableBuffer *getBuffer() const {
52        return &mExpandableContentBuffer;
53    }
54
55    bool flush(FILE *const file) const {
56        return DictFileWritingUtils::writeBufferToFileTail(file, &mExpandableContentBuffer);
57    }
58
59 private:
60    DISALLOW_COPY_AND_ASSIGN(SingleDictContent);
61
62    BufferWithExtendableBuffer mExpandableContentBuffer;
63};
64} // namespace latinime
65#endif /* LATINIME_SINGLE_DICT_CONTENT_H */
66