here is a bit of code from AV, don't think of this as c++, really it should be thought of as an AV built ins scripting DSL that happens to get compiled by the c++ compiler.
void somemethod() { class PhilosophersStone { public: PhilosophersStone(vector<uint32_t> buildings, Project&; proj) { buildings.push_back(proj.Id()); proj.OnUpdateTurn = OnUpdateTurn; } static void OnUpdateTurn(Council& council, Player& player, Territory& territory, ControlModel& controlModel) { controlModel._commandAndControl += 5; } } philosophersStone(philosophy._buildings, PROJECT_TABLE.CreateProject(5000, 10000, "Philosophers Stone", "its super cool and stuff", list_of( PREREQUISITE_TABLE.TechRequisite(philosophy)))); }
as you look at this you may notice that ive managed to get way to much code into a class declaration, yes infact the last ';' you see there is the ';' you normally see on class rawr {}; <---- that one. You might be saying to your self "holy shit wtf is he doing", well one of the features inherited from C is that you can create an instance of a struct/class directly after the definition of the type. well since this is c++, i can add a constructor and put that sucker on the end. Next up I create a project with mostly simple parameters but wait!!!! wtf is this list_of thing? list_of comes from boost.Assignment and it allows me to define a collection inline. the somewhat odd syntax for multiple elements would be list_of(5)(4)(3)(2)(1), just a bit of an eye-full there with all the overloaded operator() action. Still this once again demonstrates to me how vastly superior C++ is to every other language on the planet(that includes spoken languages).
One of the more interesting features in C++ template(IMHO) is being able to bake a compile time constant value directly into a template instance. One nice thing about function pointers is that they have a constant for an address. I'm not 100% sure what I can really use this for but it certainly looks cool! Of certain interest is template deduction on a function pointer, I was very surprised(pleasantly) to find this code compile.
#include<iostream> #include<string> using namespace std; template<typename T, T (*F)(T)> class rawrg { public: rawrg(T val) { F(val); } }; template<typename T> T method(T t) { cout<< t << endl; return t; } int main() { rawrg<const string&, &method> rawr("trivial"); rawrg<float, &method> rawrg(5.5); }
This is a snip of code from AV and I am about to tell you once again, the single coolest thing in C++ is member pointers(function and data). oh and local class declarations are also THE shit. I suppose this isnt neccisarily the most readable code in the world but.... the most awsome things rarely are. Now if only synergy supported this :d
void MessageBox::SendMessage(ArchverseCore::Game::DiplomaticMessage &message) { Player sendingPlayer, recivingPlayer; if(PLAYER_TABLE.Exists(message._senderId) && PLAYER_TABLE.Exists(message._targetId)) { class PushMessage { private: uint32_t _messageId; vector<uint32_t>& (MessageBox::*_target)(); public: PushMessage(uint32_t messageId, vector<uint32_t>& (MessageBox::*target)()) : _messageId(messageId), _target(target) {} bool operator()(Player& player) { (player.Messages().*_target)().push_back(_messageId); return true; } }; MESSAGE_TABLE.SaveMessage(message); PLAYER_TABLE.LockedTransaction(message._senderId, PushMessage(message._id, &MessageBox::Sent)); PLAYER_TABLE.LockedTransaction(message._targetId, PushMessage(message._id, &MessageBox::Inbox)); } }
Yes it is possible to do the basics of functional programming in Synergy. It is missing some of the conveniences of C++ or ML but still it works. sometime in the future I will explain how this is useful to the average user.
import System.Collectionsnamespace functor ;;; <summary> ;;; The main entry point for the application. ;;; </summary> main record al, @ArrayList ii, int proc al = new ArrayList() for ii from 0 thru 100 begin data iii, @int iii = (@int)ii al.add(iii) end open(1, o, "tt:") for_each.Call(al, new MyFunctor()) endmain class for_each public static method Call, void al, @ArrayList func, @SimpleIntFunctor endparams record ii, int proc for ii from 0 thru al.Count - 1 begin func.Call((int)al[ii]) end endmethod endclass abstract class SimpleIntFunctor public abstract method Call, void i, int proc endmethod endclass class MyFunctor extends SimpleIntFunctor public override method Call, void i, int proc writes(1, %string(i)) endmethod endclass endnamespace
template<typename T> class MemberPointerContainer { typedef int T::* MEMPTR; public: MemberPointerContainer(MEMPTR element) { _element = element; } template<typename R> static MemberPointerContainer CreateMemberPointer(R T::* element) { return MemberPointerContainer(reinterpret_cast<MEMPTR>(element)); } template<typename R> void Set(T& t, R value) { typedef R T::* MEMPTR_SPECIALIZED; t.*(reinterpret_cast<MEMPTR_SPECIALIZED>(_element)) = value; } MEMPTR _element; }; class NetworkEntity { public: int field1; int field2; double field3; }; #include<vector> void main() { std::vector< MemberPointerContainer<NetworkEntity> > vect; vect.push_back(MemberPointerContainer<NetworkEntity>::CreateMemberPointer(&NetworkEntity::field1)); vect.push_back(MemberPointerContainer<NetworkEntity>::CreateMemberPointer(&NetworkEntity::field2)); vect.push_back(MemberPointerContainer<NetworkEntity>::CreateMemberPointer(&NetworkEntity::field3)); NetworkEntity net; vect[0].Set(net, 5); }
As it turns out I really like the idea of languages other than C++, such as LISP or ML its just that when I see the syntax I want to rip my eyes out and throw them in an incinerator. Im sure they are great languages and LISP apparently even preforms as well as C++ but I just cant live with out C++. I guess I'm stuck with it, at the ripe old age of 20 and I'm to old to learn a new language. Well not totally true, I program in C# at work and I guess I do technically know DBL but I don't do anything in DBL and C# is practically just C--, so I'm right at home with it.
Ive yet again picked up working on Archspace 2, this must be the 100th time. Each time I come back to it though I look at the design and tweak it a little, eventually I might actually get the one I want. Hopefully when I do its crazy awesome good. Just a little overview for those of you who care, Ive taken a loosely coupled approach to design. Pretty much everything is an entity and entity's can contain other entity's, of course. That was design round one, in round two I realized that I would have to do a little more then just basic updates of entity's in their own self contained worlds but it would be horribly hard to do so looking through the entity's children for all the info. So I decided to keep the children and add some members that were really just pointers to the children but with type info associated. At this point I have the architecture to update anything in the game using implementations of a visitor and in that update I can actualy do something with the info contained.
Lately I've been working on the next version of Archspace. While I knew about a lot of the great things in boost I had never really used them in practice. Well I've found my new digital best friend, Boost Lambda Library. Its just so damn cool though I should say that it wouldn't be nearly as cool without Boost Bind along with it.