Salome HOME
bos #26457 Factorization of ORB initialization
[modules/kernel.git] / src / ArgvKeeper / ArgvKeeper.cxx
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ArgvKeeper.hxx"
21 #include <mutex>
22
23 namespace
24 {
25   std::recursive_mutex mutex; //!< mutex to protect access to static data
26   std::vector<std::string> argv_list; //!< cmd line arguments
27   bool isInitialized = false; //!< \c true if cmd line arguments were initialized
28 }
29
30 //! \brief Store cmd line arguments
31 void SetArgcArgv(int argc, char* argv[])
32 {
33   std::vector<std::string> tmp_list;
34   for (int i=0; i < argc; ++i)
35     tmp_list.push_back(std::string(argv[i]));
36   SetArgcArgv(tmp_list);
37 }
38
39 //! \overload void SetArgcArgv(int argc, char* argv[])
40 void SetArgcArgv(const std::vector<std::string>& argv)
41 {
42   std::lock_guard<std::recursive_mutex> g(mutex);
43   if (!isInitialized && !argv.empty())
44   {
45     isInitialized = true;
46     for (const std::string& value: argv) {
47       argv_list.push_back(value);
48     }
49   }
50 }
51
52 //! \brief Get cmd line arguments
53 std::vector<std::string> GetArgcArgv()
54 {
55   std::lock_guard<std::recursive_mutex> g(mutex);
56   return argv_list;
57 }
58
59 //! \brief Check if cmd line arguments were initialized
60 //!
61 //! In debug mode returns \c true if SetArgcArgv() was called or \c false otherwise.
62 //! In release mode always returns \c true.
63 bool ArgcArgvInitialized()
64 {
65 #ifdef DEBUG
66   return isInitialized;
67 #else
68   return true;
69 #endif
70 }