ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
admb_messages.h
Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  *
00007  * ADModelbuilder and associated libraries and documentations are
00008  * provided under the general terms of the "BSD" license.
00009  *
00010  * License:
00011  *
00012  * Redistribution and use in source and binary forms, with or without
00013  * modification, are permitted provided that the following conditions are
00014  * met:
00015  *
00016  * 1. Redistributions of source code must retain the above copyright
00017  * notice, this list of conditions and the following disclaimer.
00018  *
00019  * 2.  Redistributions in binary form must reproduce the above copyright
00020  * notice, this list of conditions and the following disclaimer in the
00021  * documentation and/or other materials provided with the distribution.
00022  *
00023  * 3.  Neither the name of the  University of California, Otter Research,
00024  * nor the ADMB Foundation nor the names of its contributors may be used
00025  * to endorse or promote products derived from this software without
00026  * specific prior written permission.
00027  *
00028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00029  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00030  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00031  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00032  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00033  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00034  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00035  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00036  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00037  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00038  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00039  *
00040  */
00041 #ifndef __ADMB_MESSAGES_H__
00042 #define __ADMB_MESSAGES_H__
00043 #include <iostream>
00044 #include <fvar.hpp>
00045 
00046 #define ADMB_ERROR(x) admb::messages::error(x);
00047 #define ADMB_ARRAY_BOUNDS_ERROR(message,function,lower_bounds,upper_bounds,\
00048 index) admb::messages::array_bounds_error(message,function,lower_bounds,\
00049 upper_bounds,index);
00050 
00051 namespace admb
00052 {
00053 const int ARRAY_BOUNDS_ERROR = 42;
00054 
00058 class messages
00059 {
00060 public:
00064   messages() {;}
00068   messages(const messages&);
00072   virtual ~messages() {;}
00073 
00074 public:
00078   static
00079   void error(const adstring& message)
00080   {
00081     error(message, 1);
00082   }
00086   static
00087   void error(const int error_code)
00088   {
00089     adstring message  = "Error: ";
00090     switch (error_code)
00091     {
00092     case 1:
00093       message += "allocating memory in dmatrix contructor";
00094       break;
00095     case 2:
00096       message +=
00097         "attempting to access non-allocated ivector in ivector::operator()";
00098       break;
00099     default:
00100       break;
00101     };
00102     error(message, error_code);
00103   }
00107   static
00108   void error(const adstring& message, const adstring& function_name,
00109     const int error_code)
00110   {
00111     adstring m = message;
00112     if (function_name.size() > 0)
00113     {
00114       m += " in " + function_name;
00115     }
00116     error(m, error_code);
00117   }
00121   static
00122   void array_bounds_error(const adstring& message,
00123                           const adstring& function,
00124                           const int lower_bounds,
00125                           const int upper_bounds,
00126                           const int index)
00127   {
00128     //tools99/string1.cpp
00129     //cerr << "First index out of bounds in adstring::operator () (int,int)\n"
00130     //<< "Index value was " << i << " The size of this adstring is "
00131     //<< shape->size() << "\n";
00132     //cerr << "Index out of bounds in adstring::operator () (const int)\n"
00133     //<< "Index value was " << i << " The size of this adstring is "
00134     //<< shape->size() << "\n";
00135     //tools99/string5.cpp
00136     //cerr << "Error index too low in adstring& operator [] (int i)" << endl;
00137     //cerr << "value was " << i << " minimum valid index is "
00138     /*
00139     adstring m = message + " in \"" + function +  "\"\n"
00140                  + adstring("Index value was ")
00141                  + str(index)
00142                  + " The size of this adstring is "
00143                  + str(upper_bounds);
00144     */
00145     adstring m = adstring("Error: Invalid index ")
00146                  + str(index)
00147                  + " used for array range ["
00148                  + str(lower_bounds)
00149                  + ", "
00150                  + str(upper_bounds)
00151                  + "] in \"" + function +  "\".\n"
00152                  + message + "\n";
00153     error(m, ARRAY_BOUNDS_ERROR);
00154   }
00158   static
00159   void error(const adstring& message, const int error_code)
00160   {
00161     cerr << message << '\n';
00162     ad_exit(error_code);
00163   }
00164 };
00165 }
00166 #endif