simulavr  1.1.0
avrfactory.cpp
Go to the documentation of this file.
1 /*
2  ****************************************************************************
3  *
4  * simulavr - A simulator for the Atmel AVR family of microcontrollers.
5  * Copyright (C) 2001, 2002, 2003 Klaus Rudolph
6  * Copyright (C) 2007 Onno Kortmann
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  ****************************************************************************
23  *
24  * $Id$
25  */
26 #include <map>
27 #include <iostream>
28 #include <cstdlib>
29 #include <vector>
30 #include "config.h"
31 #include "avrfactory.h"
32 #include "avrerror.h"
33 
34 using namespace std;
35 
36 typedef map<std::string, AvrFactory::AvrDeviceCreator> AVRDeviceMap;
37 
38 void AvrFactory::reg(const std::string name,
39  AvrDeviceCreator create) {
40  string devname(name);
41  for(unsigned int i = 0; i < devname.size(); i++)
42  devname[i] = tolower(devname[i]);
43  AVRDeviceMap & devmap = instance().devmap;
44  AVRDeviceMap::iterator i = devmap.find(devname);
45  if(i == devmap.end())
46  devmap[devname] = create;
47  else
48  avr_error("Duplicate device specification: %s", devname.c_str());
49 }
50 
52  string devname(in);
53  for(unsigned int i = 0; i < devname.size(); i++)
54  devname[i] = tolower(devname[i]);
55  if(devname == "unknown")
56  avr_error("Device type not specified, use -d | --device TYPE or insert '#include <avr/signature.h>' into your source to specify device signature");
57  AVRDeviceMap::iterator i = devmap.find(devname);
58  if(i == devmap.end())
59  avr_error("Invalid device specification: %s", in);
60 
61  return devmap[devname]();
62 }
63 
64 std::vector<std::string> &AvrFactory::supportedDevices() {
65  static std::vector<std::string> ret;
66  AVRDeviceMap & devmap = instance().devmap;
67 
68  for(AVRDeviceMap::iterator i = devmap.begin(); i != devmap.end(); i++)
69  ret.push_back(i->first);
70  return ret;
71 }
72 
74  static AvrFactory f;
75  return f;
76 }
77 
Basic AVR device, contains the core functionality.
Definition: avrdevice.h:66
map< std::string, AvrFactory::AvrDeviceCreator > AVRDeviceMap
Definition: avrfactory.cpp:36
STL namespace.
static std::vector< std::string > & supportedDevices()
Definition: avrfactory.cpp:64
Produces AVR devices.
Definition: avrfactory.h:39
static AvrFactory & instance()
Singleton class access.
Definition: avrfactory.cpp:73
#define avr_error(...)
Definition: avrerror.h:135
static void reg(const std::string name, AvrDeviceCreator create)
Register a creation static method with the factory.
Definition: avrfactory.cpp:38
AvrDevice * makeDevice(const char *config)
Definition: avrfactory.cpp:51