1 #ifndef OBJTOOLS_READERS___READER_IDGEN__HPP 2 #define OBJTOOLS_READERS___READER_IDGEN__HPP 3 4 /* $Id: reader_idgen.hpp 542379 2017-07-31 13:06:45Z dicuccio $ 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: Frank Ludwig, Wratko Hlavina 30 * 31 * File Description: 32 * Repeat Masker file reader 33 * 34 */ 35 36 #include <corelib/ncbistd.hpp> 37 #include <corelib/ncbicntr.hpp> 38 39 #include <objects/general/User_field.hpp> 40 #include <objects/general/Object_id.hpp> 41 #include <objects/seqloc/Seq_loc.hpp> 42 #include <objects/seqfeat/Feat_id.hpp> 43 44 BEGIN_NCBI_SCOPE 45 BEGIN_SCOPE(objects) 46 47 48 // The IIdGenerator, ISeqIdResolver, and ITaxonomyResolver interfaces 49 // really belong higher up in the Toolkit. To be moved when convenient. 50 51 /// Templated interface for a generator of identifiers (IDs) of any type. 52 /// 53 template <class T> 54 class IIdGenerator : public CObject 55 { 56 public: 57 typedef IIdGenerator<T> TThisType; 58 59 /// Type for the ID, which is a template parameter. 60 /// 61 typedef T TId; 62 63 /// Enforce virtual destructor. ~IIdGenerator()64 virtual ~IIdGenerator() { } 65 66 /// Generates the "next" id. 67 /// 68 /// There is no specific constraint on the next id, except that 69 /// it be effectively, if not actually, unique. Uniqueness is not 70 /// a hard-core requirement, since wrap-around, limited reuse, 71 /// and probabilistic uniqueness guarantees are all permitted, 72 /// depending on the implementation details. 73 /// 74 /// @throws if an ID cannot be generated, perhaps because the generator 75 /// has been excausted. 76 /// 77 /// @note TId should be a CRef in the case of CObject instance, 78 /// an AutoPtr for other non copy-constructible objects, 79 /// and otherwise, should have value semantics. 80 /// 81 virtual TId GenerateId() = 0; 82 83 /// Identifies if the implementation happens to be thread-safe. 84 /// 85 /// By default, implementations are not assumed thread-safe. 86 /// 87 /// @return true if the implementation is known to be thread-safe, 88 /// false otherwise (may be thread-safe, but not assured). 89 /// 90 /// @note This function admits a client that will respond dynamically 91 /// to the issue of thread-safety. Callers might use this to 92 /// optimize away mutual exclusion in MT situations, but if they 93 /// require thread-safety, it is advisable to require an 94 /// ITheadSafeIdGenerator. 95 /// 96 /// In dynamic situations, if a user requires a thread-safe ID 97 /// generator, it is trivial to test for thread-safety, and 98 /// if necessary, wrap all calls with a mutex (perhaps 99 /// using a wrapper instance of this interface, that merely 100 /// checks a mutex and delegates to the original). The 101 /// wrapping could be made into a function, say, 102 /// CIRef<IIdGenerator<> > MakeThreadSafe(IIdGenerator<>&). 103 /// The design considered requiring the implementation 104 /// to be thread-safe always, but this imposes possibly 105 /// unnecessary requirements on all implementations. 106 /// Defining a separate interface or member function 107 /// to generate an ID with thread-safety was also considered, 108 /// but some algorithms are thread-safe depending only 109 /// on their injected dependencies, and calling different 110 /// interfaces/functions complicates their implementation 111 /// (e.g. may requires templates). 112 /// IsThreadSafe()113 virtual bool IsThreadSafe() { return false; } 114 }; 115 116 /// Thread-safe version of IIdGenerator. 117 /// 118 template <class T> 119 class IThreadSafeIdGenerator : public IIdGenerator<T> 120 { 121 public: 122 typedef IIdGenerator<T> TThisType; 123 124 /// This implementeation IS thread-safe. 125 /// Please do not override! 126 /// IsThreadSafe()127 bool IsThreadSafe() { return true; } 128 }; 129 130 /// Default implementation for a generator of identifiers, 131 /// as integers, mashalled as CFeat_id objects. 132 /// 133 class COrdinalFeatIdGenerator : public IThreadSafeIdGenerator< CRef<CFeat_id> > 134 { 135 public: COrdinalFeatIdGenerator()136 COrdinalFeatIdGenerator() { } 137 GenerateId()138 TId GenerateId() 139 { 140 CRef<CFeat_id> id(new CFeat_id); 141 id->SetLocal().SetId((int)(m_Id.Add(1) - 1)); 142 return id; 143 } 144 145 private: 146 CAtomicCounter m_Id; 147 }; 148 149 150 /// Interface for resolving a sequence identifier given 151 /// a textual representation. 152 class ISeqIdResolver : public CObject 153 { 154 public: 155 typedef ISeqIdResolver TThisType; 156 157 /// Enforce virtual destructor. ~ISeqIdResolver()158 virtual ~ISeqIdResolver() { } 159 160 /// Returns a normalized representation of a sequence 161 /// identifier, as Seq-id handle. 162 virtual CSeq_id_Handle ResolveSeqId(const string& id) const = 0; 163 }; 164 165 166 /// Default implementation of a Seq-id resolver, 167 /// which knows about FASTA-formatted sequence identifiers. 168 /// 169 class NCBI_XOBJREAD_EXPORT CFastaIdsResolver : public ISeqIdResolver 170 { 171 public: 172 CSeq_id_Handle ResolveSeqId(const string& id) const; 173 }; 174 175 176 END_SCOPE(objects) 177 END_NCBI_SCOPE 178 179 180 #endif // OBJTOOLS_READERS___READER_IDGEN__HPP 181