FromFile.h 1.15 KB
Newer Older
adelmann's avatar
adelmann committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#ifndef __FROMFILE_H__
#define __FROMFILE_H__

#include <string>
#include <map>
#include <set>
#include <fstream>
#include <iterator>

#include "boost/tuple/tuple.hpp"
#include "boost/variant/get.hpp"
#include "boost/variant/variant.hpp"
#include "boost/smart_ptr.hpp"

#include "Util/Types.h"
#include "Util/OptPilotException.h"
#include "Expression/Parser/function.hpp"


/**
 *  Simple functor that reads vector data from a file. If the file contains
 *  more than one value the sum is returned.
 *  \f[
 *    result = \sum_{i=0}^n value_i
 *  \f]
 */
struct FromFile {

    static const std::string name;

    Expressions::Result_t operator()(client::function::arguments_t args) {

        filename_   = boost::get<std::string>(args[0]);

        double sum = 0;
        bool is_valid = true;

        try {
            readValues();
        } catch(OptPilotException e) {
            return boost::make_tuple(0.0, false);
        }

44
        for(double obj_value : values_)
adelmann's avatar
adelmann committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
            sum += obj_value;

        return boost::make_tuple(sum, is_valid);
    }


private:

    std::vector<double> values_;

    std::string filename_;

    void readValues();

};

#endif