Ok, I'm trying to write a little app to be used by myself and my collegues when teaching technical training courses. The purpose of the app is to auto-mark lab exercises.
I've posted elsewhere some other questions, but this one involves the storeage of mark data.
I have an XML file which contains the marking scheme for a particular lab. In it are specifications of what registry / file settings are needed on a remote machine in order to get a particular mark / set of marks.
The interface is to be a notebook with a page for each student. The idea being that when a student says they have finished, the appropriate page is brought up and a "mark" button pressed. This will kick off the process of scanning the machine.
So far so good. Now, I'm using wxWidgets, so I thought that I could use a wxArray to hold the information on the machines. Each element being an object representing the marking for hat machine.
The question is, how do I create those objects? I can't design the object until the XML file is read in for the particular lab exercise. This will then define both the data storage requirements and also the dislay requirements for thepages in the wxNotebook.
Can anyone offer any advice on how to deal with objects whose format you don't know until runtime?
Put some kind of identifier or tag in the XML that tells you what class to instantiate, which you can then do dynamically with an abstract factory.
For the XML portion you could download TinyXML from sourceforge. Google it and you will get to the page quickly.
TinyXML is not the most robust XML util out there but it is very simple to use and you will have your files loaded in no time flat. It also comes with some tutorials and doxygen docs which ease the pain a bit.
If you are really adventurous you might try Xerces XML which is extremely powerful but also extremely difficult to get setup and working. It's auto generated code is some of the ugliest C++ code I've ever had to read but thankfully you don't need to edit these files.
You must know something about the format of the XML to read it in with TinyXML and Xerces. The schema is a must have. Xerces may be what you need because it can serialize and de-serialize class objects directly to/from XML. It creates classes for you and is a great tool but the learning curve to become proficient at it is at least a couple of days if not more.
Thanks, guys, for your responses.
I think you've both missed the thrust of my question. I have the XML side of things sorted. What I need to know is how to structure my data once I have read it in. The XML file might look something like:-
In this case, I have a manual entry mark for the first paper which has an 80% passmark and a maximum total of 63. Paper 2 has a maximum mark of 123, again a pass mark of 80% but in this case the student starts with 100% and looses marks if things aren't met. The only mark (so far) is a functionality test, worth 35 marks and will require just an on/off tick-box. Paper three is again just tick-boxes, 3 off. 2 out of 3 required.
<title>Test Exam Keyfile</title>
<paper type="Written" totalmark="63" passmark="80" method="cumulative">
<paper type="Lab" totalmark="123" passmark="80" method="deduct">
<manualselect value="35">Basic Functionality Test</manualselect>
<paper type="troubleshooting" totalmark="3" passmark="65" method="cumulative">
<manualselect value="1">Issue 1</manualselect>
<manualselect value="1">Issue 2</manualselect>
<manualselect value="1">Issue 3</manualselect>
So, as I read this in, I'm goung to need to create an array of objects which contain a couple of integer properties and a few boolean. If the exam has marks for different things, then the objects created will need different properties.
Does that make sense?
Yes. Now do what medievalelks said.
But that's the point. As I write the application, there is an infinite number of possible classes, depending on the lab. I don't want to hard-code this into the application, as this would make the XML file pointless. The file structure essentially defines the format of the object, in terms of what properties it will need. Using my example, I might have a second lab which has 4 bools required for paper 3. A third which might have 6 in paper 2 and so on. I can't say what the object will look like up-front in order to embed a tag to tell the app which to instantiate.
I want to design the data structure at runtime, based on the XML file.
maybe something like this: (pardon my pseudocode)
or if you have a fixed number of data types, you could just have a vector for each kind in the labexam class.
template<typename T> class labProperty : public property
maybe i'm still missing the point...i don't see what's so tricky here...
I don't see it either. Anything you can do in that XML, you can model using classes and vectors or maps thereof.
Perhaps I haven't fully explained myself. Whilst I'm not new to programming, C++ is a new area for me, as is Windows programming, so I'm learning as I go along. As such I may well not be able to coach my questions appropriately. "Vectors or maps thereof"??? A link to a damned good howto / tutorial would be good!!!
I guess I'm looking for how to impliment a class with methods that allow me to add properties (perhaps with some sort of internal array) but for that class and it's property set to be duplicated into an array.
If you can't google "C++ vectors" we're in more trouble than we thought. And aren't there tutorials on the front page of this very site? (Hint: yes.)
But anyway, vectors are arrays, except we don't need to know how many objects ahead of time (it can grow automatically as we add to it). But, like arrays, everything we add needs to be the same type (hence making a generic type of "property" or whatever).
Ok, I think I get what you guys are saying. Correct me if I'm wrong, though, wouldn't the result of this be to add the various properties (vectors or whatever) to a particular instance of a class? What I want to do is to add the properties to the class and then instantiate that class (with all of it's properties) several times into an array (once for each student).
I can understand why people are saying it's easy if what you want to do is add properties to an instance, but to the actual class?
That's not possible. To the best of my knowledge, there isn't a single language in existence that allows modification of real classes after they have been loaded.
Furthermore, how would you access those fields?
The same way I would access them if I had added them to an instance of an object...
Humm. I suspect I'm going to have to instanciate a class, add the properties and then come up with a copy method so I can copy that object multiple times. Messey, but doable.
Thank you to everyone for looking at this for me and especially to those of you who have replied :-)
Hey, were you listening?
Originally Posted by wierdbeard65
It's not possible to add properties after an object is instantiated.
You could emulate such a thing with a index operator , though, that takes a string or integer.
Like a map.
Well, instead of adding new member variables to an instance of a class, have the object hold a collection of property objects. Then when you need to make a change, add new property objects to the object's collection.