1010
1111namespace xt
1212{
13+ enum class xfile_mode { load, init, init_on_fail };
1314
1415 template <class T >
1516 class xfile_value_reference
@@ -113,7 +114,7 @@ namespace xt
113114 static constexpr layout_type static_layout = layout_type::dynamic;
114115 static constexpr bool contiguous_layout = true ;
115116
116- xfile_array_container () = default ;
117+ xfile_array_container (const std::string& path, xfile_mode mode=xfile_mode::load) ;
117118 ~xfile_array_container ();
118119
119120 xfile_array_container (const self_type&) = default ;
@@ -194,7 +195,6 @@ namespace xt
194195 load_simd (size_type i) const ;
195196
196197 const std::string& path () const noexcept ;
197- void ignore_empty_path (bool ignore);
198198 void set_path (const std::string& path);
199199
200200 template <class C >
@@ -204,13 +204,11 @@ namespace xt
204204
205205 private:
206206
207- bool enable_io (const std::string& path) const ;
208-
209207 E m_storage;
210208 bool m_dirty;
211209 IOH m_io_handler;
212210 std::string m_path;
213- bool m_ignore_empty_path ;
211+ xfile_mode m_file_mode ;
214212 };
215213
216214 template <class T ,
@@ -339,6 +337,16 @@ namespace xt
339337 return return_type::value;
340338 }
341339
340+ template <class E , class IOH >
341+ inline xfile_array_container<E, IOH>::xfile_array_container(const std::string& path, xfile_mode file_mode)
342+ : m_storage()
343+ , m_dirty(false )
344+ , m_io_handler()
345+ , m_file_mode(file_mode)
346+ {
347+ set_path (path);
348+ }
349+
342350 template <class E , class IOH >
343351 inline xfile_array_container<E, IOH>::~xfile_array_container ()
344352 {
@@ -352,7 +360,7 @@ namespace xt
352360 , m_dirty(true )
353361 , m_io_handler()
354362 , m_path(detail::file_helper<E>::path(e))
355- , m_ignore_empty_path( false )
363+ , m_file_mode(xfile_mode::init )
356364 {
357365 }
358366
@@ -363,7 +371,7 @@ namespace xt
363371 , m_dirty(true )
364372 , m_io_handler()
365373 , m_path(path)
366- , m_ignore_empty_path( false )
374+ , m_file_mode(xfile_mode::init )
367375 {
368376 }
369377
@@ -562,18 +570,6 @@ namespace xt
562570 m_io_handler.configure_format (config);
563571 }
564572
565- template <class E , class IOH >
566- inline void xfile_array_container<E, IOH>::ignore_empty_path(bool ignore)
567- {
568- m_ignore_empty_path = ignore;
569- }
570-
571- template <class E , class IOH >
572- inline bool xfile_array_container<E, IOH>::enable_io(const std::string& path) const
573- {
574- return !path.empty () || !m_ignore_empty_path;
575- }
576-
577573 template <class E , class IOH >
578574 inline void xfile_array_container<E, IOH>::set_path(const std::string& path)
579575 {
@@ -582,10 +578,20 @@ namespace xt
582578 // maybe write to old file
583579 flush ();
584580 m_path = path;
585- // read new file
586- if (enable_io (path))
581+ if (m_file_mode != xfile_mode::init)
587582 {
588- m_io_handler.read (m_storage, path);
583+ // read new file
584+ try
585+ {
586+ m_io_handler.read (m_storage, path);
587+ }
588+ catch (const std::runtime_error& e)
589+ {
590+ if (m_file_mode == xfile_mode::load)
591+ {
592+ throw e;
593+ }
594+ }
589595 }
590596 }
591597 }
@@ -595,10 +601,7 @@ namespace xt
595601 {
596602 if (m_dirty)
597603 {
598- if (enable_io (m_path))
599- {
600- m_io_handler.write (m_storage, m_path);
601- }
604+ m_io_handler.write (m_storage, m_path);
602605 m_dirty = false ;
603606 }
604607 }
0 commit comments