Yujie
Yujie

Reputation: 445

In this case, how do I pass a function as a parameter to the function?

I have searched how to pass a function as a parameter. But now I still have a little problem. Look at the following code, there is an internal class Entry, which has a template T represented by a parameter. The log function inside is used to print debugging information. The Table class inside can draw a table. Among them set_row(row_number,vector<string> data_vector) is to set the row_number row of the table to the data in data_vector.

template <class T> class SetAssociativeCache {
  public:
    class Entry {
      public:
        uint64_t key;
        uint64_t index;
        uint64_t tag;
        bool valid;
        T data;
    };
  string log(vector<string> headers, function<void(Entry &, Table &, int)> write_data) {
        vector<Entry> valid_entries = this->get_valid_entries(); 
        Table table(headers.size(), valid_entries.size() + 1); // The parameters are the width and height of the table
        table.set_row(0, headers);
        for (unsigned i = 0; i < valid_entries.size(); i += 1)
            write_data(valid_entries[i], table, i + 1);
        return table.to_string();
    }
}

How should I pass parameters when calling the log function?

The template T type may be:

class FilterTableData {
  public:
    uint64_t pc;
    int offset;
};

or

class AccumulationTableData {
  public:
    uint64_t pc;
    int offset;
    vector<bool> pattern;
};

or

class PatternHistoryTableData {
  public:
    vector<bool> pattern;
};

If I write a function outside the class to pass parameters to the writedata of the log function, the Entry type will show that there is no such type (because it is inside the class). If written internally, the template T may be of a different type. What should I do? Thank you very much!

Here I provide some information about Table. There are some functions in him.

variable:
    unsigned width;
    unsigned height;
    vector<vector<string>> cells;
function:
   Table(int width, int height) : width(width), height(height), cells(height, vector<string>(width)) {}
    void set_cell(int row, int col, string data)
    void set_row(int row, const vector<string> &data, int start_col = 0)
    void set_col(int col, const vector<string> &data, int start_row = 0)
    string to_string()

toString is to draw it out.

All the code is here. https://github.com/Yujie-Cui/Bingo/blob/master/prefetcher/bingo_01k.llc_pref

Upvotes: 0

Views: 47

Answers (1)

Caleth
Caleth

Reputation: 62531

You can write functions like

void write_filter_table_data(SetAssociativeCache<FilterTableData>::Entry &, Table&, int) { ... }

Or lambda expressions like

auto write_accumulation_table_data = [](SetAssociativeCache<AccumulationTableData>::Entry &, Table&, int) { ... }

Or you can write a template that delegates to another function

template <typename T>
auto make_data_writer(std::function<void(T&)> writer) {
    return [writer](SetAssociativeCache<T>::Entry &, Table&, int) { ... }
}

Upvotes: 1

Related Questions