Sierra Toolkit  Version of the Day
ProductRegistry.cpp
00001 // Copyright (c) 2013, Sandia Corporation.
00002 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00003 // the U.S. Government retains certain rights in this software.
00004 //
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are
00007 // met:
00008 //
00009 //     * Redistributions of source code must retain the above copyright
00010 //       notice, this list of conditions and the following disclaimer.
00011 //
00012 //     * Redistributions in binary form must reproduce the above
00013 //       copyright notice, this list of conditions and the following
00014 //       disclaimer in the documentation and/or other materials provided
00015 //       with the distribution.
00016 //
00017 //     * Neither the name of Sandia Corporation nor the names of its
00018 //       contributors may be used to endorse or promote products derived
00019 //       from this software without specific prior written permission.
00020 //
00021 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00024 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00025 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00027 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00028 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00029 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00030 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00031 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 //
00033 
00034 #include <stk_util/environment/product_registry.h>
00035 #include <stk_util/environment/ProductRegistry.hpp>
00036 #ifdef STK_BUILT_IN_SIERRA
00037 #include <stk_util/environment/stk_version.hpp>
00038 #else
00039 #define STK_VERSION "0.1a"
00040 #endif
00041 
00042 namespace stk_classic {
00043 
00044 const std::string
00045 ProductRegistry::NAME = "Name";
00046 
00047 const std::string
00048 ProductRegistry::TITLE = "Title";
00049 
00050 const std::string
00051 ProductRegistry::VERSION = "Version";
00052 
00053 const std::string
00054 ProductRegistry::QUALIFIER = "Qualifier";
00055 
00056 const std::string
00057 ProductRegistry::BUILD_TIME = "Build Time";
00058 
00059 const std::string
00060 ProductRegistry::EXECUTABLE = "Executable";
00061 
00062 const std::string
00063 ProductRegistry::CONTACT = "Contact";
00064 
00065 const std::string
00066 ProductRegistry::ERROR = "Error";
00067 
00068 const std::string
00069 ProductRegistry::PRODUCT_TYPE = "Type";
00070 
00071 const std::string
00072 ProductRegistry::REGION_TITLE = "Region Title";
00073 
00074 const std::string
00075 ProductRegistry::BANNER_DETAIL = "Banner Detail";
00076 
00077 const std::string
00078 ProductRegistry::COPYRIGHT = "Copyright";
00079 
00080 const std::string
00081 ProductRegistry::PRODUCT_TYPE_REGION = "Region";
00082 
00083 
00084 ProductRegistry &
00085 ProductRegistry::instance()
00086 {
00087   static ProductRegistry s_productRegistry;
00088 
00089   return s_productRegistry;
00090 }
00091 
00092 
00093 const char *
00094 ProductRegistry::version()
00095 {
00096   // STK_VERSION should be a build-time define (i.e. -D flag) passed on
00097   // the compilation command line
00098   static const char *s_version = STK_VERSION;
00099   
00100   return s_version;
00101 }
00102 
00103 
00104 ProductRegistry::AttributeMap &
00105 ProductRegistry::addTPL(
00106   const std::string &   name,
00107   const std::string &   version,
00108   const std::string &   qualifier)
00109 {
00110   std::pair<ProductMap::iterator, bool> iit = m_productMap.insert(std::make_pair(name, AttributeMap()));
00111   ProductMap::iterator it = iit.first;
00112   if (iit.second) {
00113     (*it).second[NAME] = name.c_str();
00114     (*it).second[VERSION] = version;
00115     (*it).second[QUALIFIER] = qualifier;
00116   }
00117   else {
00118     std::string &current_version = (*it).second[VERSION];
00119     std::string &current_qualifer = (*it).second[QUALIFIER];
00120     if (current_version.empty())
00121       current_version = version;
00122     if (current_qualifer.empty())
00123       current_qualifer = qualifier;
00124     if (current_version != version || current_qualifer != qualifier) {
00125       (*it).second[ERROR] = std::string("Product registration of ") + (*it).first + " version/qualifier conflict, "
00126         + " initially " + (*it).second[VERSION] + "/" + (*it).second[QUALIFIER]
00127         + " tried to change to " + version + "/" + qualifier;
00128       setRegistryInvalid();
00129     }
00130   }
00131 
00132   return (*it).second;
00133 }
00134 
00135 
00136 ProductRegistry::AttributeMap &
00137 ProductRegistry::addProduct(const std::string & name)
00138 {
00139   std::pair<ProductMap::iterator, bool> iit = m_productMap.insert(std::make_pair(name, AttributeMap()));
00140   ProductMap::iterator it = iit.first;
00141   if (iit.second) {
00142     (*it).second[NAME] = name.c_str();
00143   }
00144 
00145   return (*it).second;
00146 }
00147 
00148 
00149 ProductRegistry::AttributeMap &
00150 ProductRegistry::addRegion(
00151   const std::string & name)
00152 {
00153   AttributeMap &attribute_map = addProduct(name);
00154   attribute_map[ProductRegistry::PRODUCT_TYPE] = ProductRegistry::PRODUCT_TYPE_REGION;
00155   attribute_map[ProductRegistry::VERSION] = ProductRegistry::version();
00156 
00157   return attribute_map;
00158 }
00159 
00160 
00161 ProductRegistry::AttributeMap &
00162 ProductRegistry::getProductAttributeMap(
00163   const std::string & name)
00164 {
00165   return m_productMap[name];
00166 }
00167 
00168 
00169 const std::string &
00170 ProductRegistry::getProductAttribute(
00171   const std::string & name,
00172   const std::string & attribute) const
00173 {
00174   return m_productMap[name][attribute];
00175 }
00176 
00177 
00178 std::string &
00179 ProductRegistry::getProductAttribute(
00180   const std::string & name,
00181   const std::string & attribute)
00182 {
00183   return m_productMap[name][attribute];
00184 }
00185 
00186 
00187 void
00188 ProductRegistry::setProductAttribute(
00189   const std::string & name,
00190   const std::string & attribute,
00191   const std::string & value)
00192 {
00193   m_productMap[name][attribute] = value;
00194 }
00195 
00196 } // namespace stk_classic
00197 
00198 extern "C" {
00199 
00200 void
00201 product_registry_add(
00202   const char *    name )
00203 {
00204   stk_classic::ProductRegistry::instance().addProduct(name ? name : "<unknown>");
00205 }
00206 
00207 
00208 void
00209 product_registry_add_tpl(
00210   const char *    name,
00211   const char *    version,
00212   const char *    qualifier )
00213 {
00214   stk_classic::ProductRegistry::instance().addTPL(name ? name : "<unknown>", version ? version : "", qualifier ? qualifier : "");
00215 }
00216 
00217 
00218 size_t
00219 product_registry_size()
00220 {
00221   return stk_classic::ProductRegistry::instance().getProductMap().size();
00222 }
00223 
00224 } // extern "C"
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines