Читаем Thinking In C++. Volume 2: Practical Programming полностью

<p>Policies</p>

If you inspect the char_traits specialization for wchar_t, you’ll see that it is practically identical to its char counterpart:

template<>

  struct char_traits {

  typedef wchar_t char_type;

  typedef wint_t int_type;

  typedef streamoff off_type;

  typedef wstreampos pos_type;

  typedef mbstate_t state_type;

  static void assign(char_type& c1, const char_type& c2);

  static bool eq(const char_type& c1, const char_type& c2);

  static bool lt(const char_type& c1, const char_type& c2);

  static int compare(const char_type* s1,

      const char_type*  s2, size_t n);

  static size_t length(const char_type* s);

  static const char_type* find(const char_type* s,

                size_t n,

                const char_type& a);

  static char_type* move(char_type* s1,

          const char_type* s2, size_t n);

  static char_type* copy(char_type* s1,

          const char_type* s2, size_t n);

  static char_type* assign(char_type* s, size_t n,

            char_type a);

  static int_type not_eof(const int_type& c);

  static char_type to_char_type(const int_type& c);

  static int_type to_int_type(const char_type& c);

  static bool eq_int_type(const int_type& c1,

           const int_type& c2);

  static int_type eof();

};

The only real difference between the two versions is the set of types involved (char and int vs. wchar_t and wint_t). The functionality provided is the same.[63] This highlights the fact that traits classes are indeed for traits, and therefore the things that change between related traits classes are usually types and constant values, or fixed algorithms that use type-related template parameters. Traits classes tend to be templates themselves, since the types and constants they contain are seen as characteristics of the primary template parameter(s) (for example, char and wchar_t).

It is also useful to be able to associate functionality with template arguments, so that client programmers can easily customize behavior when they code. The following version of the PoohCorner program, for instance, supports different types of entertainment:.

//: C05:PoohCorner2.cpp

// Illustrates policy classes

#include

using namespace std;

// Item classes:

class Water {

public:

  friend ostream& operator<<(ostream& os, const Water&) {

    return os << "Water";

  }

};

class Milk {

public:

  friend ostream& operator<<(ostream& os, const Milk&) {

    return os << "Milk";

  }

};

class Honey {

public:

  friend ostream& operator<<(ostream& os, const Honey&) {

    return os << "Honey";

  }

};

class Cookies {

public:

  friend ostream& operator<<(ostream& os, const Cookies&) {

    return os << "Cookies";

  }

};

// Guest classes:

class Bear {

public:

  friend ostream& operator<<(ostream& os, const Bear&) {

    return os << "Pooh";

  }

};

class Boy {

public:

  friend ostream& operator<<(ostream& os, const Boy&) {

    return os << "Christopher Robin";

  }

};

// Traits template

template

class GuestTraits;

// Traits specializations for Guest types

template<>

class GuestTraits {

public:

  typedef Water beverage_type;

  typedef Honey snack_type;

};

template<>

class GuestTraits {

public:

  typedef Milk beverage_type;

  typedef Cookies snack_type;

};

// Policy classes (require a static doAction() function)

class Feed {

public:

  static const char* doAction() {

    return "Feeding";

  }

};

class Stuff {

public:

  static const char* doAction() {

    return "Stuffing";

  }

};

// The Guest template (uses a policy and a traits class)

template< class Guest, class Action, class traits =

                      GuestTraits >

class PoohCorner {

  Guest theGuest;

  typedef typename traits::beverage_type beverage_type;

  typedef typename traits::snack_type snack_type;

  beverage_type bev;

  snack_type snack;

public:

  PoohCorner(const Guest& g)

    : theGuest(g), bev(beverage_type()), snack(snack_type()) {}

  void entertain() {

    cout << Action::doAction() << " " << theGuest

         << " with " << bev

         << " and " << snack << endl;

  }

};

int main() {

  Boy cr;

  PoohCorner pc1(cr);

  pc1.entertain();

  Bear pb;

  PoohCorner pc2(pb);

  pc2.entertain();

} ///:~.

The Action template parameter in the PoohCorner class expects to have a static member function named doAction( ), which is used in PoohCorner<>::entertain( ). Users can choose Feed or Stuff at will, both of which provide the required function. Classes that encapsulate functionality in this way are referred to as policy classes. The entertainment "policies" are provided above through Feed::doAction( ) and Stuff::doAction( ). These policy classes happen to be ordinary classes, but they can be templates, and can be combined with inheritance to great advantage. For more in-depth information on policy-based design, see is Andrei Alexandrescu’s book,[64] the definitive source.

<p>The curiously recurring template pattern</p>

Any novice C++ programmer can figure out how to modify a class to keep track of the number of objects of that class that currently exist. All you have to do is to add static members, and modify constructor and destructor logic, as follows:.

//: C05:CountedClass.cpp

// Object counting via static members

#include

using namespace std;

Перейти на страницу:

Похожие книги

1С: Бухгалтерия 8 с нуля
1С: Бухгалтерия 8 с нуля

Книга содержит полное описание приемов и методов работы с программой 1С:Бухгалтерия 8. Рассматривается автоматизация всех основных участков бухгалтерии: учет наличных и безналичных денежных средств, основных средств и НМА, прихода и расхода товарно-материальных ценностей, зарплаты, производства. Описано, как вводить исходные данные, заполнять справочники и каталоги, работать с первичными документами, проводить их по учету, формировать разнообразные отчеты, выводить данные на печать, настраивать программу и использовать ее сервисные функции. Каждый урок содержит подробное описание рассматриваемой темы с детальным разбором и иллюстрированием всех этапов.Для широкого круга пользователей.

Алексей Анатольевич Гладкий

Программирование, программы, базы данных / Программное обеспечение / Бухучет и аудит / Финансы и бизнес / Книги по IT / Словари и Энциклопедии
1С: Управление торговлей 8.2
1С: Управление торговлей 8.2

Современные торговые предприятия предлагают своим клиентам широчайший ассортимент товаров, который исчисляется тысячами и десятками тысяч наименований. Причем многие позиции могут реализовываться на разных условиях: предоплата, отсрочка платежи, скидка, наценка, объем партии, и т.д. Клиенты зачастую делятся на категории – VIP-клиент, обычный клиент, постоянный клиент, мелкооптовый клиент, и т.д. Товарные позиции могут комплектоваться и разукомплектовываться, многие товары подлежат обязательной сертификации и гигиеническим исследованиям, некондиционные позиции необходимо списывать, на складах периодически должна проводиться инвентаризация, каждая компания должна иметь свою маркетинговую политику и т.д., вообщем – современное торговое предприятие представляет живой организм, находящийся в постоянном движении.Очевидно, что вся эта кипучая деятельность требует автоматизации. Для решения этой задачи существуют специальные программные средства, и в этой книге мы познакомим вам с самым популярным продуктом, предназначенным для автоматизации деятельности торгового предприятия – «1С Управление торговлей», которое реализовано на новейшей технологической платформе версии 1С 8.2.

Алексей Анатольевич Гладкий

Финансы / Программирование, программы, базы данных