libCZI
Reading and Writing CZI documents made easy
libCZI_StreamsLib.h
1 // SPDX-FileCopyrightText: 2023 Carl Zeiss Microscopy GmbH
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4 
5 #pragma once
6 
7 #include "ImportExport.h"
8 #include <cstdint>
9 #include <string>
10 #include <map>
11 #include <memory>
12 #include <functional>
13 #include <stdexcept>
14 
15 namespace libCZI
16 {
17  class IStream;
18 
26  class LIBCZI_API StreamsFactory
27  {
28  public:
30  struct LIBCZI_API Property
31  {
32  public:
34  enum class Type
35  {
36  Invalid,
37  Int32,
38  Float,
39  Double,
40  Boolean,
41  String
42  };
43 
45  Property() : type(Type::Invalid)
46  {
47  }
48 
52  explicit Property(std::int32_t v)
53  {
54  this->SetInt32(v);
55  }
56 
60  explicit Property(double v)
61  {
62  this->SetDouble(v);
63  }
64 
68  explicit Property(float v)
69  {
70  this->SetFloat(v);
71  }
72 
76  explicit Property(bool v)
77  {
78  this->SetBool(v);
79  }
80 
84  explicit Property(const std::string& v)
85  {
86  this->SetString(v);
87  }
88 
92  explicit Property(const char* string)
93  {
94  this->SetString(string);
95  }
96 
100  void SetInt32(std::int32_t v)
101  {
102  this->type = Type::Int32;
103  this->int32Value = v;
104  }
105 
109  void SetDouble(double v)
110  {
111  this->type = Type::Double;
112  this->doubleValue = v;
113  }
114 
118  void SetFloat(float v)
119  {
120  this->type = Type::Float;
121  this->floatValue = v;
122  }
123 
127  void SetBool(bool v)
128  {
129  this->type = Type::Boolean;
130  this->boolValue = v;
131  }
132 
136  void SetString(const std::string& v)
137  {
138  this->type = Type::String;
139  this->stringValue = v;
140  }
141 
143  std::int32_t GetAsInt32OrThrow() const
144  {
145  this->ThrowIfTypeIsUnequalTo(Type::Int32);
146  return this->int32Value;
147  }
148 
150  double GetAsDoubleOrThrow() const
151  {
152  this->ThrowIfTypeIsUnequalTo(Type::Double);
153  return this->doubleValue;
154  }
155 
157  float GetAsFloatOrThrow() const
158  {
159  this->ThrowIfTypeIsUnequalTo(Type::Float);
160  return this->floatValue;
161  }
162 
164  bool GetAsBoolOrThrow() const
165  {
166  this->ThrowIfTypeIsUnequalTo(Type::Boolean);
167  return this->boolValue;
168  }
169 
171  std::string GetAsStringOrThrow() const
172  {
173  this->ThrowIfTypeIsUnequalTo(Type::String);
174  return this->stringValue;
175  }
176 
178  Type GetType() const
179  {
180  return this->type;
181  }
182 
183  private:
184  Type type;
185 
186  union
187  {
188  std::int32_t int32Value;
189  float floatValue;
190  double doubleValue;
191  bool boolValue;
192  };
193 
194  std::string stringValue;
195 
196  void ThrowIfTypeIsUnequalTo(Type typeToCheck) const
197  {
198  if (this->type != typeToCheck)
199  {
200  throw std::runtime_error("Unexpected type encountered.");
201  }
202  }
203  };
204 
207  {
208  public:
210  enum
211  {
212  kCurlHttp_Proxy = 100,
213 
214  kCurlHttp_UserAgent = 101,
215 
216  kCurlHttp_Timeout = 102,
217 
218  kCurlHttp_ConnectTimeout = 103,
219 
220  kCurlHttp_Xoauth2Bearer = 104,
221 
222  kCurlHttp_Cookie = 105,
223 
224  kCurlHttp_SslVerifyPeer = 106,
225 
226  kCurlHttp_SslVerifyHost = 107,
227 
228  kCurlHttp_FollowLocation = 108,
229 
230  kCurlHttp_MaxRedirs = 109,
231 
232  kCurlHttp_CaInfo = 110,
233 
234  kCurlHttp_CaInfoBlob = 111,
235 
239  kAzureBlob_AuthenticationMode = 200,
240  };
241  };
242 
244  struct LIBCZI_API CreateStreamInfo
245  {
246  std::string class_name;
247 
248  std::map<int, Property> property_bag;
249  };
250 
253  {
254  const char* property_name;
257  };
258 
268 
273  static void Initialize();
274 
283  static std::shared_ptr<libCZI::IStream> CreateStream(const CreateStreamInfo& stream_info, const std::string& file_identifier);
284 
293  static std::shared_ptr<libCZI::IStream> CreateStream(const CreateStreamInfo& stream_info, const std::wstring& file_identifier);
294 
296  struct LIBCZI_API StreamClassInfo
297  {
298  std::string class_name;
299  std::string short_description;
300 
303  std::function<std::string()> get_build_info;
304 
310  std::function<Property(const char* property_name)> get_property;
311  };
312 
320  static bool GetStreamInfoForClass(int index, StreamClassInfo& stream_info);
321 
325  static int GetStreamClassesCount();
326 
332  static std::shared_ptr<libCZI::IStream> CreateDefaultStreamForFile(const char* filename);
333 
339  static std::shared_ptr<libCZI::IStream> CreateDefaultStreamForFile(const wchar_t* filename);
340 
345 
350  };
351 }
Here the keys for the property-bag with options for creating a stream-object are gathered.
Definition: libCZI_StreamsLib.h:207
Definition: libCZI_StreamsLib.h:27
static std::shared_ptr< libCZI::IStream > CreateStream(const CreateStreamInfo &stream_info, const std::wstring &file_identifier)
static const char * kStreamClassInfoProperty_CurlHttp_CaInfo
Definition: libCZI_StreamsLib.h:344
static const StreamPropertyBagPropertyInfo * GetStreamPropertyBagPropertyInfo(int *count)
static void Initialize()
static int GetStreamClassesCount()
static std::shared_ptr< libCZI::IStream > CreateDefaultStreamForFile(const wchar_t *filename)
static std::shared_ptr< libCZI::IStream > CreateStream(const CreateStreamInfo &stream_info, const std::string &file_identifier)
static bool GetStreamInfoForClass(int index, StreamClassInfo &stream_info)
static std::shared_ptr< libCZI::IStream > CreateDefaultStreamForFile(const char *filename)
static const char * kStreamClassInfoProperty_CurlHttp_CaPath
Definition: libCZI_StreamsLib.h:349
External interfaces, classes, functions and structs are found in the namespace "libCZI".
Definition: libCZI.h:31
@ Invalid
Invalid pixel type.
The parameters for creating an instance of a stream object.
Definition: libCZI_StreamsLib.h:245
std::map< int, Property > property_bag
A property-bag with options for creating the stream-object.
Definition: libCZI_StreamsLib.h:248
std::string class_name
Name of the class (this uniquely identifies the class).
Definition: libCZI_StreamsLib.h:246
This declares a variant type (to be used with the property bag in the streams factory).
Definition: libCZI_StreamsLib.h:31
Property(const std::string &v)
Definition: libCZI_StreamsLib.h:84
void SetString(const std::string &v)
Definition: libCZI_StreamsLib.h:136
float GetAsFloatOrThrow() const
Returns float value if ValueType is 'Float', otherwise throws a RuntimeError.
Definition: libCZI_StreamsLib.h:157
Property(std::int32_t v)
Definition: libCZI_StreamsLib.h:52
Type
Values that represent the type represented by this variant.
Definition: libCZI_StreamsLib.h:35
Property(float v)
Definition: libCZI_StreamsLib.h:68
Property(double v)
Definition: libCZI_StreamsLib.h:60
void SetDouble(double v)
Definition: libCZI_StreamsLib.h:109
std::string GetAsStringOrThrow() const
Returns string value if ValueType is string, otherwise throws a RuntimeError.
Definition: libCZI_StreamsLib.h:171
double GetAsDoubleOrThrow() const
Returns double value if ValueType is double, otherwise throws a RuntimeError.
Definition: libCZI_StreamsLib.h:150
void SetInt32(std::int32_t v)
Definition: libCZI_StreamsLib.h:100
Property()
Default constructor - setting the variant to 'invalid'.
Definition: libCZI_StreamsLib.h:45
Property(const char *string)
Definition: libCZI_StreamsLib.h:92
bool GetAsBoolOrThrow() const
Returns boolean value if ValueType is boolean, otherwise throws a RuntimeError.
Definition: libCZI_StreamsLib.h:164
Property(bool v)
Definition: libCZI_StreamsLib.h:76
void SetBool(bool v)
Definition: libCZI_StreamsLib.h:127
std::int32_t GetAsInt32OrThrow() const
Returns integer value if ValueType is int, otherwise throws a RuntimeError.
Definition: libCZI_StreamsLib.h:143
Type GetType() const
Returns ValueType.
Definition: libCZI_StreamsLib.h:178
void SetFloat(float v)
Definition: libCZI_StreamsLib.h:118
This structure gathers information about a stream class.
Definition: libCZI_StreamsLib.h:297
std::string class_name
Name of the class (this uniquely identifies the class).
Definition: libCZI_StreamsLib.h:298
std::function< Property(const char *property_name)> get_property
Definition: libCZI_StreamsLib.h:310
std::function< std::string()> get_build_info
Definition: libCZI_StreamsLib.h:303
std::string short_description
A short and informal description of the class.
Definition: libCZI_StreamsLib.h:299
Information about a property for the property bag when creating a stream object.
Definition: libCZI_StreamsLib.h:253
int property_id
The numerical identifier for the property.
Definition: libCZI_StreamsLib.h:255
Property::Type property_type
Type of the property.
Definition: libCZI_StreamsLib.h:256
const char * property_name
(Proposed) name of the property. This is a null-terminated static string. It is enum name with the in...
Definition: libCZI_StreamsLib.h:254