comparison OrthancServer/DicomInstanceToStore.h @ 2894:a27b0e9a3fd9 db-changes

refactoring DicomInstanceToStore
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 18 Oct 2018 10:35:09 +0200
parents a21b244efb37
children e5e3253a1164
comparison
equal deleted inserted replaced
2893:1723cbba55c7 2894:a27b0e9a3fd9
31 **/ 31 **/
32 32
33 33
34 #pragma once 34 #pragma once
35 35
36 #include "../Core/DicomParsing/ParsedDicomFile.h" 36 #include "../Core/DicomFormat/DicomMap.h"
37 #include "../Core/OrthancException.h"
38 #include "DicomInstanceOrigin.h" 37 #include "DicomInstanceOrigin.h"
39 #include "ServerIndex.h" 38 #include "ServerEnumerations.h"
39
40 #include <boost/shared_ptr.hpp>
40 41
41 namespace Orthanc 42 namespace Orthanc
42 { 43 {
44 class ParsedDicomFile;
45
43 class DicomInstanceToStore 46 class DicomInstanceToStore
44 { 47 {
48 public:
49 typedef std::map<std::pair<ResourceType, MetadataType>, std::string> MetadataMap;
50
45 private: 51 private:
46 template <typename T> 52 struct PImpl;
47 class SmartContainer 53 boost::shared_ptr<PImpl> pimpl_;
48 {
49 private:
50 T* content_;
51 bool toDelete_;
52 bool isReadOnly_;
53
54 void Deallocate()
55 {
56 if (content_ && toDelete_)
57 {
58 delete content_;
59 toDelete_ = false;
60 content_ = NULL;
61 }
62 }
63
64 public:
65 SmartContainer() : content_(NULL), toDelete_(false), isReadOnly_(true)
66 {
67 }
68
69 ~SmartContainer()
70 {
71 Deallocate();
72 }
73
74 void Allocate()
75 {
76 Deallocate();
77 content_ = new T;
78 toDelete_ = true;
79 isReadOnly_ = false;
80 }
81
82 void TakeOwnership(T* content)
83 {
84 if (content == NULL)
85 {
86 throw OrthancException(ErrorCode_ParameterOutOfRange);
87 }
88
89 Deallocate();
90 content_ = content;
91 toDelete_ = true;
92 isReadOnly_ = false;
93 }
94
95 void SetReference(T& content) // Read and write assign, without transfering ownership
96 {
97 Deallocate();
98 content_ = &content;
99 toDelete_ = false;
100 isReadOnly_ = false;
101 }
102
103 void SetConstReference(const T& content) // Read-only assign, without transfering ownership
104 {
105 Deallocate();
106 content_ = &const_cast<T&>(content);
107 toDelete_ = false;
108 isReadOnly_ = true;
109 }
110
111 bool HasContent() const
112 {
113 return content_ != NULL;
114 }
115
116 T& GetContent()
117 {
118 if (content_ == NULL)
119 {
120 throw OrthancException(ErrorCode_BadSequenceOfCalls);
121 }
122
123 if (isReadOnly_)
124 {
125 throw OrthancException(ErrorCode_ReadOnly);
126 }
127
128 return *content_;
129 }
130
131 const T& GetConstContent() const
132 {
133 if (content_ == NULL)
134 {
135 throw OrthancException(ErrorCode_BadSequenceOfCalls);
136 }
137
138 return *content_;
139 }
140 };
141
142 DicomInstanceOrigin origin_;
143 SmartContainer<std::string> buffer_;
144 SmartContainer<ParsedDicomFile> parsed_;
145 SmartContainer<DicomMap> summary_;
146 SmartContainer<Json::Value> json_;
147 ServerIndex::MetadataMap metadata_;
148
149 void ComputeMissingInformation();
150 54
151 public: 55 public:
152 void SetOrigin(const DicomInstanceOrigin& origin) 56 DicomInstanceToStore();
153 { 57
154 origin_ = origin; 58 void SetOrigin(const DicomInstanceOrigin& origin);
155 }
156 59
157 const DicomInstanceOrigin& GetOrigin() const 60 const DicomInstanceOrigin& GetOrigin() const;
158 {
159 return origin_;
160 }
161 61
162 void SetBuffer(const std::string& dicom) 62 void SetBuffer(const std::string& dicom);
163 {
164 buffer_.SetConstReference(dicom);
165 }
166 63
167 void SetParsedDicomFile(ParsedDicomFile& parsed) 64 void SetParsedDicomFile(ParsedDicomFile& parsed);
168 {
169 parsed_.SetReference(parsed);
170 }
171 65
172 void SetSummary(const DicomMap& summary) 66 void SetSummary(const DicomMap& summary);
173 {
174 summary_.SetConstReference(summary);
175 }
176 67
177 void SetJson(const Json::Value& json) 68 void SetJson(const Json::Value& json);
178 { 69
179 json_.SetConstReference(json); 70 const MetadataMap& GetMetadata() const;
180 } 71
72 MetadataMap& GetMetadata();
181 73
182 void AddMetadata(ResourceType level, 74 void AddMetadata(ResourceType level,
183 MetadataType metadata, 75 MetadataType metadata,
184 const std::string& value); 76 const std::string& value);
185
186 const ServerIndex::MetadataMap& GetMetadata() const
187 {
188 return metadata_;
189 }
190
191 ServerIndex::MetadataMap& GetMetadata()
192 {
193 return metadata_;
194 }
195 77
196 const char* GetBufferData(); 78 const char* GetBufferData();
197 79
198 size_t GetBufferSize(); 80 size_t GetBufferSize();
199 81