FMpiBufferReader.hpp 4.36 KB
Newer Older
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
1
// ===================================================================================
2 3 4 5
// Copyright ScalFmm 2016 INRIA, Olivier Coulaud, Bérenger Bramas,
// Matthias Messner olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// This software is a computer program whose purpose is to compute the
// FMM.
6
//
7
// This software is governed by the CeCILL-C and LGPL licenses and
8
// abiding by the rules of distribution of free software.
9 10 11
// An extension to the license is given to allow static linking of scalfmm
// inside a proprietary application (no matter its license).
// See the main license file for more details.
12
//
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
13 14 15
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 17 18
// GNU General Public and CeCILL-C Licenses for more details.
// "http://www.cecill.info".
// "http://www.gnu.org/licenses".
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
19 20 21 22
// ===================================================================================
#ifndef FMPIBUFFERREADER_HPP
#define FMPIBUFFERREADER_HPP

23
#include <memory>
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
24
#include "../Utils/FMpi.hpp"
25
#include "FAbstractBuffer.hpp"
26
#include "../Utils/FAssert.hpp"
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
27

28 29
/** @author Cyrille Piacibello, Berenger Bramas
 * This class provide the same features as FBufferWriter
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
30 31 32 33 34
 *
 * Put some data
 * then insert back if needed
 * finally use data pointer as you like
 */
35
class FMpiBufferReader : public FAbstractBufferReader {
36
    FSize arrayCapacity;        //< Allocated space
BRAMAS Berenger's avatar
BRAMAS Berenger committed
37
    std::unique_ptr<char[]> array;  //< Allocated Array
38
    FSize currentIndex;
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
39 40

public :
BRAMAS Berenger's avatar
BRAMAS Berenger committed
41
    /*Constructor with a default arrayCapacity of 512 bytes */
42
    explicit FMpiBufferReader(const FSize inDefaultCapacity = 512):
BRAMAS Berenger's avatar
BRAMAS Berenger committed
43 44
        arrayCapacity(inDefaultCapacity),
        array(new char[inDefaultCapacity]),
45 46 47
        currentIndex(0){
        FAssertLF(array, "Cannot allocate array");
    }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
48

BRAMAS Berenger's avatar
BRAMAS Berenger committed
49
    /** To change the capacity (but reset the head to 0) */
50
    void cleanAndResize(const FSize newCapacity){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
51 52 53 54 55 56 57
        if(newCapacity != arrayCapacity){
            arrayCapacity = newCapacity;
            array.reset(new char[newCapacity]);
        }
        currentIndex = 0;
    }

BRAMAS Berenger's avatar
BRAMAS Berenger committed
58
    /** Destructor
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
59
   */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
60 61 62 63
    virtual ~FMpiBufferReader(){
    }

    /** Get allocated memory pointer */
64
    char* data() override {
BRAMAS Berenger's avatar
BRAMAS Berenger committed
65 66 67 68
        return array.get();
    }

    /** Get allocated memory pointer */
69
    const char* data() const override  {
BRAMAS Berenger's avatar
BRAMAS Berenger committed
70 71 72 73
        return array.get();
    }

    /** get the filled space */
74
    FSize getSize() const override {
BRAMAS Berenger's avatar
BRAMAS Berenger committed
75 76 77 78
        return currentIndex;
    }

    /** Size of the memory initialized */
79
    FSize getCapacity() const{
BRAMAS Berenger's avatar
BRAMAS Berenger committed
80 81 82 83
        return arrayCapacity;
    }

    /** Move the read index to a position */
84 85
    void seek(const FSize inIndex) override {
        FAssertLF(inIndex <= arrayCapacity, "FMpiBufferReader :: Aborting :: Can't move index because buffer isn't long enough ",inIndex," ",arrayCapacity);
86
        currentIndex = inIndex;
87
    }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
88 89

    /** Get the read position */
90
    FSize tell() const override  {
BRAMAS Berenger's avatar
BRAMAS Berenger committed
91 92 93 94 95 96
        return currentIndex;
    }

    /** Get a value with memory cast */
    template <class ClassType>
    ClassType getValue(){
97
        FAssertLF(currentIndex + FSize(sizeof(ClassType)) <= arrayCapacity );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
98
        ClassType value;
99 100
        memcpy(&value, &array[currentIndex], sizeof(ClassType));
        currentIndex += sizeof(ClassType);
BRAMAS Berenger's avatar
BRAMAS Berenger committed
101 102 103 104 105
        return value;
    }

    /** Get a value with memory cast at a specified index */
    template <class ClassType>
106
    ClassType getValue(const FSize ind){
107 108
        currentIndex = ind;
        return getValue<ClassType>();
BRAMAS Berenger's avatar
BRAMAS Berenger committed
109 110 111 112 113
    }

    /** Fill a value with memory cast */
    template <class ClassType>
    void fillValue(ClassType* const inValue){
114 115 116
        FAssertLF(currentIndex + FSize(sizeof(ClassType)) <= arrayCapacity );
        memcpy(inValue, &array[currentIndex], sizeof(ClassType));
        currentIndex += sizeof(ClassType);
BRAMAS Berenger's avatar
BRAMAS Berenger committed
117 118 119 120
    }

    /** Fill one/many value(s) with memcpy */
    template <class ClassType>
121
    void fillArray(ClassType* const inArray, const FSize inSize){
122 123 124
        FAssertLF(currentIndex + FSize(sizeof(ClassType))*inSize <= arrayCapacity );
        memcpy(inArray, &array[currentIndex], sizeof(ClassType)*inSize);
        currentIndex += sizeof(ClassType)*inSize;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
125 126 127 128 129 130 131
    }

    /** Same as fillValue */
    template <class ClassType>
    FMpiBufferReader& operator>>(ClassType& object){
        fillValue(&object);
        return *this;
132
    }
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
133 134 135 136

};
#endif