Skip to main content
deleted 57 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I amhave been learning C++ for the lastpast few weeks, and I made my first try with policy classes. Ultimately my goal is to use policy classes to manufacture wrapper classes on an std::vector where part. Part of what the wrapper class does is also generate a custom allocator class that depends on the policy classes.

So that'sThat's the inspiration for what I have below, which is a widget maker that makes widgets with a printing function that depends on the templated inputs. Here's the printing

Printing policy classes:

template <class T>
struct Chatty
{
  static void logVal(T val){
    std::cout << val << std::endl;
  }
  static void logS(std::string s){
    std::cout << s << std::endl;
  }
};
   
template <class T>
struct Silent
{
  static void logVal(T val){}
  static void logS(std::string s){}
};

Here's WidgetWidget:

class Widget {
public:
  int x;
  int y;
  std::function<void(std::string)> print;
  Widget()
    :x(0), y(0), print([](std::string){})
  {};
};

And finally here's WidgetManagerWidgetManager:

template < template <class Created> class CreationPolicy, template <class Created> class LoggingPolicy>
class WidgetManager : public CreationPolicy<Widget>, public LoggingPolicy<Widget>
{
public:
  WidgetManager() {};
  static Widget* doAll(){
    Widget* w = WidgetManager::Create();
    std::function<void(std::string)> f1 = WidgetManager::logS;
    w->print = WidgetManager::logS;
    return w;
  }
};

I would appreciate feedback on all aspects of the code, but I also have two specific questions also:

  1. My biggest concern is: whether there'sis there a better way to assign functions functions to the Widgets I am making? For example, perhaps I could directly directly assign member functions of WidgetsWidgets from my WidgetManagerWidgetManager, but but then I'd have to bind the WidgetMaker's staticWidgetMaker's static functions to individual individual instances of WidgetsWidgets, which seems like more overhead. What's What's best performance-wise?
  2. I am sure WidgetManager::doAll()WidgetManager::doAll() is not the way to go, but I am struggling to find another way to call functions from various policies all together. What's a better way to do this?

I am learning C++ the last few weeks, and I made my first try with policy classes. Ultimately my goal is to use policy classes to manufacture wrapper classes on an std::vector where part of what the wrapper class does is also generate a custom allocator class that depends on the policy classes.

So that's the inspiration for what I have below, which is a widget maker that makes widgets with a printing function that depends on the templated inputs. Here's the printing policy classes:

template <class T>
struct Chatty
{
  static void logVal(T val){
    std::cout << val << std::endl;
  }
  static void logS(std::string s){
    std::cout << s << std::endl;
  }
};
   
template <class T>
struct Silent
{
  static void logVal(T val){}
  static void logS(std::string s){}
};

Here's Widget

class Widget {
public:
  int x;
  int y;
  std::function<void(std::string)> print;
  Widget()
    :x(0), y(0), print([](std::string){})
  {};
};

And finally here's WidgetManager:

template < template <class Created> class CreationPolicy, template <class Created> class LoggingPolicy>
class WidgetManager : public CreationPolicy<Widget>, public LoggingPolicy<Widget>
{
public:
  WidgetManager() {};
  static Widget* doAll(){
    Widget* w = WidgetManager::Create();
    std::function<void(std::string)> f1 = WidgetManager::logS;
    w->print = WidgetManager::logS;
    return w;
  }
};

I would appreciate feedback on all aspects of the code, but I have two specific questions also:

  1. My biggest concern is whether there's a better way to assign functions to the Widgets I am making? For example, perhaps I could directly assign member functions of Widgets from my WidgetManager, but then I'd have to bind the WidgetMaker's static functions to individual instances of Widgets, which seems like more overhead. What's best performance-wise?
  2. I am sure WidgetManager::doAll() is not the way to go, but I am struggling to find another way to call functions from various policies all together. What's a better way to do this?

I have been learning C++ for the past few weeks, and I made my first try with policy classes. Ultimately my goal is to use policy classes to manufacture wrapper classes on an std::vector. Part of what the wrapper class does is generate a custom allocator class that depends on the policy classes.

That's the inspiration for what I have below, which is a widget maker that makes widgets with a printing function that depends on the templated inputs.

Printing policy classes:

template <class T>
struct Chatty
{
  static void logVal(T val){
    std::cout << val << std::endl;
  }
  static void logS(std::string s){
    std::cout << s << std::endl;
  }
};
   
template <class T>
struct Silent
{
  static void logVal(T val){}
  static void logS(std::string s){}
};

Widget:

class Widget {
public:
  int x;
  int y;
  std::function<void(std::string)> print;
  Widget()
    :x(0), y(0), print([](std::string){})
  {};
};

WidgetManager:

template < template <class Created> class CreationPolicy, template <class Created> class LoggingPolicy>
class WidgetManager : public CreationPolicy<Widget>, public LoggingPolicy<Widget>
{
public:
  WidgetManager() {};
  static Widget* doAll(){
    Widget* w = WidgetManager::Create();
    std::function<void(std::string)> f1 = WidgetManager::logS;
    w->print = WidgetManager::logS;
    return w;
  }
};

I would appreciate feedback on all aspects of the code, but I also have two specific questions:

  1. My biggest concern: is there a better way to assign functions to the Widgets I am making? For example, perhaps I could directly assign member functions of Widgets from my WidgetManager, but then I'd have to bind the WidgetMaker's static functions to individual instances of Widgets, which seems like more overhead. What's best performance-wise?
  2. I am sure WidgetManager::doAll() is not the way to go, but I am struggling to find another way to call functions from various policies all together. What's a better way to do this?
Source Link
sunny
  • 1.8k
  • 1
  • 13
  • 29

Policy classes for making widgets

I am learning C++ the last few weeks, and I made my first try with policy classes. Ultimately my goal is to use policy classes to manufacture wrapper classes on an std::vector where part of what the wrapper class does is also generate a custom allocator class that depends on the policy classes.

So that's the inspiration for what I have below, which is a widget maker that makes widgets with a printing function that depends on the templated inputs. Here's the printing policy classes:

template <class T>
struct Chatty
{
  static void logVal(T val){
    std::cout << val << std::endl;
  }
  static void logS(std::string s){
    std::cout << s << std::endl;
  }
};
   
template <class T>
struct Silent
{
  static void logVal(T val){}
  static void logS(std::string s){}
};

Here's Widget

class Widget {
public:
  int x;
  int y;
  std::function<void(std::string)> print;
  Widget()
    :x(0), y(0), print([](std::string){})
  {};
};

And finally here's WidgetManager:

template < template <class Created> class CreationPolicy, template <class Created> class LoggingPolicy>
class WidgetManager : public CreationPolicy<Widget>, public LoggingPolicy<Widget>
{
public:
  WidgetManager() {};
  static Widget* doAll(){
    Widget* w = WidgetManager::Create();
    std::function<void(std::string)> f1 = WidgetManager::logS;
    w->print = WidgetManager::logS;
    return w;
  }
};

I would appreciate feedback on all aspects of the code, but I have two specific questions also:

  1. My biggest concern is whether there's a better way to assign functions to the Widgets I am making? For example, perhaps I could directly assign member functions of Widgets from my WidgetManager, but then I'd have to bind the WidgetMaker's static functions to individual instances of Widgets, which seems like more overhead. What's best performance-wise?
  2. I am sure WidgetManager::doAll() is not the way to go, but I am struggling to find another way to call functions from various policies all together. What's a better way to do this?