# ai_error.hpp

Go to the documentation of this file.

    00001 /* $Id: ai_error.hpp 17248 2009-08-21 20:21:05Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #ifndef AI_ERROR_HPP
    00013 #define AI_ERROR_HPP
    00014
    00015 #include "ai_object.hpp"
    00016 #include <map>
    00017
    00023 #define EnforcePrecondition(returnval, condition)               \
    00024   if (!(condition)) {                                           \
    00025     AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);   \
    00026     return returnval;                                           \
    00027   }
    00028
    00035 #define EnforcePreconditionCustomError(returnval, condition, error_code)   \
    00036   if (!(condition)) {                                                      \
    00037     AIObject::SetLastError(error_code);                                    \
    00038     return returnval;                                                      \
    00039   }
    00040
    00044 class AIError : public AIObject {
    00045 public:
    00046   static const char *GetClassName() { return "AIError"; }
    00047
    00051   enum ErrorCategories {
    00052     ERR_CAT_NONE = 0,
    00053     ERR_CAT_GENERAL,
    00054     ERR_CAT_VEHICLE,
    00055     ERR_CAT_STATION,
    00056     ERR_CAT_BRIDGE,
    00057     ERR_CAT_TUNNEL,
    00058     ERR_CAT_TILE,
    00059     ERR_CAT_SIGN,
    00060     ERR_CAT_RAIL,
    00061     ERR_CAT_ROAD,
    00062     ERR_CAT_ORDER,
    00063     ERR_CAT_MARINE,
    00064
    00069     ERR_CAT_BIT_SIZE = 8,
    00070   };
    00071
    00075   enum ErrorMessages {
    00077     ERR_NONE = ERR_CAT_NONE << ERR_CAT_BIT_SIZE,  // []
    00079     ERR_UNKNOWN,                                  // []
    00081     ERR_PRECONDITION_FAILED,                      // []
    00083     ERR_PRECONDITION_STRING_TOO_LONG,             // []
    00085     ERR_NEWGRF_SUPPLIED_ERROR,                    // []
    00086
    00088     ERR_GENERAL_BASE = ERR_CAT_GENERAL << ERR_CAT_BIT_SIZE,
    00089
    00091     ERR_NOT_ENOUGH_CASH,                          // [STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY]
    00092
    00094     ERR_LOCAL_AUTHORITY_REFUSES,                  // [STR_ERROR_LOCAL_AUTHORITY_REFUSES_TO_ALLOW_THIS]
    00095
    00097     ERR_ALREADY_BUILT,                            // [STR_ERROR_ALREADY_BUILT, STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST]
    00098
    00100     ERR_AREA_NOT_CLEAR,                           // [STR_ERROR_BUILDING_MUST_BE_DEMOLISHED, STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST, STR_ERROR_MUST_DEMOLISH_RAILROAD, STR_ERROR_MUST_DEMOLISH_AIRPORT_FIRST, STR_ERROR_MUST_DEMOLISH_CARGO_TRAM_STATION_FIRST, STR_ERROR_MUST_DEMOLISH_TRUCK_STATION_FIRST, STR_ERROR_MUST_DEMOLISH_PASSENGER_TRAM_STATION_FIRST, STR_ERROR_MUST_DEMOLISH_BUS_STATION_FIRST, STR_ERROR_BUOY_IN_THE_WAY, STR_ERROR_MUST_DEMOLISH_DOCK_FIRST, STR_ERROR_UNMOVABLE_OBJECT_IN_THE_WAY, STR_ERROR_COMPANY_HEADQUARTERS_IN, STR_ERROR_OBJECT_IN_THE_WAY, STR_ERROR_MUST_REMOVE_ROAD_FIRST, STR_ERROR_MUST_REMOVE_RAILROAD_TRACK, STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST, STR_ERROR_MUST_DEMOLISH_TUNNEL_FIRST, STR_ERROR_EXCAVATION_WOULD_DAMAGE]
    00101
    00103     ERR_OWNED_BY_ANOTHER_COMPANY,                 // [STR_ERROR_AREA_IS_OWNED_BY_ANOTHER, STR_ERROR_OWNED_BY]
    00104
    00106     ERR_NAME_IS_NOT_UNIQUE,                       // [STR_ERROR_NAME_MUST_BE_UNIQUE]
    00107
    00109     ERR_FLAT_LAND_REQUIRED,                       // [STR_ERROR_FLAT_LAND_REQUIRED]
    00110
    00112     ERR_LAND_SLOPED_WRONG,                        // [STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION]
    00113
    00115     ERR_VEHICLE_IN_THE_WAY,                       // [STR_ERROR_TRAIN_IN_THE_WAY, STR_ERROR_ROAD_VEHICLE_IN_THE_WAY, STR_ERROR_SHIP_IN_THE_WAY, STR_ERROR_AIRCRAFT_IN_THE_WAY]
    00116
    00118     ERR_SITE_UNSUITABLE,                          // [STR_ERROR_SITE_UNSUITABLE]
    00119
    00121     ERR_TOO_CLOSE_TO_EDGE,                        // [STR_ERROR_TOO_CLOSE_TO_EDGE_OF_MAP]
    00122
    00124     ERR_STATION_TOO_SPREAD_OUT,                   // [STR_ERROR_STATION_TOO_SPREAD_OUT]
    00125   };
    00126
    00132   static ErrorCategories GetErrorCategory();
    00133
    00138   static AIErrorType GetLastError();
    00139
    00144   static char *GetLastErrorString();
    00145
    00146 #ifndef EXPORT_SKIP
    00147
    00153   static AIErrorType StringToError(StringID internal_string_id);
    00154
    00161   static void RegisterErrorMap(StringID internal_string_id, AIErrorType ai_error_msg);
    00162
    00169   static void RegisterErrorMapString(AIErrorType ai_error_msg, const char *message);
    00170 #endif /* EXPORT_SKIP */
    00171
    00172 private:
    00173   typedef std::map<StringID, AIErrorType> AIErrorMap;
    00174   typedef std::map<AIErrorType, const char *> AIErrorMapString;
    00175
    00176   static AIErrorMap error_map;
    00177   static AIErrorMapString error_map_string;
    00178 };
    00179
    00180 #endif /* AI_ERROR_HPP */