NCBI C++ ToolKit
edit_commands_impl.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef OBJECTS_OBJMGR_IMPL___EDIT_COMMNADS_IMPL__HPP
2 #define OBJECTS_OBJMGR_IMPL___EDIT_COMMNADS_IMPL__HPP
3 
4 /* $Id: edit_commands_impl.hpp 91808 2020-12-14 15:35:18Z grichenk $
5 * ===========================================================================
6 *
7 * PUBLIC DOMAIN NOTICE
8 * National Center for Biotechnology Information
9 *
10 * This software/database is a "United States Government Work" under the
11 * terms of the United States Copyright Act. It was written as part of
12 * the author's official duties as a United States Government employee and
13 * thus cannot be copyrighted. This software/database is freely available
14 * to the public for use. The National Library of Medicine and the U.S.
15 * Government have not placed any restriction on its use or reproduction.
16 *
17 * Although all reasonable efforts have been taken to ensure the accuracy
18 * and reliability of the software and data, the NLM and the U.S.
19 * Government do not and cannot warrant the performance or results that
20 * may be obtained by using this software or data. The NLM and the U.S.
21 * Government disclaim all warranties, express or implied, including
22 * warranties of performance, merchantability or fitness for any particular
23 * purpose.
24 *
25 * Please cite the author in any work or product based on this material.
26 *
27 * ===========================================================================
28 *
29 * Author: Maxim Didenko
30 *
31 * File Description:
32 *
33 */
34 
35 #include <corelib/ncbistd.hpp>
36 #include <corelib/ncbiobj.hpp>
37 
40 #include <objmgr/impl/tse_info.hpp>
41 #include <objmgr/edit_saver.hpp>
42 
44 #include <objmgr/tse_handle.hpp>
45 
47 
48 #include <objects/seq/Seqdesc.hpp>
49 
52 
53 template<typename CMD>
54 struct CMDReturn {
55  typedef bool TReturn;
56  static inline TReturn GetRet(CMD*) { return false; }
57 };
58 
60 {
61 public:
63 
64  template<typename CMD>
66  {
67  CRef<IEditCommand> rcmd(cmd);
68  CRef<IScopeTransaction_Impl> tr( &m_Scope.GetTransaction() );
69  cmd->Do( *tr );
70  if (tr->ReferencedOnlyOnce())
71  tr->Commit();
73  }
74 
75 private:
78 
79 private:
80 
81  void* operator new(size_t) = delete;
82  void operator delete(void*) = delete;
85 };
86 
87 
88 ///////////////////////////////////////////////////////////////////////////////
89 ///////////////////////////////////////////////////////////////////////////////
90 
91 template<typename Handle>
92 inline IEditSaver* GetEditSaver(const Handle& handle)
93 {
94  const CTSE_Info& tse = handle.GetTSE_Handle().x_GetTSE_Info();
95  IEditSaver *saver = tse.GetEditSaver().GetPointer();
96  return saver;
97 }
98 
99 template<typename T, bool cref = false>
100 struct MemetoTrait {
101  typedef T TValue;
102  typedef T TStorage;
103  typedef T TRef;
104  typedef T TConstRef;
105 
106  static inline TStorage Store(TRef t) {return t;}
107  static inline TRef Restore(TStorage t) {return t;}
108 };
109 
110 template<typename T>
111 struct MemetoTrait<T,true> {
112  typedef T TValue;
113  typedef CRef<T> TStorage;
114  typedef T& TRef;
115  typedef const T& TConstRef;
116 
117  static inline TStorage Store(TRef t) {return TStorage(&t);}
118  static inline TRef Restore(TStorage t) {return *t;}
119 };
120 
121 template<typename T>
122 struct IsCRef { enum { value = 0 }; };
123 
124 #define DEFINE_CREF_TYPE(T) \
125 template<> struct IsCRef<T> { enum { value = 1 }; }
126 
127 template<typename TEditHandle, typename T>
130  typedef typename TTrait::TValue TValue;
131  typedef typename TTrait::TStorage TStorage;
132  typedef typename TTrait::TRef TRef;
133  typedef typename TTrait::TConstRef TConstRef;
134 
135  static inline bool IsSet(const TEditHandle& handle);
136  static inline TStorage Get(const TEditHandle& handle);
137  static inline void Set(const TEditHandle& handle, const TStorage& data);
138  static inline void Reset(const TEditHandle& handle);
139 };
140 
141 template<typename T>
142 class CMemeto
143 {
144 public:
146  typedef typename TTrait::TStorage TStorage;
147  typedef typename TTrait::TRef TRef;
148  typedef typename TTrait::TConstRef TConstRef;
149 
151 
153 
154  template<typename TEditHandle>
155  CMemeto(const TEditHandle& handle)
156  {
158  m_WasSet = TFunc::IsSet(handle);
159  if (m_WasSet) m_Storage = TFunc::Get(handle);
160  }
161  template<typename TEditHandle>
162  void RestoreTo(const TEditHandle& handle)
163  {
165  if (m_WasSet) TFunc::Set(handle, m_Storage);
166  else TFunc::Reset(handle);
167  }
168  bool WasSet() const { return m_WasSet; }
169 
171 private:
173  bool m_WasSet;
174 };
175 
176 template<typename THandle, typename T>
177 struct DBFunc {
179  typedef typename TTrait::TRef TRef;
180  typedef typename TTrait::TConstRef TConstRef;
181 
182  static inline void Set(IEditSaver&, const THandle&,
184  static inline void Reset(IEditSaver&, const THandle&,
186 };
187 
188 ///////////////////////////////////////////////////////////////////////////////
189 ///////////////////////////////////////////////////////////////////////////////
190 
191 
193 
194 template<typename TEditHandle>
195 struct MemetoFunctions<TEditHandle,CSeq_descr> {
197  typedef typename TTrait::TValue TValue;
198  typedef typename TTrait::TStorage TStorage;
199 
200  static inline bool IsSet(const TEditHandle& handle)
201  { return handle.IsSetDescr(); }
202 
203  static inline TStorage Get(const TEditHandle& handle)
204  { return TStorage( const_cast<TValue*>(&handle.GetDescr())); }
205 
206  static inline void Set(const TEditHandle& handle, TStorage& data)
207  { handle.x_RealSetDescr(TTrait::Restore(data)); }
208  static inline void Reset(const TEditHandle& handle)
209  { handle.x_RealResetDescr(); }
210 };
211 
212 template<typename Handle>
214  static inline void Set(IEditSaver& saver,
215  const Handle& handle,
217  { saver.SetDescr(handle, data, mode); }
218 
219  static inline void Reset(IEditSaver& saver,
220  const Handle& handle, IEditSaver::ECallMode mode)
221  { saver.ResetDescr(handle, mode); }
222 
223  static inline void Add(IEditSaver& saver,
224  const Handle& handle,
226  { saver.AddDescr(handle, data, mode); }
227 };
228 
229 template<typename TEditHandle, typename T>
231 {
232 public:
234  typedef typename TMemeto::TTrait TTrait;
235  typedef typename TMemeto::TRef TRef;
236  typedef typename TMemeto::TStorage TStorage;
239 
240  CSetValue_EditCommand(const TEditHandle& handle, TRef value)
241  : m_Handle(handle), m_Value(TTrait::Store(value)) {}
242 
244 
245  virtual void Do(IScopeTransaction_Impl& tr)
246  {
247  m_Memeto.reset(new TMemeto(m_Handle));
249  tr.AddCommand(CRef<IEditCommand>(this));
250  IEditSaver* saver = GetEditSaver(m_Handle);
251  if (saver) {
252  tr.AddEditSaver(saver);
254  }
255  }
256 
257  virtual void Undo()
258  {
259  _ASSERT(m_Memeto.get());
260  m_Memeto->RestoreTo(m_Handle);
261  IEditSaver* saver = GetEditSaver(m_Handle);
262  if (saver) {
263  if( m_Memeto->WasSet() )
264  TDBFunc::Set(*saver, m_Handle, m_Memeto->GetRefValue(),
266  else
268  }
269  m_Memeto.reset();
270  }
271 
272 private:
273 
274  TEditHandle m_Handle;
276  unique_ptr<TMemeto> m_Memeto;
277 };
278 
279 template<typename TEditHandle, typename T>
281 {
282 public:
284  typedef typename TMemeto::TTrait TTrait;
285  typedef typename TMemeto::TRef TRef;
288 
289  CResetValue_EditCommand(const TEditHandle& handle)
290  : m_Handle(handle) {}
291 
293 
294  virtual void Do(IScopeTransaction_Impl& tr)
295  {
296  if (!TFunc::IsSet(m_Handle))
297  return;
298  m_Memeto.reset(new TMemeto(m_Handle));
300  tr.AddCommand(CRef<IEditCommand>(this));
301  IEditSaver* saver = GetEditSaver(m_Handle);
302  if (saver) {
303  tr.AddEditSaver(saver);
305  }
306  }
307 
308  virtual void Undo()
309  {
310  _ASSERT(m_Memeto.get());
311  m_Memeto->RestoreTo(m_Handle);
312  IEditSaver* saver = GetEditSaver(m_Handle);
313  if (saver) {
314  TDBFunc::Set(*saver, m_Handle, m_Memeto->GetRefValue(),
316  }
317  m_Memeto.reset();
318  }
319 
320 private:
321  TEditHandle m_Handle;
322  unique_ptr<TMemeto> m_Memeto;
323 
324 };
325 
326 
327 ///////////////////////////////////////////////////////////////////////////////
328 ///////////////////////////////////////////////////////////////////////////////
329 template<typename TEditHandle>
331 {
332 public:
335 
336  CAddDescr_EditCommand(const TEditHandle& handle, CSeq_descr& descr)
337  : m_Handle(handle), m_Descr(&descr) {}
339 
340  virtual void Do(IScopeTransaction_Impl& tr)
341  {
342  m_Memeto.reset(new TSeq_descr_Memeto(m_Handle));
343  m_Handle.x_RealAddSeq_descr(*m_Descr);
344  tr.AddCommand(CRef<IEditCommand>(this));
345  IEditSaver* saver = GetEditSaver(m_Handle);
346  if (saver) {
347  tr.AddEditSaver(saver);
348  TDBFunc::Add(*saver, m_Handle, *m_Descr, IEditSaver::eDo);
349  }
350  }
351  virtual void Undo()
352  {
353  m_Memeto->RestoreTo(m_Handle);
354  IEditSaver* saver = GetEditSaver(m_Handle);
355  if (saver) {
356  if( m_Memeto->WasSet() )
357  TDBFunc::Set(*saver, m_Handle, m_Memeto->GetRefValue(),
359  else
361  }
362  m_Memeto.reset();
363  }
364 
365 private:
366  TEditHandle m_Handle;
367  unique_ptr<TSeq_descr_Memeto> m_Memeto;
369 };
370 
371 
372 ///////////////////////////////////////////////////////////////////////////////
373 ///////////////////////////////////////////////////////////////////////////////
374 template<typename Handle>
375 struct DescDBFunc {
376  static inline void Add(IEditSaver& saver,
377  const Handle& handle,
378  const CSeqdesc& desc,
380  { saver.AddDesc(handle, desc, mode); }
381  static inline void Remove(IEditSaver& saver,
382  const Handle& handle,
383  const CSeqdesc& desc,
385  { saver.RemoveDesc(handle, desc, mode); }
386 };
387 
388 template<typename TEditHandle, bool add = true>
390  typedef bool TReturn;
391 
392  static inline TReturn Do(const TEditHandle& handle,
393  CSeqdesc& desc)
394  { return handle.x_RealAddSeqdesc(desc); }
395  static inline void Undo(const TEditHandle& handle,
396  CSeqdesc& desc)
397  { handle.x_RealRemoveSeqdesc( desc ); }
398  static inline void DoInDB(IEditSaver& saver,
399  const TEditHandle& handle,
400  const CSeqdesc& desc)
401  { DescDBFunc<TEditHandle>::Add(saver,handle, desc, IEditSaver::eDo); }
402 
403  static inline void UndoInDB(IEditSaver& saver,
404  const TEditHandle& handle,
405  const CSeqdesc& desc)
406  { DescDBFunc<TEditHandle>::Remove(saver,handle, desc, IEditSaver::eUndo); }
407 };
408 
409 template<typename TEditHandle>
410 struct DescEditAction<TEditHandle,false> {
412 
413  static inline TReturn Do(const TEditHandle& handle, CSeqdesc& desc)
414  { return handle.x_RealRemoveSeqdesc(desc); }
415  static inline void Undo(const TEditHandle& handle, CSeqdesc& desc)
416  { handle.x_RealAddSeqdesc( desc ); }
417  static inline void DoInDB(IEditSaver& saver,
418  const TEditHandle& handle,
419  const CSeqdesc& desc)
420  { DescDBFunc<TEditHandle>::Remove(saver,handle, desc, IEditSaver::eDo); }
421  static inline void UndoInDB(IEditSaver& saver,
422  const TEditHandle& handle,
423  const CSeqdesc& desc)
424  { DescDBFunc<TEditHandle>::Add(saver,handle, desc, IEditSaver::eUndo); }
425 };
426 
427 
428 
429 template <typename TEditHandle, bool add>
431 {
432 public:
434  typedef typename TAction::TReturn TReturn;
435 
436 
437  CDesc_EditCommand(const TEditHandle& handle, const CSeqdesc& desc)
438  : m_Handle(handle), m_Desc(const_cast<CSeqdesc*>(&desc)) {}
439 
440 
441  virtual ~CDesc_EditCommand() {}
442 
443  virtual void Do(IScopeTransaction_Impl& tr)
444  {
446  if (!m_Ret)
447  return;
448  tr.AddCommand(CRef<IEditCommand>(this));
449  IEditSaver* saver = GetEditSaver(m_Handle);
450  if (saver) {
451  tr.AddEditSaver(saver);
452  TAction::DoInDB(*saver, m_Handle, *m_Desc);
453  }
454  }
455 
456  virtual void Undo()
457  {
459  IEditSaver* saver = GetEditSaver(m_Handle);
460  if (saver) {
461  TAction::UndoInDB(*saver, m_Handle, *m_Desc);
462  }
463  }
464 
465 
466  TReturn GetRet() const { return m_Ret; }
467 private:
468  TEditHandle m_Handle;
471 };
472 
473 template<typename TEditHandle, bool add>
474 struct CMDReturn<CDesc_EditCommand<TEditHandle,add> > {
476  typedef typename TCMD::TReturn TReturn;
477  static inline TReturn GetRet(TCMD* cmd) { return cmd->GetRet(); }
478 };
479 
480 ///////////////////////////////////////////////////////////////////////////////
481 ///////////////////////////////////////////////////////////////////////////////
482 template<typename TEditHandle>
483 struct RemoveAction {
484  static inline void Do(CScope_Impl& scope,
485  const CSeq_entry_EditHandle& entry,
486  const TEditHandle& handle);
487  static inline void Undo(CScope_Impl& scope,
488  const CSeq_entry_EditHandle& entry,
489  const TEditHandle& handle);
490 
491  static inline void DoInDB(IEditSaver& saver,
492  const CSeq_entry_EditHandle& entry,
493  const TEditHandle& handle);
494  static inline void UndoInDB(IEditSaver& saver,
495  const CBioObjectId& old_id,
496  const CSeq_entry_EditHandle& entry,
497  const TEditHandle& handle);
498 };
499 
500 template<typename TEditHandle>
502 {
503 public:
505 
506  CRemove_EditCommand(const TEditHandle& handle,
507  CScope_Impl& scope)
508  : m_Handle(handle), m_Scope(scope) {}
509 
510 
511  virtual ~CRemove_EditCommand() {}
512 
513  virtual void Do(IScopeTransaction_Impl& tr)
514  {
515  m_Entry = m_Handle.GetParentEntry();
516  if (!m_Entry)
517  return;
518  tr.AddCommand(CRef<IEditCommand>(this));
519  IEditSaver* saver = GetEditSaver(m_Handle);
521  if (saver) {
522  tr.AddEditSaver(saver);
523  TAction::DoInDB(*saver, m_Entry, m_Handle);
524  }
525  }
526  virtual void Undo()
527  {
528  _ASSERT(m_Entry);
531  IEditSaver* saver = GetEditSaver(m_Handle);
532  if (saver) {
533  TAction::UndoInDB(*saver,old_id, m_Entry, m_Handle);
534  }
535  }
536 
537 
538 private:
540  TEditHandle m_Handle;
542 
543 };
544 
545 ///////////////////////////////////////////////////////////////////////////////
546 ///////////////////////////////////////////////////////////////////////////////
547 
548 
551 
552 #endif // OBJECTS_OBJMGR_IMPL___EDIT_COMMNADS_IMPL__HPP
static CRef< CScope > m_Scope
CAliasTypeInfoFunctions TFunc
Definition: aliasinfo.cpp:59
DBFunc< TEditHandle, CSeq_descr > TDBFunc
unique_ptr< TSeq_descr_Memeto > m_Memeto
CAddDescr_EditCommand(const TEditHandle &handle, CSeq_descr &descr)
virtual void Do(IScopeTransaction_Impl &tr)
CMemeto< CSeq_descr > TSeq_descr_Memeto
CRef< CSeq_descr > m_Descr
CRef< IScopeTransaction_Impl > m_Transaction
CCommandProcessor(const CCommandProcessor &)=delete
CMDReturn< CMD >::TReturn run(CMD *cmd)
CCommandProcessor & operator=(const CCommandProcessor &)=delete
virtual void Do(IScopeTransaction_Impl &tr)
CRef< CSeqdesc > m_Desc
DescEditAction< TEditHandle, add > TAction
TAction::TReturn TReturn
TReturn GetRet() const
CDesc_EditCommand(const TEditHandle &handle, const CSeqdesc &desc)
TTrait::TConstRef TConstRef
MemetoTrait< T, IsCRef< T >::value > TTrait
TStorage m_Storage
TTrait::TStorage TStorage
bool WasSet() const
TRef GetRefValue() const
TTrait::TRef TRef
CMemeto(const TEditHandle &handle)
void RestoreTo(const TEditHandle &handle)
CRef –.
Definition: ncbiobj.hpp:618
CSeq_entry_EditHandle m_Entry
virtual void Do(IScopeTransaction_Impl &tr)
RemoveAction< TEditHandle > TAction
CRemove_EditCommand(const TEditHandle &handle, CScope_Impl &scope)
unique_ptr< TMemeto > m_Memeto
CResetValue_EditCommand(const TEditHandle &handle)
MemetoFunctions< TEditHandle, T > TFunc
DBFunc< TEditHandle, T > TDBFunc
virtual void Do(IScopeTransaction_Impl &tr)
@Seq_descr.hpp User-defined methods of the data storage class.
Definition: Seq_descr.hpp:55
CSeq_entry_Handle –.
unique_ptr< TMemeto > m_Memeto
TMemeto::TStorage TStorage
MemetoFunctions< TEditHandle, T > TFunc
DBFunc< TEditHandle, T > TDBFunc
CSetValue_EditCommand(const TEditHandle &handle, TRef value)
virtual void Do(IScopeTransaction_Impl &tr)
CRef< IEditSaver > GetEditSaver() const
Definition: tse_info.hpp:949
Interface (functor) for object editing.
Edit Saver Interface.
Definition: edit_saver.hpp:72
ECallMode
This flag can be used for optimization purpose.
Definition: edit_saver.hpp:76
@ eUndo
The method is called when a modification has just been undone.
Definition: edit_saver.hpp:78
@ eDo
The method is called when a modification has just been done.
Definition: edit_saver.hpp:77
virtual void SetDescr(const CBioseq_Handle &, const CSeq_descr &, ECallMode)=0
virtual void AddDesc(const CBioseq_Handle &, const CSeqdesc &, ECallMode)=0
virtual void RemoveDesc(const CBioseq_Handle &, const CSeqdesc &, ECallMode)=0
virtual void AddDescr(const CBioseq_Handle &, const CSeq_descr &, ECallMode)=0
Description operations.
virtual void ResetDescr(const CBioseq_Handle &, ECallMode)=0
virtual void AddCommand(TCommand)=0
virtual void Commit()=0
virtual void AddEditSaver(IEditSaver *)=0
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define T(s)
Definition: common.h:230
#define DEFINE_CREF_TYPE(T)
IEditSaver * GetEditSaver(const Handle &handle)
static CS_COMMAND * cmd
Definition: ct_dynamic.c:26
#define true
Definition: bool.h:35
#define false
Definition: bool.h:36
char data[12]
Definition: iconv.c:80
#define Handle
Definition: ncbistd.hpp:119
const CBioObjectId & GetBioObjectId(void) const
Get unique object id.
TObjectType * GetPointer(void) THROWS_NONE
Get pointer,.
Definition: ncbiobj.hpp:998
bool ReferencedOnlyOnce(void) const THROWS_NONE
Check if object is referenced only once.
Definition: ncbiobj.hpp:475
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
#define NCBI_XOBJMGR_EXPORT
Definition: ncbi_export.h:1307
const TYPE & Get(const CNamedParameterList *param)
mdb_mode_t mode
Definition: lmdb++.h:38
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
EIPRangeType t
Definition: ncbi_localip.c:101
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
static TReturn GetRet(CMD *)
static void Reset(IEditSaver &saver, const Handle &handle, IEditSaver::ECallMode mode)
static void Set(IEditSaver &saver, const Handle &handle, const CSeq_descr &data, IEditSaver::ECallMode mode)
static void Add(IEditSaver &saver, const Handle &handle, const CSeq_descr &data, IEditSaver::ECallMode mode)
static void Set(IEditSaver &, const THandle &, TConstRef, IEditSaver::ECallMode)
TTrait::TConstRef TConstRef
TTrait::TRef TRef
static void Reset(IEditSaver &, const THandle &, IEditSaver::ECallMode)
MemetoTrait< T, IsCRef< T >::value > TTrait
static void Add(IEditSaver &saver, const Handle &handle, const CSeqdesc &desc, IEditSaver::ECallMode mode)
static void Remove(IEditSaver &saver, const Handle &handle, const CSeqdesc &desc, IEditSaver::ECallMode mode)
static void UndoInDB(IEditSaver &saver, const TEditHandle &handle, const CSeqdesc &desc)
static void DoInDB(IEditSaver &saver, const TEditHandle &handle, const CSeqdesc &desc)
static void Undo(const TEditHandle &handle, CSeqdesc &desc)
static TReturn Do(const TEditHandle &handle, CSeqdesc &desc)
static void DoInDB(IEditSaver &saver, const TEditHandle &handle, const CSeqdesc &desc)
static void Undo(const TEditHandle &handle, CSeqdesc &desc)
static TReturn Do(const TEditHandle &handle, CSeqdesc &desc)
static void UndoInDB(IEditSaver &saver, const TEditHandle &handle, const CSeqdesc &desc)
static void Set(const TEditHandle &handle, TStorage &data)
static bool IsSet(const TEditHandle &handle)
static void Reset(const TEditHandle &handle)
MemetoTrait< CSeq_descr, IsCRef< CSeq_descr >::value > TTrait
static TStorage Get(const TEditHandle &handle)
TTrait::TValue TValue
static bool IsSet(const TEditHandle &handle)
TTrait::TConstRef TConstRef
static void Set(const TEditHandle &handle, const TStorage &data)
MemetoTrait< T, IsCRef< T >::value > TTrait
static void Reset(const TEditHandle &handle)
static TStorage Get(const TEditHandle &handle)
TTrait::TStorage TStorage
static TStorage Store(TRef t)
static TRef Restore(TStorage t)
static TRef Restore(TStorage t)
static TStorage Store(TRef t)
static void Do(CScope_Impl &scope, const CSeq_entry_EditHandle &entry, const TEditHandle &handle)
static void DoInDB(IEditSaver &saver, const CSeq_entry_EditHandle &entry, const TEditHandle &handle)
static void Undo(CScope_Impl &scope, const CSeq_entry_EditHandle &entry, const TEditHandle &handle)
static void UndoInDB(IEditSaver &saver, const CBioObjectId &old_id, const CSeq_entry_EditHandle &entry, const TEditHandle &handle)
#define _ASSERT
Modified on Wed May 22 11:32:47 2024 by modify_doxy.py rev. 669887