0

I am trying to build a custom ORM. Here is a basic code for the same.

class modelField
{
        public:
                modelField(int size) {__size__=size;};
                modelField() {};
                virtual ~modelField() {};
                virtual std::string __type__() = 0;
                virtual void set(std::string) = 0;
                virtual void set(int) = 0;
                int __size__ = -1;
};


class charField : public modelField
{
        public:
                charField(int value) : modelField(value) {};
                charField() : modelField() {};
                virtual void set(std::string val) {__value__ = val;};
                virtual void set(int) {};
                std::string __type__();
        private:
                int size;
                std::string __value__;
};

Now charField in this is just a column type, i have more ranging from int to double. Currently, i made two virtual methods set and overloaded them to accept diffrent data types. But this doesn't seem to be the best way to go. How can i make a generic function that can accept string/int/double or other values passed to the same. There probably is some solution in template although i am not able to think of any. Another thing that i should mention is that modelField is only base there can be other fields like lets say IPv4 addr which is a child of charField. Thanks in advance for any suggestions.

xander cage
  • 219
  • 1
  • 4
  • 11
  • [Don't use double underscore prefixes](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier), and you don't need semicolons after a compound expression (`{};`), vs struct/class declarations (http://stackoverflow.com/questions/19164757/why-does-c-have-a-after-a-class-declaration). Popular alternatives to the __ prefix is either to use an `m_` prefix or to use a `_` suffix: `m_size`, `size_`. – kfsone Jul 09 '16 at 06:32
  • @kfsone thanks about that will make changes for the same in my next commit :) any suggestions about how to set value ? – xander cage Jul 09 '16 at 06:34
  • Your `modelField` class has a `virtual void set(std::string)` method. How would it be used in your code? I know you override it in a child class, I'm asking why it doesn't *start* in that child class. – n. m. could be an AI Jul 09 '16 at 07:04
  • @n.m. yes thats also an option. The following represents the code that i have written as of now. What i am not sure is the proper way i can set the value. I am open to any ideas regarding the same. – xander cage Jul 09 '16 at 07:49
  • It's rather unclear how to apply this whole idea to an ORM. Suppose you have defined all of your field classes, doesn't matter right now how exactly you do that. Now what? Do you create an array of pointers to `modelField` and then downcast each time you want to use one? – n. m. could be an AI Jul 09 '16 at 08:35
  • @n.m. there is a `model` class that contains a map to modelField. Every class extends models (tables not the fields that i mentioned above). `model` class has a method save() in which i iterate over the map. map in model is like std::map m_fields; where string is column name. – xander cage Jul 09 '16 at 08:41
  • @n.m. anyways i found [this](http://stackoverflow.com/questions/3559412/how-to-store-different-data-types-in-one-list-c) which is pretty much what i wanted in first place :) – xander cage Jul 09 '16 at 08:42
  • OK so all of your objects inherit `model` and `model` has a field map. Great. How will `model` or its child classes use that map? `mymap[fieldName].whatnow?()` Suppose you want to set a string field, wouldn't you have to `dynamic_cast(mymap[fieldName])`? – n. m. could be an AI Jul 09 '16 at 08:49

0 Answers0