c++ - Deriving from templatized base class -
i've buil small database of different kinds type of objects. idea have base templatized class, let's call database , let derive other classes that.
for example:
template< typename record, size_t record_size, char record_sep = '!', char record_param_sep = ',', char record_field_sep = '~', size_t max_records_per_query = 5000, size_t min_record_count = 15000 > class database { public: typedef record record_t; typedef std::vector< unsigned char > querybuffer; database( const std::string& basepath, const std::string& recordpath, const std::string tablefilename ); enum class queryresult { ok, no_data, overflow, future_date, future_range, error }; void add( void add( const record_t& r ) { lock lock( mmutex ); // ... lots of stuff here } queryresult query(querybuffer &qb, time_t first, time_t last ) { lock lock( mmutex ); // ... lots of stuff here } queryresult query(querybuffer &qb, time_t first ) { lock lock( mmutex ); // ... lots of stuff here } // protected , private data };
then derive class in way
class gamedatabase : public database< gametype, 9 > { private: using mybase database< gametype, 9 >; public: gamedatabase( const std::string& basepath ) : mybase{ basepath, "g/", "g.dat" } { } // query method queryresult query(querybuffer &qb, size_t first, size_t last ) { lock lock( mmutex ); // use protected methods of mybase // .... lots of stuff here } };
is idea or better solution? problem definition of database must inlined, , long code.
it's not unusual idiom, ordinarily template parameters either types, or integers need compile-time. hoping gain compiling sizes , delimiters in that?
i suggest make them members of database, initialized in constructor. if want inline everything, go right ahead. don't make things template parameters when need them const.
Comments
Post a Comment