All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ShapeService.h
1 #pragma once
2 
3 #include "ShapesModule.h"
4 #include "Types.h"
5 
6 namespace Eegeo
7 {
8  namespace Shapes
9  {
10  template <typename TModel, typename TCreateParams, typename TFactory, typename TRepository>
12  {
13  public:
14  typedef TModel ModelType;
15  typedef TCreateParams CreateParamsType;
16  typedef TFactory ModelFactoryType;
17  typedef TRepository ModelRepositoryType;
18 
19  virtual ~TShapeServiceBase() {}
20 
21  virtual typename ModelType::IdType Create(const CreateParamsType& polygonCreateParams) = 0;
22 
23  virtual void Destroy(typename ModelType::IdType modelId) = 0;
24 
25  virtual bool Exists(typename ModelType::IdType modelId) const = 0;
26 
27  virtual ModelType& Get(typename ModelType::IdType modelId) const = 0;
28  };
29 
30 
31  template <typename TBase>
32  class TShapeService : public TBase, private Eegeo::NonCopyable
33  {
34  public:
35  typedef typename TBase::ModelType ModelType;
36  typedef typename TBase::CreateParamsType CreateParamsType;
37  typedef typename TBase::ModelFactoryType ModelFactoryType;
38  typedef typename TBase::ModelRepositoryType ModelRepositoryType;
39 
40 
41 
42 
43  TShapeService(ModelFactoryType& modelFactory,
44  ModelRepositoryType& modelRepository)
45  : m_modelFactory(modelFactory)
46  , m_modelRepository(modelRepository)
47  {
48 
49  }
50 
51  virtual ~TShapeService() {}
52 
53  virtual typename ModelType::IdType Create(const CreateParamsType& createParams)
54  {
55  ModelType* pShapeModel = m_modelFactory.Create(createParams);
56 
57  m_modelRepository.Add(pShapeModel->GetId(), pShapeModel);
58 
59  return pShapeModel->GetId();
60  }
61 
62  virtual void Destroy(typename ModelType::IdType shapeModelId)
63  {
64  Eegeo_ASSERT(Exists(shapeModelId), "Shape id %d does not exist", shapeModelId);
65 
66  ModelType* pShapeModel = m_modelRepository.Get(shapeModelId);
67 
68  m_modelRepository.Remove(shapeModelId);
69 
70  Eegeo_DELETE pShapeModel;
71  }
72 
73  virtual bool Exists(typename ModelType::IdType shapeModelId) const
74  {
75  return m_modelRepository.Contains(shapeModelId);
76  }
77 
78  virtual ModelType& Get(typename ModelType::IdType shapeModelId) const
79  {
80  Eegeo_ASSERT(Exists(shapeModelId), "Shape model id %d does not exist", shapeModelId);
81  return *m_modelRepository.Get(shapeModelId);
82  }
83 
84 
85 
86  private:
87  ModelFactoryType& m_modelFactory;
88  ModelRepositoryType& m_modelRepository;
89  };
90 
91  }
92 }