CanvasCV: CanvasCV-doxygen/src/canvascv/shapes/shapefactory.h Source File
CanvasCV  1.0.0
shapefactory.h
1 #ifndef SHAPEFACTORY_H
2 #define SHAPEFACTORY_H
3 
4 #include <opencv2/core.hpp>
5 #include <map>
6 #include <functional>
7 
8 #include "canvascv/themes/themerepository.h"
9 
10 namespace canvascv
11 {
12 
13 using namespace std;
14 using namespace cv;
15 
16 class Shape;
17 
25 {
26 public:
28  static Shape *newShape(std::string type, const cv::Point &pos);
29 protected:
30  typedef std::function<Shape*(const cv::Point &)> Allocator;
31  static void addShape(std::string name, Allocator a);
32 
33 private:
34  typedef map<std::string,Allocator> AllocatorsMap;
35  static AllocatorsMap *allocators;
36 };
37 
44 template <class T>
46 {
47 public:
49  static bool addType(std::string name);
50 
52  static T *newShape(const cv::Point &pos);
53 };
54 
55 template <class T>
56 bool ShapeFactoryT<T>::addType(std::string name)
57 {
58  static bool doneOnce;
59  if (! doneOnce)
60  {
61  addShape(name, [](const cv::Point& pos)->Shape*{ return new T(pos);});
62  doneOnce = true;
63  }
64  return true;
65 }
66 
67 template <class T>
68 T *ShapeFactoryT<T>::newShape(const cv::Point &pos)
69 {
70  T *shape = new T(pos);
72  return shape;
73 }
74 
75 }
76 
78 #define CCV_REGISTER_SHAPE(X) static bool regShape##X = canvascv::ShapeFactoryT<X>::addType(#X)
79 
80 
81 #endif // SHAPEFACTORY_H
The ShapeFactory class.
Definition: shapefactory.h:24
The c++ std namespace.
Definition: canvas.h:462
The OpenCV namespace.
Definition: canvas.h:465
The Shape class hierarchy is for geomertric user interaction.
Definition: shape.h:32
static void applyCurrentTheme(Shape *shape)
used automatically by the ShapeFactory when creating a shape
The ShapeFactoryT class.
Definition: shapefactory.h:45
This namespace holds all the classes of the CanvasCV library.
Definition: canvas.h:20
static T * newShape(const cv::Point &pos)
create a shape by compile time type at a certain initial pos (don&#39;t use directly. Use Canvas::createS...
Definition: shapefactory.h:68
static bool addType(std::string name)
add a shape by compile time type - use the macro CCV_REGISTER_SHAPE
Definition: shapefactory.h:56