ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
inmemoryplugin.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2024 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
8
9#if !defined(ALIB_DOX)
10#if !defined (HPP_ALIB_CONFIG_INMEMORY_PLUGIN)
12#endif
13#endif // !defined(ALIB_DOX)
14
15
16
17namespace alib { namespace config {
18
19// #################################################################################################
20// interface
21// #################################################################################################
22
25, allocator ( externalMA ? externalMA : MonoAllocator::Create(8 * 1024) )
26, plugInName( allocator->EmplaceString(pName) )
27, entryTable( allocator )
28, sections ( allocator )
29, isMAOwner ( externalMA == nullptr )
30{
31 #if ALIB_DEBUG_MONOMEM
32 if( externalMA == nullptr )
33 allocator->LogDomain= A_CHAR("MA/CFG/IMPLGN");
34 #endif
35}
36
44
46{
47 entryTable.Clear();
48 sections .Clear();
49}
50
52{
53 if( sections.IsEmpty() )
55
56 auto sIt= sections.begin();
57 while( sIt != sections.end() )
58 {
59 if( sIt->Name().Equals<true, lang::Case::Ignore>( sectionName ) )
60 return &*sIt;
61 ++sIt;
62 }
63 return nullptr;
64}
65
66std::pair<InMemoryPlugin::Section*, bool>
68{
69 Section* s= const_cast<Section*>( SearchSection( sectionName ) );
70 if ( s != nullptr )
71 return std::make_pair( s, false );
72
73 return std::make_pair( createSection( sectionName ), true );
74}
75
76// #################################################################################################
77// Configuration plug-in interface implementation
78// #################################################################################################
79
80bool InMemoryPlugin::Load( Variable& variable, bool searchOnly )
81{
82 ALIB_ASSERT_WARNING( variable.Name().IsNotEmpty(), "CONFIG", "Empty name given" )
83 auto* entry= searchEntry( variable.Category(), variable.Name() );
84 if( entry != nullptr && !searchOnly )
85 ToVariable( *entry, variable );
86
87 return entry != nullptr;
88}
89
91{
92 auto it= entryTable.Find( EntryKey(variable.Category(), variable.Name()) );
93
94 // exists
95 if( it != entryTable.end() )
96 {
97 // delete if given variable is nulled
98 if ( variable.Size() == 0 )
99 {
100 auto handle= entryTable.Extract( it ); ALIB_ASSERT( !handle.IsEmpty() )
101 handle.Mapped().first->entries.Erase( handle.Mapped().second );
102 }
103 else
104 FromVariable( *it.Mapped().second, variable );
105
106 return true; // written (or deleted)
107 }
108
109 // does not exist, yet
110 if ( variable.Size() == 0 )
111 return false; // not written
112
113 // search section and create there
114 Section* section= SearchOrCreateSection( variable.Category() ).first;
115 auto* entry= createEntry( section, variable.Name() );
116 FromVariable( *entry, variable );
117 return true;
118}
119
120
121void InMemoryPlugin::ToVariable( Entry& entry, Variable& variable ) const
122{
123 ALIB_ASSERT( entry.Delim != '\0' || entry.AdditionalValues.Size() == 0)
124 if ( entry.Delim != '\0' ) variable.SetDelim ( entry.Delim );
125 if ( entry.FmtHints != FormatHints::None ) variable.SetFmtHints ( entry.FmtHints );
127 if ( entry.Comments .IsNotEmpty() ) variable.ReplaceComments ( entry.Comments );
128
129 if( entry.Value.IsNotNull() )
130 {
131 variable.Add( entry.Value );
132 for( const AString& val : entry.AdditionalValues )
133 variable.Add( val );
134 }
135}
136
137void InMemoryPlugin::FromVariable( Entry& entry, Variable& variable ) const
138{
139 // copy attributes
140 entry.Delim= variable.Delim();
141 entry.FmtHints= variable.FmtHints();
143
144 if ( entry.Comments.IsEmpty() ) // do not overwrite comments
145 entry.Comments._( variable.Comments() );
146
147 // adjust size of value array and copy values
148 integer varSize= variable.Size();
149 auto valueIt= entry.SetValueCount( varSize );
150
151 entry.Value.Reset( variable.GetString( 0 ) );
152 int idx= 1;
153 while( valueIt != entry.AdditionalValues.end() )
154 {
155 valueIt->Reset( variable.GetString( idx++ ) );
156 ++valueIt;
157 }
158}
159
160
161// #################################################################################################
162// CLIArgs Iterator
163// #################################################################################################
164//! @cond NO_DOX
165class InMemoryPluginIteratorImpl : public ConfigurationPlugin::Iterator
166{
167 InMemoryPlugin& parent;
170
171
172 public:
173
174 InMemoryPluginIteratorImpl( InMemoryPlugin& plugin, const String& sectionName )
175 : parent(plugin)
176 {
177 if( (section= const_cast<InMemoryPlugin::Section*>( parent.SearchSection( sectionName ))) !=nullptr )
178 actualEntry= section->entries.begin();
179 }
180
181 virtual ~InMemoryPluginIteratorImpl() override
182 {}
183
184 virtual bool Next( Variable& variable ) override
185 {
186 if( section == nullptr || actualEntry == section->entries.end() )
187 return false;
188
189 variable.Declare( section->Name(), actualEntry->Name() );
190 parent.ToVariable( *actualEntry, variable );
191 ++actualEntry;
192 return true;
193 }
194};
195
196ConfigurationPlugin::Iterator* InMemoryPlugin::GetIterator( const String& sectionName )
197{
198 return new InMemoryPluginIteratorImpl( *this, sectionName );
199}
200//! @endcond
201
202
203
204}} // namespace [alib::config]
TValueList::Iterator SetValueCount(integer requestedSize)
TEntryList entries
The list of variables of the section.
virtual ALIB_API void ToVariable(Entry &entry, Variable &variable) const
ALIB_API Entry * createEntry(Section *section, const String &name)
ALIB_API std::pair< Section *, bool > SearchOrCreateSection(const String &sectionName)
virtual ALIB_API bool Load(Variable &variable, bool searchOnly=false) override
virtual ALIB_API Iterator * GetIterator(const String &sectionName) override
virtual Section * createSection(const String &sectionName)
monomem::HashMap< EntryKey, std::pair< Section *, TEntryList::Iterator >, EntryKey::Hash, EntryKey::EqualTo > entryTable
virtual ALIB_API void Clear()
virtual ALIB_API void FromVariable(Entry &entry, Variable &variable) const
virtual ALIB_API bool Store(Variable &variable) override
ALIB_API const Section * SearchSection(const String &sectionName)
virtual ALIB_API ~InMemoryPlugin() override
Entry * searchEntry(const String &section, const String &name)
ALIB_API InMemoryPlugin(String pName, MonoAllocator *externalMA=nullptr)
void SetFmtHints(FormatHints hints)
Definition variable.hpp:538
integer Size() const
Definition variable.hpp:708
void ReplaceFormatAttrAlignment(const String &newValue)
Definition variable.hpp:694
const String & FormatAttrAlignment() const
Definition variable.hpp:549
void ReplaceComments(const String &newValue)
Definition variable.hpp:668
FormatHints FmtHints() const
Definition variable.hpp:527
ALIB_API void Add(const String &value)
Definition variable.cpp:223
character Delim() const
Definition variable.hpp:506
const String & Comments() const
Definition variable.hpp:558
const String & Name() const
Definition variable.hpp:496
void SetDelim(character delim)
Definition variable.hpp:517
const String & Category() const
Definition variable.hpp:487
const String & GetString(int idx=0)
Definition variable.hpp:780
TAString & _(const TString< TChar > &src, integer regionStart, integer regionLength=MAX_LEN)
Definition astring.hpp:1056
constexpr bool IsEmpty() const
Definition string.hpp:414
constexpr bool IsNotEmpty() const
Definition string.hpp:420
constexpr bool IsNotNull() const
Definition string.hpp:402
#define A_CHAR(STR)
#define ALIB_ASSERT_WARNING(cond,...)
Definition alib.hpp:985
#define ALIB_ASSERT(cond)
Definition alib.hpp:983
static ALIB_FORCE_INLINE void Destruct(T *object)
Definition alib.cpp:57
constexpr CString EmptyString()
Definition cstring.hpp:502
strings::TString< character > String
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286