context.hpp (2265B)
1 #ifndef UUID_C2DF26B7_DE7D_47B8_BAEE_9F6EEBB12891 2 #define UUID_C2DF26B7_DE7D_47B8_BAEE_9F6EEBB12891 3 #pragma once 4 5 #include "item.hpp" 6 #include "../dumpable.hpp" 7 8 #include <boost/intrusive/set.hpp> 9 #include <string> 10 #include <map> 11 12 namespace Neptools 13 { 14 15 class Context : public ItemWithChildren 16 { 17 LIBSHIT_LUA_CLASS; 18 public: 19 Context(); 20 ~Context() override; 21 22 void Fixup() override; 23 24 template <typename T, typename... Args> 25 LIBSHIT_NOLUA Libshit::NotNull<Libshit::SmartPtr<T>> Create(Args&&... args) 26 { 27 return Libshit::MakeSmart<T>( 28 Item::Key{}, *this, std::forward<Args>(args)...); 29 } 30 31 Libshit::NotNull<LabelPtr> GetLabel(const std::string& name) const; 32 Libshit::NotNull<LabelPtr> CreateLabel(std::string name, ItemPointer ptr); 33 Libshit::NotNull<LabelPtr> CreateLabelFallback( 34 const std::string& name, ItemPointer ptr); 35 Libshit::NotNull<LabelPtr> CreateLabelFallback( 36 const std::string& name, FilePosition pos) 37 { return CreateLabelFallback(name, GetPointer(pos)); } 38 39 Libshit::NotNull<LabelPtr> CreateOrSetLabel(std::string name, ItemPointer ptr); 40 Libshit::NotNull<LabelPtr> GetOrCreateDummyLabel(std::string name); 41 42 Libshit::NotNull<LabelPtr> GetLabelTo(ItemPointer ptr); 43 Libshit::NotNull<LabelPtr> GetLabelTo(FilePosition pos) 44 { return GetLabelTo(GetPointer(pos)); } 45 46 Libshit::NotNull<LabelPtr> GetLabelTo( 47 FilePosition pos, const std::string& name); 48 49 ItemPointer GetPointer(FilePosition pos) const noexcept; 50 51 void Dispose() noexcept override; 52 53 protected: 54 void SetupParseFrom(Item& item); 55 56 private: 57 friend class Item; 58 59 // properties needed: stable pointers 60 using LabelsMap = boost::intrusive::set< 61 Label, 62 boost::intrusive::base_hook<LabelNameHook>, 63 boost::intrusive::constant_time_size<false>, 64 boost::intrusive::key_of_value<LabelKeyOfValue>>; 65 LabelsMap labels; 66 67 // properties needed: sorted 68 using PointerMap = std::map<FilePosition, Item*>; 69 PointerMap pmap; 70 }; 71 72 struct PrintLabelStruct { const Label* label; }; 73 std::ostream& operator<<(std::ostream& os, PrintLabelStruct label); 74 inline PrintLabelStruct PrintLabel(const LabelPtr& label) 75 { return {label.get()}; } 76 } 77 78 #endif