aGrUM  0.14.2
operatorRegister4MultiDim_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 
30 # include <agrum/agrum.h>
31 
33 
34 namespace gum {
35 
36  // adds a new entry into the register
37  template < typename GUM_SCALAR >
39  const std::string& operation_name,
40  const std::string& type1,
41  const std::string& type2,
43  // insert the new entry
44  OperatorSet* theset;
45 
46  if (!__set.exists(operation_name)) {
47  theset = __set.insert(operation_name, new OperatorSet).second;
48 # ifdef GUM_DEBUG_MODE
49  // for debugging purposes, we should inform the aGrUM's debugger that
50  // the hashtable contained within the OperatorRegister4MultiDim will be
51  // removed at the end of the program's execution.
52  __debug__::__inc_deletion(
53  "HashTable", __FILE__, __LINE__, "destructor of", (void*)theset);
54 # endif /* GUM_DEBUG_MODE */
55  } else {
56  theset = __set[operation_name];
57  }
58 
59  std::pair< std::string, std::string > thepair(type1, type2);
60 
61  theset->insert(thepair, newFunction);
62  }
63 
64  // removes a given entry from the register
65  template < typename GUM_SCALAR >
67  const std::string& operation_name,
68  const std::string& type1,
69  const std::string& type2) {
70  if (!__set.exists(operation_name)) return;
71 
72  OperatorSet* theset = __set[operation_name];
73 
74  theset->erase(std::pair< std::string, std::string >(type1, type2));
75  }
76 
77  // indicates whether a given entry exists in the register
78  template < typename GUM_SCALAR >
80  const std::string& operation_name,
81  const std::string& type1,
82  const std::string& type2) const {
83  if (!__set.exists(operation_name)) return false;
84  OperatorSet* theset = __set[operation_name];
85  return theset->exists(std::pair< std::string, std::string >(type1, type2));
86  }
87 
90  template < typename GUM_SCALAR >
93  const std::string& operation_name,
94  const std::string& type1,
95  const std::string& type2) const {
96  OperatorSet* theset = __set[operation_name];
97  return (*theset)[std::pair< std::string, std::string >(type1, type2)];
98  }
99 
100  // a named constructor that constructs one and only one Register per data
101  // type
102  template < typename GUM_SCALAR >
103  OperatorRegister4MultiDim< GUM_SCALAR >&
105  static OperatorRegister4MultiDim< GUM_SCALAR >* container = nullptr;
106  static bool first = true;
107 
108  if (first) {
109  first = false;
110  container = new OperatorRegister4MultiDim< GUM_SCALAR >;
111 
112 # ifdef GUM_DEBUG_MODE
113  // for debugging purposes, we should inform the aGrUM's debugger that
114  // the hashtable contained within the OperatorRegister4MultiDim will be
115  // removed at the end of the program's execution.
116  __debug__::__inc_deletion("HashTable",
117  __FILE__,
118  __LINE__,
119  "destructor of",
120  (void*)&container->__set);
121 # endif /* GUM_DEBUG_MODE */
122  }
123 
124  return *container;
125  }
126 
127 
128  // Default constructor: creates an empty register
129  template < typename GUM_SCALAR >
131 
132  // destructor
133  template < typename GUM_SCALAR >
135  // remove all the sets
137  __set.beginSafe();
138  iter != __set.endSafe();
139  ++iter)
140  delete iter.val();
141  }
142 
143  // a function to more easily register new operators in MultiDims
144  template < typename GUM_SCALAR >
145  void registerOperator(
146  const std::string& operation_name,
147  const std::string& type1,
148  const std::string& type2,
151  operation_name, type1, type2, function);
152  }
153 
154 } /* namespace gum */
155 
156 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
A container for registering binary functions on multiDimImplementations.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
void registerOperator(const std::string &operation_name, const std::string &type1, const std::string &type2, typename OperatorRegister4MultiDim< GUM_SCALAR >::OperatorPtr function)
A function to more easily register new operators in MultiDims.
HashTableIteratorSafe< Key, Val > iterator_safe
Types for STL compliance.
Definition: hashTable.h:692
bool exists(const std::string &operation_name, const std::string &type1, const std::string &type2) const
Indicates whether a given entry exists in the register.
void erase(const std::string &operation_name, const std::string &type1, const std::string &type2)
removes a given entry from the register
void insert(const std::string &operation_name, const std::string &type1, const std::string &type2, OperatorPtr newFunction)
adds a new entry into the register
static OperatorRegister4MultiDim< GUM_SCALAR > & Register()
A named constructor that constructs one and only one Register per data type.
MultiDimImplementation< GUM_SCALAR > *(* OperatorPtr)(const MultiDimImplementation< GUM_SCALAR > *, const MultiDimImplementation< GUM_SCALAR > *)
The type of functions used by the register.
OperatorRegister4MultiDim()
Default constructor: creates an empty register.
OperatorPtr get(const std::string &operation_name, const std::string &type1, const std::string &type2) const
returns the specialized operator assigned to a given pair of MultiDimImplementations ...
~OperatorRegister4MultiDim()
Destructor.