NCBI C++ ToolKit
macro_argument.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: macro_argument.cpp 46407 2021-04-20 18:00:38Z asztalos $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Authors: Vladislav Evgeniev
27 */
28 
29 
30 #include <ncbi_pch.hpp>
32 
34 USING_SCOPE(macro);
35 
36 CArgument::CArgument(const SArgMetaData &arg, bool enabled, bool shown)
37  : m_Arg(arg)
38 {
39  m_Value.Attach(this);
40  m_Enabled = enabled;
41  m_Shown = shown;
42 }
43 
44 void CArgument::Attach(IArgumentSubscriber* subscriber) const
45 {
46  assert(subscriber);
47  m_Subscribers.push_back(subscriber);
48 }
49 
50 void CArgument::Detach(IArgumentSubscriber* subscriber) const
51 {
52  assert(subscriber);
53  TSubscribersVector::iterator itToBeDetached;
54  for (itToBeDetached = m_Subscribers.begin(); itToBeDetached != m_Subscribers.end(); ++itToBeDetached) {
55  if (subscriber != *itToBeDetached)
56  continue;
57 
58  m_Subscribers.erase(itToBeDetached);
59  break;
60  }
61 }
62 
64 {
65  for (auto subscriber : m_Subscribers)
66  subscriber->Update(*this);
67 }
68 
69 // CArgumentList
70 void CArgumentList::Add(const SArgMetaData& arg, bool enabled, bool shown)
71 {
72  CRef<CArgument> argument(new CArgument(arg, enabled, shown));
73  m_Args.push_back(argument);
74  m_ArgsNew.emplace(arg.m_Name, argument);
75  argument->Attach(this);
76 }
77 
78 CArgument& CArgumentList::operator[](const string &arg_name)
79 {
80  auto it = m_ArgsNew.find(arg_name);
81  if ( it != m_ArgsNew.end()) {
82  return *(it->second);
83  }
84  NCBI_THROW(CException, eUnknown, "Argument '" + arg_name + "' not found!");
85 }
86 
87 const CArgument& CArgumentList::operator[](const string &arg_name) const
88 {
89  const auto it = m_ArgsNew.find(arg_name);
90  if (it != m_ArgsNew.end()) {
91  return *(it->second);
92  }
93  NCBI_THROW(CException, eUnknown, "Argument '" + arg_name + "' not found!");
94 }
95 
96 size_t CArgumentList::count(const string& arg_name) const
97 {
98  return m_ArgsNew.count(arg_name);
99 }
100 
102 {
103  assert(subscriber);
104  m_Subscribers.push_back(subscriber);
105 }
106 
108 {
109  assert(subscriber);
110  TSubscribersVector::iterator itToBeDetached;
111  for (itToBeDetached = m_Subscribers.begin(); itToBeDetached != m_Subscribers.end(); ++itToBeDetached) {
112  if (subscriber != *itToBeDetached)
113  continue;
114 
115  m_Subscribers.erase(itToBeDetached);
116  break;
117  }
118 }
119 
121 {
122  for (auto subscriber : m_Subscribers)
123  (*subscriber)(*this, arg);
124 }
125 
127 {
128 }
129 
void Notify(CArgument &arg)
TArguments m_Args
TSubscribersVector m_Subscribers
void Attach(FOnArgumentChanged subscriber) const
map< string, CRef< CArgument > > m_ArgsNew
Same information as m_Args, used for fast searching.
CArgument & operator[](const string &arg_name)
virtual ~CArgumentList()
void Detach(FOnArgumentChanged subscriber) const
void Add(const macro::SArgMetaData &arg, bool enabled, bool shown)
size_t count(const string &arg_name) const
CArgument(const macro::SArgMetaData &arg, bool enabled=true, bool shown=true)
TSubscribersVector m_Subscribers
void Attach(IArgumentSubscriber *subscriber) const
void Detach(IArgumentSubscriber *subscriber) const
const_iterator end() const
Definition: map.hpp:152
const_iterator find(const key_type &key) const
Definition: map.hpp:153
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
USING_SCOPE(macro)
void(* FOnArgumentChanged)(CArgumentList &, CArgument &)
#define assert(x)
Definition: srv_diag.hpp:58
Modified on Tue May 07 08:14:26 2024 by modify_doxy.py rev. 669887