Mybowlcut
March 6th, 2008, 08:55 AM
Hey. I've got a class called Screen that is an abstraction of graphical elements (that can be drawn to the screen). The class calls this function in it's constructor:
void Screen::Load_Elements(const std::string& directory)
{
std::ifstream image_file;
IO::open_read_file(image_file, directory + IMAGES_FN);
IO::read_data_clone<XImage, std::vector<Base_XImage*> >(elements, image_file);
image_file.close();
std::ifstream button_file;
IO::open_read_file(button_file, directory + BUTTONS_FN);
IO::read_data_clone<XButton, std::vector<Base_XImage*> >(elements, button_file);
button_file.close();
std::ifstream audible_button_file;
IO::open_read_file(audible_button_file, directory + AUDIBLE_BUTTONS_FN);
IO::read_data_clone<Audible_XButton, std::vector<Base_XImage*> >(elements, audible_button_file);
audible_button_file.close();
std::ifstream draggable_button_file;
IO::open_read_file(draggable_button_file, directory + DRAGGABLE_BUTTONS_FN);
IO::read_data_clone<Draggable_XButton, std::vector<Base_XImage*> >(elements, draggable_button_file);
draggable_button_file.close();
std::ifstream dialog_box_file;
IO::open_read_file(dialog_box_file, directory + DIALOG_BOXES_FN);
IO::read_data_clone<XDialog_Box, std::vector<Base_XImage*> >(elements, dialog_box_file);
dialog_box_file.close();
std::ifstream input_box_file;
IO::open_read_file(input_box_file, directory + INPUT_BOXES_FN);
IO::read_data_clone<XInput_Box, std::vector<Base_XImage*> >(elements, input_box_file);
input_box_file.close();
}
This is the member that stores all the elements.
std::vector<Base_XImage*> elements;
The obvious problem here is that the Load_Elements function is huge... and will only get huger each time I create a new class like Slider or something. Also, I have a folder structure that looks like this:
<screen_name>
images.txt buttons.txt etc....
<name x y> <name x y> <...>
Where screen_name is a folder and images.txt and buttons.txt are text files and <name x y> is each individual respective element to load. Doing it this way means that each time I add a new class (e.g Slider), I have to add a corresponding text file in every "Screen" folder to every project that uses Screen, even if it's empty, otherwise open_read_file throws an exception to tell me the file wasn't found! I could fix this particular issue by putting try/catch around each paragraph of code in Load_Elements, but that is just dodgy...
I thought about something like this:template<typename T>
void Load_Elements_Of_Type(std::vector<Base_XImage*>& elements,
const std::string& directory, const std::string& file_name)
{
try
{
std::ifstream element_file;
open_read_file(element_file, directory + file_name);
read_data_clone<T, std::vector<Base_XImage*> >(elements, element_file);
element_file.close();
}
catch(...)
{ // Don't want to load this type.
}
}
// Inside Load_Elements:
Load_Elements_Of_Type<XButton>(elements, "directory/", "file.txt");
Load_Elements_Of_Type<XSlider>(elements, "directory/", "file.txt");
// Load every possible type.
But I don't know what happens when T doesn't derive from Base_XImage... and it's still gonna be non-scalable.
I really need to sort these problems out now before they get worse... Any suggestions?
Cheers. :thumb:
void Screen::Load_Elements(const std::string& directory)
{
std::ifstream image_file;
IO::open_read_file(image_file, directory + IMAGES_FN);
IO::read_data_clone<XImage, std::vector<Base_XImage*> >(elements, image_file);
image_file.close();
std::ifstream button_file;
IO::open_read_file(button_file, directory + BUTTONS_FN);
IO::read_data_clone<XButton, std::vector<Base_XImage*> >(elements, button_file);
button_file.close();
std::ifstream audible_button_file;
IO::open_read_file(audible_button_file, directory + AUDIBLE_BUTTONS_FN);
IO::read_data_clone<Audible_XButton, std::vector<Base_XImage*> >(elements, audible_button_file);
audible_button_file.close();
std::ifstream draggable_button_file;
IO::open_read_file(draggable_button_file, directory + DRAGGABLE_BUTTONS_FN);
IO::read_data_clone<Draggable_XButton, std::vector<Base_XImage*> >(elements, draggable_button_file);
draggable_button_file.close();
std::ifstream dialog_box_file;
IO::open_read_file(dialog_box_file, directory + DIALOG_BOXES_FN);
IO::read_data_clone<XDialog_Box, std::vector<Base_XImage*> >(elements, dialog_box_file);
dialog_box_file.close();
std::ifstream input_box_file;
IO::open_read_file(input_box_file, directory + INPUT_BOXES_FN);
IO::read_data_clone<XInput_Box, std::vector<Base_XImage*> >(elements, input_box_file);
input_box_file.close();
}
This is the member that stores all the elements.
std::vector<Base_XImage*> elements;
The obvious problem here is that the Load_Elements function is huge... and will only get huger each time I create a new class like Slider or something. Also, I have a folder structure that looks like this:
<screen_name>
images.txt buttons.txt etc....
<name x y> <name x y> <...>
Where screen_name is a folder and images.txt and buttons.txt are text files and <name x y> is each individual respective element to load. Doing it this way means that each time I add a new class (e.g Slider), I have to add a corresponding text file in every "Screen" folder to every project that uses Screen, even if it's empty, otherwise open_read_file throws an exception to tell me the file wasn't found! I could fix this particular issue by putting try/catch around each paragraph of code in Load_Elements, but that is just dodgy...
I thought about something like this:template<typename T>
void Load_Elements_Of_Type(std::vector<Base_XImage*>& elements,
const std::string& directory, const std::string& file_name)
{
try
{
std::ifstream element_file;
open_read_file(element_file, directory + file_name);
read_data_clone<T, std::vector<Base_XImage*> >(elements, element_file);
element_file.close();
}
catch(...)
{ // Don't want to load this type.
}
}
// Inside Load_Elements:
Load_Elements_Of_Type<XButton>(elements, "directory/", "file.txt");
Load_Elements_Of_Type<XSlider>(elements, "directory/", "file.txt");
// Load every possible type.
But I don't know what happens when T doesn't derive from Base_XImage... and it's still gonna be non-scalable.
I really need to sort these problems out now before they get worse... Any suggestions?
Cheers. :thumb: