FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
C++
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
June 01, 2006

Supporting Custom C++ Types

(Page 2 of 6)

SOCI Implementation

SOCI supports native types such as int and std::string as parameters for use() and into() functions via template specialization. The framework internally makes use of an inheritance hierarchy, which is by design hidden from users.

The use() function in fact creates an instance of the class template UseType, which is specialized for each supported native type. The class UseType is derived from StandardUseType, which in turn is derived from the abstract base class UseTypeBase.

The into() function works similarly, creating instances of IntoType, which is also specialized for each built-in type. Class IntoType is derived from StandardIntoType, which in turn is derived from the abstract base class IntoTypeBase. Figure 1 presents the relevant UML class diagrams, and Listing Two implements the simplified class definitions.

Figure 1: SOCI internal class hierarchy.

 
class UseTypeBase
{
public:
    virtual void bind(Statement &st, int &position) = 0;
    virtual void preUse() = 0;
    virtual void postUse(bool gotData) = 0;
    virtual void cleanUp() = 0;
};

class StandardUseType : public UseTypeBase { public: StandardUseType(void *data, eExchangeType type, std::string const &name = std::string());

~StandardUseType(); virtual void bind(Statement &st, int &position);

private: virtual void preUse(); virtual void postUse(bool gotData); virtual void cleanUp();

void *data_; eExchangeType type_; eIndicator *ind_; std::string name_; StandardUseTypeBackEnd *backEnd_; //database specific };

template <> class UseType<int> : public StandardUseType { public: UseType(int &i, std::string const &name = std::string()) : StandardUseType(&i, eXInteger, name) {} };

template <> class UseType<std::string> : public StandardUseType { public: UseType(std::string &s, std::string const &name = std::string()) : StandardUseType(&s, eXStdString, name) {} };

template <typename T> details::UseTypePtr use(T &t) { return UseTypePtr(new UseType<T>(t)); }

// similar definitions for UseType<double> etc. not shown

class IntoTypeBase { public: virtual void define(Statement &st, int &position) = 0; virtual void preFetch() = 0; virtual void postFetch(bool gotData, bool calledFromFetch) = 0; virtual void cleanUp() = 0; };

class StandardIntoType : public IntoTypeBase { public: StandardIntoType(void *data, eExchangeType type); virtual ~StandardIntoType();

private: virtual void define(Statement &st, int &position); virtual void preFetch(); virtual void postFetch(bool gotData, bool calledFromFetch); virtual void cleanUp();

void *data_; eExchangeType type_; StandardIntoTypeBackEnd *backEnd_; };

template <> class IntoType<int> : public StandardIntoType { public: IntoType(int &i) : StandardIntoType(&i, eXInteger) {} };

template <> class IntoType<std::string> : public StandardIntoType { public: IntoType(std::string &s) : StandardIntoType(&s, eXStdString) {} };

template <> class IntoType<std::tm> : public StandardIntoType { public: IntoType(std::tm &t) : StandardIntoType(&t, eXStdTm) {} };

template <typename T> IntoTypePtr into(T &t) { return IntoTypePtr(new IntoType<T>(t)); }

// similar definitions for IntoType<double> etc. not shown

Listing Two

Previous Page | 1 Supporting Custom C++ Types | 2 SOCI Implementation | 3 The Problem: Supporting Additional Types | 4 Traits & TypeConversion<T> | 5 Pass-Through Version of TypeConversion | 6 Aggregate Types and Object-Relational Mapping Next Page
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK