0

(mostly pasted from accessing variable from another class template to separate two problems)

i am trying to make a system of container classes that can be used with a data loader class to load data from text files

here are the two classes of data:

class Customer
{
    //...
};

class Tour
{
    std::vector<Customer*> custPtrs;
    //...
};

these are my two container classes:

template <class T>
class P_VContainer
{
    boost::ptr_vector<T> data;
    //...
};

template <class T>
class ListContainer
{
    std::list<T> data;
    //...
};

and finally my data loader template:

template<template<class> class T>
class DataLoader
{
    T<Customer> custList;
    T<Tour> tourList;

    //...
};

i have overloaded the >> operator in Customer and Tour so that an ifstream can be passed to them, a line is taken from the stream, tokenised and put it into the object instance variables.

the container classes handle the insertion in order and the data loader manages the lists and creates the ifstream so that it can be passed to the objects.

this is my problem:

i am loading my customers file in first, and populating that list.

after that i have to load in the tours, which have customerIDs for the customers that booked them, and i want to store those customers in a vector of pointers in each of the tour objects so that the customer information is easily accessible.

at the moment i am storing the customerIDs as a list of strings, then when the tours are all loaded, passing custList into a function that searches through custList, matching it with the list of strings

this means i am having to maintain two lists, one of strings and the other pointers, and basically double handle all the data.. considering the datasets are quite large, this means a lot longer in loading time..

so i was wondering if there is a way that i can access the custList instance variable from inside the overloaded >> operator for Tour and generate the list of pointers as i create the Tour objects?

technically everything is occurring inside the scope of the DataLoader class, so i think it should be possible, but im just not too sure how to go about it.. maybe make it a friend class? i have tried doing that but havent had any luck so far..

any help would be greatly appreciated, and sorry for the long winded explanation, hopefully it made sense..

Community
  • 1
  • 1
guskenny83
  • 1,321
  • 14
  • 27
  • How are customers related to a tour? it's important to know to understand the relationship between those 2 data types. Logically I'd assume customer type owns list of tours. – Iuri Covalisin Oct 19 '13 at 19:39
  • neither inherit from eachother and it is the other way around, that Tour holds a list of customers, this is why i need to access the already loaded customer list while creating the Tour objects.. i will update the code to reflect the relationship.. – guskenny83 Oct 19 '13 at 20:11
  • "at the moment i am storing the customerIDs as a list of strings, then when the tours are all loaded, passing custList into a function that searches through custList, matching it with the list of strings" I think `boost::serialize` can handle this scenario internally. Anyway, why do you need a list of pointers in `Tour`, when a list of customer IDs should work as well? If you really need pointers, you can supply the customer list via the left side of `>>`, i.e. `mysource >> someTour` and use a custom type for `mysource`. – dyp Oct 19 '13 at 21:12

1 Answers1

1

The final stream usage can look like this:

custStream >> customers >> toursStream >> tours;

To achieve this, you must wrap ifstream with two streams - for customers and for tours and keep the customer list in the stream, here's the code, you can replace the container to your favorite:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

class CustomersInFileStream {
public:
    std::vector<Customer> customers;
    std::ifstream &input;
    CustomersInFileStream(std::ifstream &fileIn)
        :input(fileIn){
    }
};

class ToursInFileStream {
public:
    std::vector<Customer> customers;
    std::ifstream &input;
    ToursInFileStream(std::ifstream &fileIn)
        :input(fileIn){
    }
};

CustomersInFileStream &operator>>(CustomersInFileStream &input, std::vector<Customer> customers) {
    // perform file parsing here using ifstream member
    input.customers = customers;
    return input;
}

ToursInFileStream &operator>>(CustomersInFileStream &customersStream,
                                  ToursInFileStream &toursStream) {
    toursStream.customers = customersStream.customers;
    return toursStream;
}

ToursInFileStream &operator>>(ToursInFileStream &input, std::vector<Tour> tours) {
    // perform file parsing here using ifstream member
    // you also do have customers list here
    return input;
}

int main()
{

    std::ifstream customersFile("~/customers.txt");
    std::ifstream toursFile("~/tours.txt");

    CustomersInFileStream custStream(customersFile);
    ToursInFileStream toursStream(toursFile);

    std::vector<Customer> customers;
    std::vector<Tour> tours;

    custStream >> customers >> toursStream >> tours;

    return 0;
}
Iuri Covalisin
  • 645
  • 4
  • 7
  • I'd rather suggest to create the custom stream with the customer list as a parameter to the ctor than using `operator>>` as it's not a stream extraction; but other than that, +1, I also wanted to suggest some form of this in my comment to the OP. – dyp Oct 19 '13 at 22:05
  • @DyP custStream >> customers returns CustomersInFileStream that is input parameter for >> tours. The test also runs smooth, where do you expect it to fail? – Iuri Covalisin Oct 19 '13 at 22:15
  • all operators were there, but usage missed tours stream, fixed now. – Iuri Covalisin Oct 19 '13 at 22:44
  • Alright, that's what I meant with "conversion". The implicit `custStream >> toursStream` copies data (which is, btw, unnecessary), so I wouldn't write it like a stream extraction. I thought of something like `ToursInFileStream toursStream(toursFile, customers); toursStream >> tours;`, where `toursStream` stores a reference to the customer list. – dyp Oct 19 '13 at 23:28